Navigation Menu

Skip to content

Commit

Permalink
Operate given timeout as a part of envelope
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 20, 2015
1 parent 4c0673d commit 7a3670d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
18 changes: 12 additions & 6 deletions lib/adapter/api/droonga.js
Expand Up @@ -14,10 +14,13 @@ module.exports = {
onRequest: function(request, connection) {
var messageType = pathToMessageType(request.params[0]);
var body = {};
if (typeof body.timeout != 'number' &&
var options = {
timeout: body.timeout
};
if (typeof options.timeout != 'number' &&
typeof request.query.timeout == 'number')
body.timeout = request.query.timeout;
connection.emit(messageType, body);
options.timeout = request.query.timeout;
connection.emit(messageType, body, options);
}
}),
// XXX dangerous! this must be disabled on public services.
Expand All @@ -32,11 +35,14 @@ module.exports = {
});
request.on('end', function() {
body = JSON.parse(body);
if (typeof body.timeout != 'number' &&
var options = {
timeout: body.timeout
};
if (typeof options.timeout != 'number' &&
typeof request.query.timeout == 'number')
body.timeout = request.query.timeout;
options.timeout = request.query.timeout;
body.type = body.type || 'droonga-' + messageType;
connection.emit(messageType, body);
connection.emit(messageType, body, options);
});
}
}),
Expand Down
23 changes: 20 additions & 3 deletions lib/adapter/wrapper.js
Expand Up @@ -23,12 +23,29 @@ DroongaProtocolConnectionWrapper.prototype = {
return this._routeToSelf ||
(this._routeToSelf = this._connection.getRouteToSelf(this._options));
},
emit: function(event, data, callback) {
emit: function(event, data, callback, options) {
if (this._connection.closed) {
this._logger.warn('connection is already closed.');
return;
}

// support emitMessage(type, body, options)
if (callback && typeof callback != 'function') {
options = callback;
callback = null;
}

var unifiedOptions = this._options;
if (options && typeof options == 'object') {
unifiedOptions = {};
Object.keys(this._options).forEach(key) {
unifiedOptions[key] = this._options[key];
}, this);
Object.keys(options).forEach(key) {
options[key] = this._options[key];
}, this);
}

if (callback) {
var originalCallback = callback;
callback = function(error, response) {
Expand All @@ -38,9 +55,9 @@ DroongaProtocolConnectionWrapper.prototype = {
callback = this._callback;
}
if (callback)
this._connection.emitMessage(event, data, callback, this._options);
this._connection.emitMessage(event, data, callback, unifiedOptions);
else
this._connection.emitMessage(event, data, this._options);
this._connection.emitMessage(event, data, unifiedOptions);
},
on: function(event, listener) {
this._connection.on(event, listener);
Expand Down
12 changes: 8 additions & 4 deletions lib/droonga-protocol/connection.js
Expand Up @@ -236,10 +236,14 @@ Connection.prototype.emitMessage = function(type, body, callback, options) {
type: type,
body: body
};
if (typeof options.timeout == 'number')
envelope.timeout = options.timeout;
else if (typeof this.defaultTimeout == 'number')
envelope.timeout = this.defaultTimeout;
if (typeof envelope.timeout != 'number') {
if (typeof options.timeout == 'number')
envelope.timeout = options.timeout;
else if (typeof this.defaultTimeout == 'number')
envelope.timeout = this.defaultTimeout;
}
if (typeof envelope.timeout == 'number')
envelope.timeout = toFloat(envelope.timeout);

this._logger.trace('emitMessage: trying to send message: ',
envelope);
Expand Down

0 comments on commit 7a3670d

Please sign in to comment.