package
Sharing Connection among Multi-Process Nodejs
npm install cluster-clientGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
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)
+--------+ +--------+
| Client | | Client | ...
+--------+ +--------+
| \ / |
| \ / |
| / \ |
| / \ |
+--------+ +--------+
| Server | | Server | ...
+--------+ +--------+
using cluster-client
+-------+
| start |
+---+---+
|
+--------+---------+
__| port competition |__
win / +------------------+ \ lose
/ \
+--------+ tcp conn +----------+
| Leader |<---------------->| Follower |
+--------+ +----------+
|
+--------+
| Client |
+--------+
| \
| \
| \
| \
+--------+ +--------+
| Server
0 1 2 4 12
+-------+-------+---------------+---------------------------------------------------------------+
|version|req/res| reserved | request id |
+-------------------------------+-------------------------------+-------------------------------+
| timeout | connection object length | application object length |
+-------------------------------+---------------------------------------------------------------+
| conn object (JSON format) ... | app object |
+-----------------------------------------------------------+ |
| ... |
+-----------------------------------------------------------------------------------------------+ +----------+ +---------------+ +---------+
| Follower | | local server | | Leader |
+----------+ +---------------+ +---------+
| register channel | assign to |
+ -----------------------> | --------------------> |
| | |
| subscribe |
+ ------------------------------------------------> |
| subscribe result |
| <------------------------------------------------ +
| |
| invoke |
+
npm install cluster-client --saveNode.js >= 6.0.0 required
'use strict';
const co = require('co');
const Base = require('sdk-base');
const cluster = require('cluster-client');
/**
* Client Example
*/
class YourClient extends Base {
constructor(options) {
super(options);
this.options = options;
this.ready(true);
}
subscribe(reg, listener) {
// subscribe logic
}
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 DataClientconst Base = require('sdk-base');
class DataClient extends Base {
constructor(options) {
super(options);
this.ready(true);
}
subscribe(info, listener) {
// subscribe data from server
}
publish(info) {
// publish data to server
}
* getData(id) {
// asynchronous API
}
}const DataClient = require('./your-data-client');
const { APIClientBase } = require('cluster-client');
class APIClient extends APIClientBase {
constructor(options) {
super(options);
this._cache = new Map();
}
get DataClient() {
return DataClient;
}
get delegates() {
return {
getData: 'invoke',
};
}
get clusterOptions() {
return {
name:
|------------------------------------------------|
| APIClient |
| |----------------------------------------|
| | ClusterClient |
| | |---------------------------------|
| | | DataClient |
|-------|------|---------------------------------|For more information, you can refer to the discussion
This project follows the git-contributor spec, auto updated at Tue Jun 20 2023 12:29:14 GMT+0800.
eggjs-admin
fengmk2+eggjs@gmail.com
semantic-release-bot |
|---|
atian25 |
|---|
leoner | mansonchor | sinkhaha | limitMe |
Estimated unpacked size across 25 packages.
1.3 MB
Includes dependencies.
^1.0.3
1.0.3
^5.0.1
5.0.1