YUKI Hiroshi
null+****@clear*****
Mon Jan 28 19:09:20 JST 2013
YUKI Hiroshi 2013-01-28 19:09:20 +0900 (Mon, 28 Jan 2013) New Revision: 1103e06b54ff6e74291514fc63078834f786a890 https://github.com/groonga/express-kotoumi/commit/1103e06b54ff6e74291514fc63078834f786a890 Log: Re-send messages automatically with delay Modified files: lib/backend/connection.js Modified: lib/backend/connection.js (+26 -8) =================================================================== --- lib/backend/connection.js 2013-01-28 18:33:51 +0900 (e9d3fee) +++ lib/backend/connection.js 2013-01-28 19:09:20 +0900 (7d85ae1) @@ -40,13 +40,15 @@ Connection.prototype._init = function() { this._initReceiver(); }; -Connection.prototype._initSender = function() { - this._sender = ( - this._params.sender || - fluent.createFluentSender(this._params.tag || DEFAULT_FLUENT_TAG, - { host: this._params.hostName || DEFAULT_FLUENT_HOST_NAME, - port: this._params.port || DEFAULT_FLUENT_PORT }) - ); +Connection.prototype._initSender = function(wait) { + this._sender = this._params.sender; + if (!this._params.sender) { + var options = { host: this._params.hostName || DEFAULT_FLUENT_HOST_NAME, + port: this._params.port || DEFAULT_FLUENT_PORT }; + var sender = fluent.createFluentSender(this._params.tag || DEFAULT_FLUENT_TAG, + options) + this._sender = sender; + } }; Connection.prototype._initReceiver = function() { @@ -123,15 +125,31 @@ Connection.prototype.emitMessage = function(type, body, callback, timeout) { setTimeout((function() { if (this.listeners(event)) { this.removeAllListeners(event); + this._sender.removeAllListeners('error'); callback(ERROR_GATEWAY_TIMEOUT, null); } }).bind(this), timeout); } } - this._sender.emit('message', envelope); + setTimeout((function() { + this._sendEnvelope(envelope); + }).bind(this), 0); return envelope; }; +Connection.prototype._sendEnvelope = function(envelope, wait) { + var errorHandler = (function(error) { + wait = wait ? wait * 2 : 1000; + setTimeout((function() { + this._sendEnvelope(envelope, wait); + }).bind(this), wait); + }).bind(this); + this._sender.on('error', errorHandler); + this._sender.emit('message', envelope, (function() { + this._sender.removeListener('error', errorHandler); + }).bind(this)); +}; + Connection.prototype.close = function() { this.removeAllListeners(); -------------- next part -------------- HTML����������������������������...Download