YUKI Hiroshi
null+****@clear*****
Thu Oct 30 18:30:50 JST 2014
YUKI Hiroshi 2014-10-30 18:30:50 +0900 (Thu, 30 Oct 2014) New Revision: 3bc4cf12b2578a9ca7165194cd610efc23cf48b7 https://github.com/droonga/express-droonga/commit/3bc4cf12b2578a9ca7165194cd610efc23cf48b7 Message: Send messages after the receiver for responses is completely initialized Modified files: lib/droonga-protocol/connection.js Modified: lib/droonga-protocol/connection.js (+24 -0) =================================================================== --- lib/droonga-protocol/connection.js 2014-10-30 18:27:13 +0900 (bab676e) +++ lib/droonga-protocol/connection.js 2014-10-30 18:30:50 +0900 (5dda6ab) @@ -48,6 +48,7 @@ util.inherits(Connection, EventEmitter); Connection.prototype._init = function() { this.closed = false; this._callbacks = {}; + this._pendingMessages = []; this._id = Date.now(); this.tag = process.env.DROONGA_ENGINE_TAG || this._params.tag || @@ -116,6 +117,7 @@ Connection.prototype._initReceiver = function() { { logger: this._logger }); receiver.listen((function() { this.receivePort = receiver.port; + this._sendPendingMessages(); }).bind(this)); var tag = this.tag + '.message'; @@ -194,6 +196,15 @@ Connection.prototype.emitMessage = function(type, body, callback, options) { callback = null; } + if (!this.receivePort) { + this._logger.debug('Connection.emitMessage %d (%s): ' + + 'Receiver is not initialized yet. ' + + 'Given message will be sent later.', + this._id, this.hostAndPort); + this._pendingMessages.push([type, body, callback, options]); + return null; + } + var id = createId(); this._logger.debug('Connection.emitMessage %d (%s):', this._id, this.hostAndPort); @@ -239,6 +250,19 @@ Connection.prototype.thenableEmitMessage = function(type, body, options) { }).bind(this)); }; +Connection.prototype._sendPendingMessages = function() { + this._logger.debug('Connection._sendPendingMessages %d (%s): ' + + 'Send %d pending message(s).', + this._id, this.hostAndPort, + this._pendingMessages.length); + if (this._pendingMessages.length) { + this._pendingMessages.forEach(function(args) { + this.emitMessage.apply(this, args); + }, this); + this._pendingMessages = []; + } +}; + Connection.prototype.close = function() { this.removeAllListeners(); -------------- next part -------------- HTML����������������������������...Télécharger