Navigation Menu

Skip to content

Commit

Permalink
Fix a bug that server process is died on engine connection error
Browse files Browse the repository at this point in the history
"error" message is special in EventEmitter. If there is no handler
for "error" message, node.js is aborted.

This change adds "error" message handler. It returns an error response
to clients.
  • Loading branch information
kou committed Apr 7, 2014
1 parent 4543f1f commit a4f348b
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/droonga-protocol/connection.js
Expand Up @@ -60,6 +60,7 @@ Connection.prototype._init = function() {
};

Connection.prototype._initSender = function(wait) {
this._sendingMessages = {};
var options = { host: process.env.DROONGA_ENGINE_HOST ||
this._params.hostName ||
DEFAULT_FLUENT_HOST_NAME,
Expand All @@ -69,7 +70,29 @@ Connection.prototype._initSender = function(wait) {
var sender = fluent.createFluentSender(this.tag, options);
this._sender = sender;
this._sender.on('error', (function(error) {
this.emit('error', error);
var errorMessage =
'An error is occurred in protocol adapter: ' +
'[' + error.name + '] ' + error.message;
var ids = Object.keys(this._sendingMessages);
if (ids.length == 0) {
console.error(errorMessage, error);
} else {
ids.forEach(function(id) {
var sendingMessage = this._sendingMessages[id];
var message = {
inReplyTo: id,
statusCode: 500,
type: sendingMessage.type + '.result',
body: {
name: 'ProtocolAdapterError',
message: errorMessage,
detail: error
}
}
this.emit('reply:' + id, message.statusCode, message);
}.bind(this));
this._sendingMessages = {};
}
}).bind(this));
};

Expand Down Expand Up @@ -109,6 +132,7 @@ Connection.prototype._handleMessage = function(envelope) {
}
var inReplyTo = envelope.inReplyTo;
if (inReplyTo) {
delete this._sendingMessages[inReplyTo];
debug('Connection._handleMessage.reply %d:', this._id, inReplyTo);
var errorCode = envelope.statusCode;
if (!errorCode || isSuccess(errorCode))
Expand Down Expand Up @@ -199,6 +223,9 @@ Connection.prototype.emitMessage = function(type, body, callback, options) {
}
}).bind(this), options.timeout);
}
this._sendingMessages[id] = {
type: type
};
this._sender.emit('message', envelope, options.emittedCallback);
return envelope;
};
Expand Down

0 comments on commit a4f348b

Please sign in to comment.