Comparing version 3.5.33 to 3.5.34
104
drain.js
@@ -28,2 +28,10 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var GOAL_NOOP = 'noop'; | ||
var GOAL_CLOSE_DRAINED = 'close drained connections'; | ||
var GOAL_CLOSE_PEER = 'close peer'; | ||
PeerDrain.GOAL_NOOP = GOAL_NOOP; | ||
PeerDrain.GOAL_CLOSE_DRAINED = GOAL_CLOSE_DRAINED; | ||
PeerDrain.GOAL_CLOSE_PEER = GOAL_CLOSE_PEER; | ||
// TODO: subsume and unify with channel draining | ||
@@ -38,3 +46,9 @@ | ||
assert(!chan.draining, 'cannot drain a peer while channel is draining'); | ||
assert(!options.goal || | ||
options.goal === GOAL_NOOP || | ||
options.goal === GOAL_CLOSE_DRAINED || | ||
options.goal === GOAL_CLOSE_PEER, | ||
'expected a valid goal (if any)'); | ||
self.goal = options.goal || PeerDrain.GOAL_NOOP; | ||
self.channel = chan; | ||
@@ -51,3 +65,8 @@ self.peer = peer; | ||
self.finishedAt = 0; | ||
self.thenFinish = thenFinish; | ||
function thenFinish(err) { | ||
var now = self.channel.timers.now(); | ||
self.finish(err, now); | ||
} | ||
} | ||
@@ -59,4 +78,5 @@ | ||
info.drainGoal = self.goal; | ||
info.drainReason = self.reason; | ||
info.drainTimeout = self.timeout; | ||
info.drainReason = self.reason; | ||
info.drainDirection = self.direction; | ||
@@ -122,8 +142,18 @@ info.drainStartedAt = self.startedAt; | ||
} | ||
if (!self.finishedAt) { | ||
self.finishedAt = now; | ||
if (self.callback) { | ||
self.callback(err); | ||
self.callback = null; | ||
} | ||
switch (self.goal) { | ||
case GOAL_NOOP: | ||
self.finish(err, now); | ||
break; | ||
case GOAL_CLOSE_DRAINED: | ||
self.thenCloseDrained(err); | ||
break; | ||
case GOAL_CLOSE_PEER: | ||
self.thenClosePeer(err); | ||
break; | ||
default: | ||
self.finish(err || new Error('invalid drain goal'), now); | ||
} | ||
@@ -150,2 +180,62 @@ } | ||
PeerDrain.prototype.thenCloseDrained = | ||
function thenCloseDrained(err) { | ||
var self = this; | ||
if (err) { | ||
var info = self.peer.extendLogInfo(self.extendLogInfo({ | ||
error: err | ||
})); | ||
if (err.type === 'tchannel.drain.peer.timed-out') { | ||
self.peer.logger.warn( | ||
'drain timed out, force closing connections', | ||
info); | ||
} else { | ||
self.peer.logger.warn( | ||
'unexpected error draining connections, closing anyhow', | ||
info); | ||
} | ||
} | ||
self.peer.closeDrainedConnections(self.thenFinish); | ||
}; | ||
PeerDrain.prototype.thenClosePeer = | ||
function thenClosePeer(err) { | ||
var self = this; | ||
if (err) { | ||
var info = self.peer.extendLogInfo(self.extendLogInfo({ | ||
error: err | ||
})); | ||
if (err.type === 'tchannel.drain.peer.timed-out') { | ||
self.peer.logger.warn( | ||
'drain timed out, force closing peer', | ||
info); | ||
} else { | ||
self.peer.logger.warn( | ||
'unexpected error draining connections, closing peer anyhow', | ||
info); | ||
} | ||
} | ||
self.peer.close(self.thenFinish); | ||
}; | ||
PeerDrain.prototype.finish = | ||
function finish(err, now) { | ||
var self = this; | ||
if (!self.finishedAt) { | ||
self.finishedAt = now; | ||
if (self.callback) { | ||
self.callback(err); | ||
self.callback = null; | ||
} | ||
} | ||
}; | ||
PeerDrain.prototype.drainConnection = | ||
@@ -152,0 +242,0 @@ function drainConnection(conn) { |
@@ -5,3 +5,3 @@ { | ||
"author": "mranney@uber.com", | ||
"version": "3.5.33", | ||
"version": "3.5.34", | ||
"scripts": { | ||
@@ -8,0 +8,0 @@ "lint": "eslint $(git ls-files | grep '.js$')", |
@@ -123,2 +123,6 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
TChannelPeer.prototype.DRAIN_GOAL_NOOP = PeerDrain.GOAL_NOOP; | ||
TChannelPeer.prototype.DRAIN_GOAL_CLOSE_DRAINED = PeerDrain.GOAL_CLOSE_DRAINED; | ||
TChannelPeer.prototype.DRAIN_GOAL_CLOSE_PEER = PeerDrain.GOAL_CLOSE_PEER; | ||
TChannelPeer.prototype.toString = | ||
@@ -125,0 +129,0 @@ function toString() { |
1389411
36675