Class: GenServer

Clusterluck.GenServer(kernel)

new GenServer(kernel)

Generic server implementation. Basic unit of logic handling in clusterluck. Provides the ability to send/receive messages across a cluster using call, cast, multicall, and abcast. Largely derived from Erlang's gen_server model, but incorporated into node.js' EventEmitter model. Internally, message streams are memoized until finished, which then triggers the firing of an event for given event (say we're listening for event "hello", if this server receives a message stream with event "hello", we'll fire it under the hood). In addition to message streams, message singletons are supported, which are message streams with a singleton message and bypass any stream memoization.

Think of it like event emitters that are targetable across a cluster, as well as internal to a node.

Parameters:
Name Type Description
kernel Clusterluck.NetKernel

Network kernel this instance will listen for messages on.

Source:

Methods

abcast(nodes, id, event, data) → {Clusterluck.GenServer}

Analogous to cast, but makes the cast to the GenServer with name id on each node in nodes.

Parameters:
Name Type Description
nodes Array

Array of nodes to send this message to.

id String

Receiving GenServer.

event String

Event this message will be emitted under on the receiving GenServer.

data Buffer | String | Number | Boolean | Object | Array

Data to send with this message.

Source:
Returns:

This instance.

Type
Clusterluck.GenServer

call(id, event, data, cbopt, timeoutopt) → {Stream}

Makes a synchronous request against id with event event and event args data. If id is an object, it will be parsed into a GenServer id and a receiving node. If a callback is supplied, the response will be aggregated and returned on success as cb(null, res) or on error as cb(err). In either case, the response stream is returned, which can be used to pipe to other streams.

Parameters:
Name Type Attributes Description
id String | Object

Receiving GenServer. Can be a string, in which case this message will be sent locally to id. If an object, will be parsed into a receiving node and GenServer id.

event String

Event this message will be emitted under on the receiving GenServer.

data Buffer | String | Number | Boolean | Object | Array

Data to send with this message.

cb function <optional>

Optional callback for response/error handling.

timeout Number <optional>

Timeout to wait for response before returning an error. If no callback is supplied, the response stream will emit an equivalent error. Defaults to Infinity, meaning no timeout will occur.

Source:
Returns:

Response stream for request.

Type
Stream

cast(id, event, data) → {Clusterluck.GenServer}

Makes an asynchronous request against id with event event and event args data. If id is an object, it will be parsed into a GenServer id and a receiving node.

Parameters:
Name Type Description
id String | Object

Receiving GenServer. Can be a string, in which case this message will be sent locally to id. If an object, will be parsed into a receiving node and GenServer id.

event String

Event this message will be emitted under on the receiving GenServer.

data Buffer | String | Number | Boolean | Object | Array

Data to send with this message.

Source:
Returns:

This instance.

Type
Clusterluck.GenServer

(abstract) decodeJob(job) → {Object}

Parses a fully memoized message stream into an object containing a key/value pair. If we fail to parse the job buffer (invalid JSON, etc), we just return an error and this GenServer will skip emitting an event. Otherwise, triggers user-defined logic for the parsed event.

Parameters:
Name Type Description
job Buffer

Memoized buffer that represents complete message stream.

Source:
Returns:

Object containing an event and data key/value pair, which are used to emit an event for user-defined logic.

Type
Object

(abstract) decodeJob(job) → {Object}

Parses a message singleton into an object containing a key/value pair. If we fail to parse the job, we just return an error and this GenServer will skip emitting an event. Otherwise, triggers user-defined logic for the parsed event.

Parameters:
Name Type Description
job Object

Memoized buffer that represents complete message stream.

Source:
Returns:

Object containing an event and data key/value pair, which are used to emit an event for user-defined logic.

Type
Object

id(idopt) → {String}

Acts as a getter/setter for the ID of this instance.

Parameters:
Name Type Attributes Description
id String <optional>

Handler ID to set on this instance.

Source:
Returns:

The ID of this instance.

Type
String

(abstract) idle() → {Boolean}

Returns whether this handler is in an idle state or not.

Source:
Returns:

Whether this process is idle or not.

Type
Boolean

kernel(kernelopt) → {Clusterluck.NetKernel}

Acts as a getter/setter for the netkernel of this instance.

Parameters:
Name Type Attributes Description
kernel Clusterluck.NetKernel <optional>

NetKernel to set on this instance.

Source:
Returns:

NetKernel of this instance.

Type
Clusterluck.NetKernel

multicall(nodes, id, event, data, cbopt, timeoutopt) → {Array}

Analogous to call, but makes the call to the GenServer with name id on each node in nodes.

Parameters:
Name Type Attributes Description
nodes Array

Array of nodes to send this message to.

id String

Receiving GenServer name.

event String

Event this message will be emitted under on the receiving GenServer.

data Buffer | String | Number | Boolean | Object | Array

Data to send with this message.

cb function <optional>

Optional callback for response/error handling.

timeout Number <optional>

Timeout to wait for response before returning an error. If no callback is supplied, the response stream will emit an equivalent error. Defaults to Infinity, meaning no timeout will occur.

Source:
Returns:

Array of response streams.

Type
Array

(abstract) pause() → {Clusterluck.GenServer}

Pauses the GenServer's message processing. As a result, any messages routed from the network kernel to this GenServer will be missed until this.resume() is called.

Source:
Fires:
Returns:

This instance.

