Skip to content

Commit

Permalink
add ZK connection timemout
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunong Xiao committed Sep 5, 2012
1 parent 1746168 commit d72d8dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/client.js
Expand Up @@ -5,6 +5,7 @@ var path = require('path');
var util = require('util');

var vasync = require('vasync');
var verror = require('verror');
var ZK = require('zookeeper');

var assert = require('./assert');
Expand Down Expand Up @@ -67,6 +68,8 @@ function ZKClient(options) {
assertNumber('options.timeout', options.timeout);
if (options.pollInterval)
assertNumber('options.pollInterval', options.pollInterval);
if (options.connectTimeout)
assertNumber('options.connectTimeout', options.connectTimeout);

EventEmitter.call(this);

Expand All @@ -92,6 +95,12 @@ function ZKClient(options) {
});

this.timeout = options.timeout;
this.connectTimeout = options.connectTimeout || 2000;

this.connectTimeoutId = setTimeout(function() {
throw new verror.VError('could not connect to ZK');
}, self.connectTimeout);

this.zk = new ZK({
connect: _connect.join(','),
timeout: self.timeout,
Expand Down Expand Up @@ -156,6 +165,7 @@ ZKClient.prototype.connect = function connect(callback) {
self.emit('error', e);
callback(e);
} else {
clearTimeout(self.connectTimeoutId);
log.trace('connected');
self.pollState();
self.emit('connect');
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -9,6 +9,7 @@
"bunyan": "0.11.0",
"node-uuid": "1.3.3",
"vasync": "1.3.1",
"verror": "1.3.3",
"zookeeper": "git://github.com/mcavage/node-zookeeper.git#8a28f8c"
},
"devDependencies": {
Expand Down

0 comments on commit d72d8dd

Please sign in to comment.