callosum-client-tcp

TCP client for Callosum: a self-balancing distributed services protocol

npm install callosum-client-tcp
12 downloads in the last week
24 downloads in the last month

callosum-client-tcp

Stability: 1 - Experimental

NPM version

TCP Client for Callosum: a self-balancing distributed services protocol.

Usage

var CallosumClient = require('callosum-client-tcp');
var callosumClient = new CallosumClient({MAX_SLOTS: 100});

callosumClient.on('error', function (error) {
    console.log(error);
});

// add a connection to the connection pool
// client will either hang on to the connection or close it if it is not needed
callosumClient.newConnection(slot, socket);

var socket = callosumClient.getConnection();
// socket is either a connection that is available or undefined

callosumClient.returnConnection(socket);
// return a previously leased connection to the client pool

Tests

npm test

Overview

TCP Client for Callosum: a self-balancing distributed services protocol. It servers as a connection pool.

Open available connections are maintained internally via two heap data structures. The min heap data structure is maintained in order to provide a connection from the pool with the lowest slot value. The max heap data structure is maintained in order to enable rapid checking and replacing of a connection with a high slot value if a new connection with a lower slot value becomes available.

Documentation

CallosumClient

Public API

new CallosumClient([options])

  • options: Object (Default: {})
    • MAX_SLOTS: Integer (Default: 100) Number of connection slots to
        maintain.
      

Creates a new instance of CallosumClient.

callosumClient.getConnection()

  • Return: Socket object
    • _destroyed: Boolean CAUTION: reserved for internal use
    • _destroySocket: Function CAUTION: reserved for internal use
    • _leased: Boolean CAUTION: reserved for internal use
    • _slot: Integer CAUTION: reserved for internal use

Returns next available socket with lowest slot number. If no sockets are available, undefined is returned.

callosumClient.insertSocket(slot, socket)

CAUTION: reserved for internal use

  • slot: Integer Slot number.
  • socket: Socket object Socket.

Internal bookkeeping of the socket via a min heap and a max heap with event listeners for socket destruction.

callosumClient.newConnection(slot, socket)

  • slot: Integer Slot number.
  • socket: Socket object Socket.

New connection (from a rover) is available for the client to keep or discard.

callosumClient.returnConnection(socket)

  • socket: Socket object
    • _destroyed: Boolean CAUTION: reserved for internal use
    • _destroySocket: Function CAUTION: reserved for internal use
    • _leased: Boolean CAUTION: reserved for internal use
    • _slot: Integer CAUTION: reserved for internal use

When the user of the socket is finished with it, instead of closing it, this method allows the socket to be handed back to Callosum.

npm loves you