Type
Clusterluck.GenServer

reply(from, data) → {Clusterluck.GenServer}

Replies to the sending node for an event stream. Used when a triggered event contains synchronous logic, in which we need to respond to the calling node of this event.

Parameters:
Name Type Description
from Object

Object received on synchronous message. This contains a tag to uniquely identify the request on the sender's end.

Properties
Name Type Description
tag String

Unique identifer for request.

node Clusterluck.Node

Node representing the sender.

data Buffer | String | Number | Boolean | Object | Array

Data to reply to synchronous message with.

Source:
Returns:

This instance.

Type
Clusterluck.GenServer

(abstract) resume() → {Clusterluck.GenServer}

Resumes the processing of message streams on this GenServer. Any messages missed in between pausing and now will result in failed message parsing (since all JSON contents are sent in one message).

Source:
Fires:
Listens to Events:
Returns:

This instance.

Type
Clusterluck.GenServer

(abstract) start(nameopt)

Starts this GenServer. Use this to start processing messages. As a result of this function call, any message with an id equal to this server's id (or name, if provided) will be routed by the network kernel to this server. If called multiple times in a row, this function will throw, since name uniqueness is enforced by the network kernel. If no name is provided, the generated short id at construction-time will be used as the routing name.

Parameters:
Name Type Attributes Description
name String <optional>

Name to register this handler with instead of the unique id attached. Any message received on the network kernel with id name will be routed to this instance for message stream parsing, and possible event firing.

Source:
Listens to Events:

(abstract) stop()

Stops the processing of messages on this GenServer. As a result, any further messages sent to the network kernel will fail to route to this GenServer. Additionally, all current message streams will be discarded, and the current name will be regenerated to another short id.

Source:
Fires:

streams(streamsopt) → {Map}

Acts as a getter/setter for the internal message stream map of this instance.

Parameters:
Name Type Attributes Description
streams Map <optional>

Internal message stream map to set on this instance.

Source:
Returns:

Internal message stream map of this instance.

Type
Map

Events

GenServer:idle

Emitted when the number of active messages streams on this server is zero (or, if extending the GenServer class, when this.idle() returns true).

Source:
Listeners of This Event:
Example
server.once("idle", () => {
  // now we could safely stop the server w/o killing any message streams
});

GenServer:pause

Emitted when the server has paused. On 'pause', if the server is started with name name, the network kernel will remove this server's event handler for name.

Source:
Listeners of This Event:
Example
let name = server.id();
let old = server.kernel().listeners(name).length;
server.once("pause", () => {
  assert.lengthOf(server.kernel().listeners(name), old-1);
});
server.pause();

GenServer:resume

Emitted when the server has been resumed. Before 'resume' is emitted, the network kernel restarts the server's event handler for name, where name is the name of the server.

Source:
Example
let name = server.id();
let old = server.kernel().listeners(name).length;
server.once("resume", () => {
  assert.lengthOf(server.kernel().listeners(name), old+1);
});
server.resume();

GenServer:stop

Emitted when the server has completely stopped executing handler logic on any user-defined events it's listening for.

Source:
Listeners of This Event:
Example
// in any subclasses og GenServer, we could make stop fire asynchronously
server.on("stop", () => {
  console.log("We've stopped!");
});
server.stop();

GenServer:user_defined

Events defined by users for message handling.

Suppose we have a GenServer named server started with name name_goes_here. Any message stream routed to our node, both internal and external, with id set to name_goes_here will be gathered by serve into a map of streams. Each stream has a stream ID stream and a boolean flag done which indicates whether the stream has finished sending data. This stream ID is used as an index for stream memoization between concurrent requests.

// 'data' is the input data for this part of the stream
if (!this._streams.has(stream.stream)) {
  this._streams.set(stream.stream, {data: Buffer.from(""), ...});
}
let inner = this._streams.get(stream.stream);
inner.data = Buffer.concat([inner.data, data], ...);

Once sending has finished, the job is parsed using decodeJob into an event and message, under event and data respectively. From here, the server emits an event just like an EventEmitter with event and data.

// similar to this
if (stream.done === true) {
  let job = this.decodeJob(memo);
  serve.emit(job.event, job.data, from);
}

This is where our own event handling logic comes into play. Everything else is handled under the hood by this class.

Diagram of data flow for message streams:

Messages comes into node with id set to `name_goes_here` ---------------> this._parse
Message comes into node with `stream.done` set to true -----------------> this.decodeJob ---> this.emit(event, data,from) ---> this.on(event, handlerLogic...)

As for singleton message streams (a message stream with only one message, and done set to true), no memoization of streams into a stream map occurs. The data is parsed by decodeSingleton into an event and message, under event and data respectively. From here, the server emits an event just like an EventEmitter with event and data.

Diagram of data flow for message singletons:

Messages comes into node with id set to `name_goes_here` ---------------> this._parse ---> this.decodeSingleton ---> this.emit(event, data,from) ---> this.on(event, handlerLogic...)
Properties:
Name Type Description
data Any

Data emitted with this user-defined event. Analogous to how an EventEmitter emits events.

from Object

Object received on synchronous message. This contains a tag to uniquely identify the request on the sender's end.

Properties
Name Type Description
tag String

Unique identifer for request.

node Clusterluck.Node

Node representing the sender.

Source:
Example
server.on("hello", (data, from) => {
  server.reply(from, "world");
});