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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Sharing Connection among Multi-Process Nodejs
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.
normal (without using cluster client)
-----+ +--------+
ient | | Client | ...
-----+ +--------+
| \ / |
| \ / |
| / \ |
| / \ |
-----+ +--------+
rver | | Server | ...
-----+ +--------+
using cluster-client
+-------+
| start |
+---+---+
|
+--------+---------+
__| port competition |__
/ +------------------+ \ lose
\
-----+ tcp conn +----------+
ader |<---------------->| Follower |
-----+ +----------+
|
-----+
ient |
-----+
| \
| \
| \
| \
-----+ +--------+
rver | | Server | ...
-----+ +--------+
1 2 4 12
----+-------+---------------+---------------------------------------------------------------+
sion|req/res| reserved | request id |
----------------------------+-------------------------------+-------------------------------+
timeout | connection object length | application object length |
----------------------------+---------------------------------------------------------------+
conn object (JSON format) ... | app object |
--------------------------------------------------------+ |
... |
--------------------------------------------------------------------------------------------+
--------+ +---------------+ +---------+
ollower | | local server | | Leader |
--------+ +---------------+ +---------+
| register channel | assign to |
+ -----------------------> | --------------------> |
| | |
| subscribe |
+ ------------------------------------------------> |
| subscribe result |
| <------------------------------------------------ +
| |
| invoke |
+ ------------------------------------------------> |
| invoke result |
| <------------------------------------------------ +
| |
m install cluster-client --save
Node.js >= 6.0.0 required
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));
delegate(from, to)
:
create delegate method, from
is the method name your want to create, and to
have 6 possible values: [ subscribe
, unSubscribe
, publish
, invoke
, invokeOneway
, close
], and the default value is invokeoverride(name, value)
:
override one propertycreate(?)
create the client instanceclose(client)
close the clientAPIClientBase
a base class to help you create your api clientcluster
issuecluster-client
to wrap DataClientDataClient
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