cluster-client
Advanced tools
Comparing version
1.7.3 / 2018-04-28 | ||
================== | ||
**fixes** | ||
* [[`ddb2b86`](http://github.com/node-modules/cluster-client/commit/ddb2b86ff3b90213220fffa486d188852c85e227)] - fix: add connect timeout & logger connect error (#40) (zōng yǔ <<gxcsoccer@users.noreply.github.com>>) | ||
1.7.2 / 2018-04-03 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -11,2 +11,3 @@ 'use strict'; | ||
const Follower = require('./follower'); | ||
const sleep = require('mz-modules/sleep'); | ||
const ClusterServer = require('./server'); | ||
@@ -26,2 +27,3 @@ const { | ||
close, | ||
isClosed, | ||
} = require('./symbol'); | ||
@@ -47,2 +49,3 @@ | ||
this[isClosed] = false; | ||
this[closeHandler] = () => { | ||
@@ -85,4 +88,17 @@ this[logger].warn('[ClusterClient:%s] %s closed, and try to init it again', this.options.name, this[innerClient].isLeader ? 'leader' : 'follower'); | ||
} else if (this.options.isLeader === false) { | ||
// wait for leader active | ||
yield ClusterServer.waitFor(port, this.options.maxWaitTime); | ||
let leaderReady = false; | ||
const start = Date.now(); | ||
do { | ||
try { | ||
yield ClusterServer.tryToConnect(port); | ||
leaderReady = true; | ||
} catch (err) { | ||
this[logger].warn(err); | ||
// timeout | ||
if (Date.now() - start > this.options.maxWaitTime) { | ||
throw err; | ||
} | ||
yield sleep(3000); | ||
} | ||
} while (!leaderReady && !this[isClosed]); | ||
} else { | ||
@@ -234,2 +250,3 @@ debug('[ClusterClient:%s] init cluster client, try to seize the leader on port:%d', name, port); | ||
} | ||
this[isClosed] = true; | ||
}.bind(this)); | ||
@@ -236,0 +253,0 @@ } |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const Base = require('sdk-base'); | ||
const awaitFirst = require('await-first'); | ||
const Packet = require('./protocol/packet'); | ||
@@ -23,4 +24,2 @@ | ||
const sleep = timeout => cb => setTimeout(cb, timeout); | ||
function claimServer(port) { | ||
@@ -50,23 +49,2 @@ return cb => { | ||
function tryToConnect(port) { | ||
return cb => { | ||
const socket = net.connect(port, '127.0.0.1'); | ||
debug('try to connecting %s', port); | ||
let success = false; | ||
socket.on('connect', () => { | ||
success = true; | ||
cb(null, true); | ||
// disconnect | ||
socket.end(); | ||
debug('test connected %s success, end now', port); | ||
}); | ||
socket.on('error', err => { | ||
debug('test connect %s error: %s, success: %s', port, err, success); | ||
// if success before, ignore it | ||
if (success) return; | ||
cb(null, false); | ||
}); | ||
}; | ||
} | ||
class ClusterServer extends Base { | ||
@@ -227,23 +205,12 @@ /** | ||
/** | ||
* Wait for Leader Startup | ||
* | ||
* @param {Number} port - the port | ||
* @param {Number} timeout - the max wait time | ||
* @return {void} | ||
*/ | ||
static* waitFor(port, timeout) { | ||
const start = Date.now(); | ||
let connect = false; | ||
while (!connect) { | ||
connect = yield tryToConnect(port); | ||
// if timeout, throw error | ||
if (Date.now() - start > timeout) { | ||
throw new Error(`[ClusterClient] leader does not be active in ${timeout}ms on port:${port}`); | ||
} | ||
if (!connect) { | ||
yield sleep(3000); | ||
} | ||
} | ||
static* tryToConnect(port) { | ||
const socket = net.connect(port, '127.0.0.1'); | ||
socket.setTimeout(1000, () => { | ||
const err = new Error('socket#127.0.0.1:' + port + ' connect timeout(1000ms)'); | ||
err.name = 'ClusterClientConnectTimeoutError'; | ||
socket.destroy(err); | ||
}); | ||
debug('try to connecting %s', port); | ||
yield awaitFirst(socket, [ 'connect', 'error' ]); | ||
socket.destroy(); | ||
} | ||
@@ -250,0 +217,0 @@ } |
@@ -15,1 +15,2 @@ 'use strict'; | ||
exports.close = Symbol.for('ClusterClient#close'); | ||
exports.isClosed = Symbol.for('ClusterClient#isClosed'); |
{ | ||
"name": "cluster-client", | ||
"version": "1.7.2", | ||
"version": "1.7.3", | ||
"description": "Sharing Connection among Multi-Process Nodejs", | ||
@@ -35,27 +35,30 @@ "main": "./index.js", | ||
"dependencies": { | ||
"byte": "^1.2.0", | ||
"await-first": "^1.0.0", | ||
"byte": "^1.4.0", | ||
"co": "^4.6.0", | ||
"debug": "^3.0.1", | ||
"egg-logger": "^1.6.0", | ||
"debug": "^3.1.0", | ||
"egg-logger": "^1.6.2", | ||
"is-type-of": "^1.2.0", | ||
"json-stringify-safe": "^5.0.1", | ||
"long": "^3.2.0", | ||
"sdk-base": "^3.3.0", | ||
"serialize-json": "^1.0.1", | ||
"tcp-base": "^3.0.0", | ||
"utility": "^1.12.0" | ||
"long": "^4.0.0", | ||
"mz-modules": "^2.1.0", | ||
"sdk-base": "^3.4.0", | ||
"serialize-json": "^1.0.2", | ||
"tcp-base": "^3.1.0", | ||
"utility": "^1.13.1" | ||
}, | ||
"devDependencies": { | ||
"address": "^1.0.3", | ||
"autod": "^2.9.0", | ||
"autod": "^3.0.1", | ||
"autod-egg": "^1.1.0", | ||
"await-event": "^2.1.0", | ||
"coffee": "^4.1.0", | ||
"contributors": "^0.5.1", | ||
"egg-bin": "^4.3.3", | ||
"egg-bin": "^4.7.0", | ||
"egg-ci": "^1.8.0", | ||
"egg-mock": "^3.12.1", | ||
"eslint": "^4.7.1", | ||
"eslint-config-egg": "^5.1.1", | ||
"egg-mock": "^3.17.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-egg": "^7.0.0", | ||
"mm": "^2.2.0", | ||
"mz-modules": "^2.0.0", | ||
"mz-modules": "^2.1.0", | ||
"pedding": "^1.1.0", | ||
@@ -62,0 +65,0 @@ "spy": "^1.0.0" |
69877
0.24%13
18.18%15
7.14%1787
-0.67%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
Updated
Updated
Updated
Updated
Updated
Updated
Updated
Updated