cluster-client
Advanced tools
Comparing version 1.6.0 to 1.6.1
1.6.1 / 2017-04-20 | ||
================== | ||
* fix: invoke before client ready issue (#23) | ||
* fix: fix symbol property error (#22) | ||
1.6.0 / 2017-04-18 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -46,3 +46,3 @@ 'use strict'; | ||
this[closeHandler] = () => { | ||
this[logger].warn('[ClusterClient#%s] %s closed, and try to init it again', this.options.name, this[innerClient].isLeader ? 'leader' : 'follower'); | ||
this[logger].warn('[ClusterClient:%s] %s closed, and try to init it again', this.options.name, this[innerClient].isLeader ? 'leader' : 'follower'); | ||
this[isReady] = false; | ||
@@ -82,3 +82,3 @@ this.ready(false); | ||
} else { | ||
this[logger].info('[ClusterClient#%s] init cluster client, try to seize the leader on port:%d', name, port); | ||
this[logger].info('[ClusterClient:%s] init cluster client, try to seize the leader on port:%d', name, port); | ||
server = yield ClusterServer.create(name, port); | ||
@@ -89,6 +89,6 @@ } | ||
this[innerClient] = new Leader(Object.assign({ server }, this.options)); | ||
this[logger].info('[ClusterClient#%s] has seized port %d, and serves as leader client.', name, port); | ||
this[logger].info('[ClusterClient:%s] has seized port %d, and serves as leader client.', name, port); | ||
} else { | ||
this[innerClient] = new Follower(this.options); | ||
this[logger].info('[ClusterClient#%s] gives up seizing port %d, and serves as follower client.', name, port); | ||
this[logger].info('[ClusterClient:%s] gives up seizing port %d, and serves as follower client.', name, port); | ||
} | ||
@@ -133,5 +133,5 @@ | ||
[subscribe](reg, listener) { | ||
assert(is.function(listener), `[ClusterClient#${this.options.name}] subscribe(reg, listener) listener should be a function`); | ||
assert(is.function(listener), `[ClusterClient:${this.options.name}] subscribe(reg, listener) listener should be a function`); | ||
this[logger].info('[ClusterClient#%s] subscribe %j', this.options.name, reg); | ||
this[logger].info('[ClusterClient:%s] subscribe %j', this.options.name, reg); | ||
const key = this.options.formatKey(reg); | ||
@@ -155,3 +155,3 @@ const registrations = this[subInfo].get(key) || []; | ||
[unSubscribe](reg, listener) { | ||
this[logger].info('[ClusterClient#%s] unSubscribe %j', this.options.name, reg); | ||
this[logger].info('[ClusterClient:%s] unSubscribe %j', this.options.name, reg); | ||
const key = this.options.formatKey(reg); | ||
@@ -181,3 +181,3 @@ const registrations = this[subInfo].get(key) || []; | ||
[publish](reg) { | ||
this[logger].info('[ClusterClient#%s] publish %j', this.options.name, reg); | ||
this[logger].info('[ClusterClient:%s] publish %j', this.options.name, reg); | ||
const key = this.options.formatKey(reg); | ||
@@ -201,8 +201,14 @@ this[pubInfo].set(key, reg); | ||
if (!this[isReady]) { | ||
this.ready(() => this[invoke](method, args, callback)); | ||
this.ready(err => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
this[innerClient].invoke(method, args, callback); | ||
}); | ||
return; | ||
} | ||
debug('[ClusterClient#%s] invoke method: %s, args: %j', this.options.name, method, args); | ||
return this[innerClient].invoke(method, args, callback); | ||
debug('[ClusterClient:%s] invoke method: %s, args: %j', this.options.name, method, args); | ||
this[innerClient].invoke(method, args, callback); | ||
} | ||
@@ -212,13 +218,18 @@ | ||
return co(function* () { | ||
// close after ready, in case of innerClient is initializing | ||
yield this.ready(); | ||
try { | ||
// close after ready, in case of innerClient is initializing | ||
yield this.ready(); | ||
} catch (err) { | ||
// ignore | ||
} | ||
const client = this[innerClient]; | ||
// prevent re-initializing | ||
client.removeListener('close', this[closeHandler]); | ||
if (client) { | ||
// prevent re-initializing | ||
client.removeListener('close', this[closeHandler]); | ||
if (client.close) { | ||
yield utils.callFn(client.close.bind(client)); | ||
if (client.close) { | ||
yield utils.callFn(client.close.bind(client)); | ||
} | ||
} | ||
this.removeAllListeners(); | ||
}.bind(this)); | ||
@@ -225,0 +236,0 @@ } |
'use strict'; | ||
const ConsoleLogger = require('zlogger'); | ||
const { Logger, ConsoleTransport } = require('egg-logger'); | ||
const { consoleFormatter } = require('egg-logger/lib/utils'); | ||
const logger = new Logger(); | ||
logger.set('console', new ConsoleTransport({ | ||
level: 'INFO', | ||
formatter: consoleFormatter, | ||
})); | ||
module.exports = new ConsoleLogger({ | ||
prefix: '> ', | ||
}); | ||
module.exports = logger; |
@@ -9,3 +9,2 @@ 'use strict'; | ||
const Response = require('./protocol/response'); | ||
const empty = () => {}; | ||
@@ -106,3 +105,3 @@ class Follower extends Base { | ||
if (!this._subInfo.has(key)) { | ||
this.logger.info('[Follower#%s] subscribe %j for first time', this.options.name, reg); | ||
this.logger.info('[Follower:%s] subscribe %j for first time', this.options.name, reg); | ||
const req = new Request({ | ||
@@ -121,3 +120,3 @@ connObj: { type: 'subscribe', key, reg }, | ||
} else if (this._subData.has(key)) { | ||
this.logger.info('[Follower#%s] subscribe %j', this.options.name, reg); | ||
this.logger.info('[Follower:%s] subscribe %j', this.options.name, reg); | ||
process.nextTick(() => { | ||
@@ -138,3 +137,3 @@ listener(this._subData.get(key)); | ||
if (this.listeners(key).length === 0) { | ||
this.logger.info('[Follower#%s] no more subscriber for %j, send unSubscribe req to leader', this.options.name, reg); | ||
this.logger.info('[Follower:%s] no more subscriber for %j, send unSubscribe req to leader', this.options.name, reg); | ||
this._subInfo.delete(key); | ||
@@ -201,7 +200,2 @@ | ||
_connect(done) { | ||
done = done || empty; | ||
return super._connect(done); | ||
} | ||
_registerChannel() { | ||
@@ -212,3 +206,2 @@ // make sure socket exists | ||
} | ||
const req = new Request({ | ||
@@ -221,3 +214,2 @@ connObj: { | ||
}); | ||
// send invoke request | ||
@@ -234,3 +226,3 @@ this.send({ | ||
} | ||
this.logger.info('[Follower#%s] register to channel: %s success', this.options.name, this.options.name); | ||
this.logger.info('[Follower:%s] register to channel: %s success', this.options.name, this.options.name); | ||
this.ready(true); | ||
@@ -251,3 +243,3 @@ }); | ||
_handleRequest(req) { | ||
debug('[Follower#%s] receive req: %j from leader', this.options.name, req); | ||
debug('[Follower:%s] receive req: %j from leader', this.options.name, req); | ||
const connObj = req.connObj || {}; | ||
@@ -254,0 +246,0 @@ if (connObj.type === 'subscribe_result') { |
@@ -191,3 +191,4 @@ 'use strict'; | ||
const keys = Reflect.ownKeys(proto) | ||
.filter(key => !key.startsWith('_') && | ||
.filter(key => typeof key !== 'symbol' && | ||
!key.startsWith('_') && | ||
!this._descriptors.has(key)); | ||
@@ -194,0 +195,0 @@ |
@@ -34,2 +34,3 @@ 'use strict'; | ||
this._transcode = this.options.transcode; | ||
this._isReady = false; | ||
// the real client | ||
@@ -48,2 +49,3 @@ this._realClient = this.options.createRealClient(); | ||
} else { | ||
this._isReady = true; | ||
this.ready(true); | ||
@@ -146,2 +148,14 @@ } | ||
invoke(methodName, args, callback) { | ||
if (!this._isReady) { | ||
this.ready(err => { | ||
if (err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
return; | ||
} | ||
this.invoke(methodName, args, callback); | ||
}); | ||
return; | ||
} | ||
let method = this._realClient[methodName]; | ||
@@ -194,3 +208,3 @@ // compatible with generatorFunction | ||
if (conn) { | ||
this.logger.info('[Leader#%s] push subscribe data to cluster client#%s', this.options.name, connKey); | ||
this.logger.info('[Leader:%s] push subscribe data to cluster client#%s', this.options.name, connKey); | ||
conn.send(new Request({ | ||
@@ -233,3 +247,3 @@ timeout, | ||
_handleConnection(socket) { | ||
this.logger.info('[Leader#%s] socket connected, port: %d', this.options.name, socket.remotePort); | ||
this.logger.info('[Leader:%s] socket connected, port: %d', this.options.name, socket.remotePort); | ||
@@ -275,7 +289,7 @@ const conn = new Connection({ | ||
case 'subscribe': | ||
this.logger.info('[Leader#%s] received subscribe request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
this.logger.info('[Leader:%s] received subscribe request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
this._handleSubscribe(req, conn); | ||
break; | ||
case 'unSubscribe': | ||
this.logger.info('[Leader#%s] received unSubscribe request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
this.logger.info('[Leader:%s] received unSubscribe request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
this._handleUnSubscribe(req, conn); | ||
@@ -285,3 +299,3 @@ break; | ||
{ | ||
debug('[Leader#%s] received invoke request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
debug('[Leader:%s] received invoke request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
const argLength = connObj.argLength; | ||
@@ -306,3 +320,3 @@ const args = []; | ||
if (req.timeout && Date.now() - startTime > req.timeout) { | ||
const err = new Error(`[Leader#${this.options.name}] invoke method:${connObj.method} timeout for req#{req.id}`); | ||
const err = new Error(`[Leader:${this.options.name}] invoke method:${connObj.method} timeout for req#{req.id}`); | ||
err.name = 'ClusterLeaderTimeoutError'; | ||
@@ -327,3 +341,3 @@ err.method = connObj.method; | ||
} else { | ||
debug('[Leader#%s] send method:%s result to follower, result: %j', this.options.name, connObj.method, result); | ||
debug('[Leader:%s] send method:%s result to follower, result: %j', this.options.name, connObj.method, result); | ||
const data = this._transcode.encode(result); | ||
@@ -342,3 +356,3 @@ res.connObj = { | ||
case 'heartbeat': | ||
debug('[Leader#%s] received heartbeat request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
debug('[Leader:%s] received heartbeat request from follower, req: %j, conn: %s', this.options.name, req, conn.key); | ||
res.connObj = { type: 'heartbeat_res' }; | ||
@@ -379,3 +393,3 @@ conn.send(res); | ||
return co(function* () { | ||
this.logger.info('[Leader#%s] try to close leader', this.options.name); | ||
this.logger.info('[Leader:%s] try to close leader', this.options.name); | ||
// 1. stop listening to server channel | ||
@@ -402,3 +416,3 @@ this._server.removeListener(`${this.options.name}_connection`, this._handleConnection); | ||
setTimeout(() => { | ||
reject(new Error(`[Leader#${this.options.name}] close failed: follower connections are still not closed after 30s`)); | ||
reject(new Error(`[Leader:${this.options.name}] close failed: follower connections are still not closed after 30s`)); | ||
}, 30000); | ||
@@ -405,0 +419,0 @@ }); |
{ | ||
"name": "cluster-client", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Sharing Connection among Multi-Process Nodejs", | ||
@@ -36,2 +36,3 @@ "main": "./index.js", | ||
"debug": "^2.6.3", | ||
"egg-logger": "^1.5.0", | ||
"is-type-of": "^1.0.0", | ||
@@ -42,5 +43,4 @@ "json-stringify-safe": "^5.0.1", | ||
"serialize-json": "^1.0.1", | ||
"tcp-base": "^2.0.0", | ||
"utility": "^1.11.0", | ||
"zlogger": "^1.1.0" | ||
"tcp-base": "^3.0.0", | ||
"utility": "^1.12.0" | ||
}, | ||
@@ -53,5 +53,5 @@ "devDependencies": { | ||
"egg": "^0.5.0", | ||
"egg-bin": "^3.2.1", | ||
"egg-bin": "^3.3.0", | ||
"egg-ci": "^1.6.0", | ||
"egg-mock": "^3.2.0", | ||
"egg-mock": "^3.4.0", | ||
"eslint": "^3.12.0", | ||
@@ -58,0 +58,0 @@ "eslint-config-egg": "^3.2.0", |
66400
1725
+ Addedegg-logger@^1.5.0
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcircular-json@0.5.9(transitive)
+ Addeddepd@1.1.2(transitive)
+ Addedegg-logger@1.8.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedtcp-base@3.2.0(transitive)
- Removedzlogger@^1.1.0
- Removedduplexify@3.7.1(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@1.0.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedpump@2.0.1(transitive)
- Removedpumpify@1.5.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsplit2@2.2.0(transitive)
- Removedstream-shift@1.0.3(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtcp-base@2.0.0(transitive)
- Removedthrough2@2.0.5(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)
- Removedzlogger@1.1.0(transitive)
Updatedtcp-base@^3.0.0
Updatedutility@^1.12.0