node-modules/cluster-client

Name: cluster-client

Owner: node_modules

Description: Sharing Connection among Multi-Process Nodejs

Created: 2016-12-12 03:03:39.0

Updated: 2018-05-21 17:39:06.0

Pushed: 2018-05-03 03:33:48.0

Homepage: null

Size: 121

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

cluster-client

Sharing Connection among Multi-Process Nodejs

NPM version build status Test coverage David deps Known Vulnerabilities npm download

As we know, each Node.js process runs in a single thread. Usually, we split a single process into multiple processes to take advantage of multi-core systems. On the other hand, it brings more system overhead, sush as maintaining more TCP connections between servers.

This module is designed to share connections among multi-process Nodejs.

Theory
Diagram

normal (without using cluster client)

-----+   +--------+
ient |   | Client |   ...
-----+   +--------+
|  \     /   |
|    \ /     |
|    / \     |
|  /     \   |
-----+   +--------+
rver |   | Server |   ...
-----+   +--------+

using cluster-client

         +-------+
         | start |
         +---+---+
             |
    +--------+---------+
  __| port competition |__
/   +------------------+  \ lose
                           \
-----+     tcp conn     +----------+
ader |<---------------->| Follower |
-----+                  +----------+
|
-----+
ient |
-----+
|  \
|    \
|      \
|        \
-----+   +--------+
rver |   | Server |   ...
-----+   +--------+
Protocol
--------+             +---------------+          +---------+
ollower |             |  local server |          |  Leader |
--------+             +---------------+          +---------+
  |     register channel     |       assign to        |
  + -----------------------> |  --------------------> |
  |                          |                        |
  |                                subscribe          |
  + ------------------------------------------------> |
  |       subscribe result                            |
  | <------------------------------------------------ +
  |                                                   |
  |                                 invoke            |
  + ------------------------------------------------> |
  |          invoke result                            |
  | <------------------------------------------------ +
  |                                                   |
Install
m install cluster-client --save

Node.js >= 6.0.0 required

Usage
 strict';

t co = require('co');
t Base = require('sdk-base');
t cluster = require('cluster-client');


lient Example

s YourClient extends Base {
nstructor(options) {
super(options);

this.options = options;
this.ready(true);


bscribe(reg, listener) {
// subscribe logic


blish(reg) {
// publish logic


getData(id) {
// invoke api


tDataCallback(id, cb) {
// ...


tDataPromise(id) {
// ...



reate some client instances, but only one instance will connect to server
t client_1 = cluster(YourClient)
elegate('getData')
elegate('getDataCallback')
elegate('getDataPromise')
reate({ foo: 'bar' });
t client_2 = cluster(YourClient)
elegate('getData')
elegate('getDataCallback')
elegate('getDataPromise')
reate({ foo: 'bar' });
t client_3 = cluster(YourClient)
elegate('getData')
elegate('getDataCallback')
elegate('getDataPromise')
reate({ foo: 'bar' });

ubscribe information
nt_1.subscribe('some thing', result => console.log(result));
nt_2.subscribe('some thing', result => console.log(result));
nt_3.subscribe('some thing', result => console.log(result));

ublish data
nt_2.publish('some data');

nvoke method
nt_3.getDataCallback('some thing', (err, val) => console.log(val));
nt_2.getDataPromise('some thing').then(val => console.log(val));

unction*() {
nst ret = yield client_1.getData('some thing');
nsole.log(ret);
atch(err => console.error(err));
API
Best Practice
  1. DataClient
  2. Only provider data API, interact with server and maintain persistent connections etc.
  3. No need to concern cluster issue
  4. APIClient
  5. Using cluster-client to wrap DataClient
  6. Put your bussiness logic here

DataClient

t Base = require('sdk-base');

s DataClient extends Base {
nstructor(options) {
super(options);
this.ready(true);


bscribe(info, listener) {
// subscribe data from server


blish(info) {
// publish data to server


getData(id) {
// asynchronous API


APIClient

t DataClient = require('./your-data-client');
t { APIClientBase } = require('cluster-client');

s APIClient extends APIClientBase {
nstructor(options) {
super(options);
this._cache = new Map();

t DataClient() {
return DataClient;

t delegates() {
return {
  getData: 'invoke',
};

t clusterOptions() {
return {
  name: 'MyClient',
};

bscribe(...args) {
return this._client.subscribe(...args);

blish(...args) {
return this._client.publish(...args);

getData(id) {
// write your business logic & use data client API
if (this._cache.has(id)) {
  return this._cache.get(id);
}
const data = yield this._client.getData(id);
this._cache.set(id, data);
return datal


s
---------------------------------------------|
IClient                                      |
    |----------------------------------------|
    | ClusterClient                          |
    |      |---------------------------------|
    |      | DataClient                      |
----|------|---------------------------------|

For more information, you can refer to the discussion

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.