[Groonga-commit] groonga/express-kotoumi [master] Implement "timeout" feature

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Jan 23 17:01:29 JST 2013


YUKI Hiroshi	2013-01-23 17:01:29 +0900 (Wed, 23 Jan 2013)

  New Revision: b317bedead87179dcaefaa662b8a2cb576dfdb55
  https://github.com/groonga/express-kotoumi/commit/b317bedead87179dcaefaa662b8a2cb576dfdb55

  Log:
    Implement "timeout" feature

  Modified files:
    lib/backend-adaptor.js
    lib/rest-adaptor.js
    test/backend-adaptor.test.js
    test/test-utils.js

  Modified: lib/backend-adaptor.js (+21 -3)
===================================================================
--- lib/backend-adaptor.js    2013-01-23 16:04:00 +0900 (c5757da)
+++ lib/backend-adaptor.js    2013-01-23 17:01:29 +0900 (4422990)
@@ -38,6 +38,8 @@ var DEFAULT_FLUENT_PORT       = 24224;
 var DEFAULT_RECEIVE_HOST_NAME = 'localhost';
 var DEFAULT_RECEIVE_PORT      = 10030;
 
+var ERROR_GATEWAY_TIMEOUT = exports.ERROR_GATEWAY_TIMEOUT = 504;
+
 function Connection(params) {
   this._params = params || {};
   this._init();
@@ -86,12 +88,19 @@ Connection.prototype._initReceiver = function() {
   this._receiver.set('log level', 1);
 };
 
+function isSuccess(code) {
+  return Math.float(code / 100) == 2;
+}
+
 Connection.prototype._handleMessage = function(envelope) {
   this.emit('message', envelope);
 
   var inReplyTo = envelope['inReplyTo'];
   if (inReplyTo) {
-    this.emit('inReplyTo:' + inReplyTo, envelope);
+    var errorCode = envelope.statusCode;
+    if (!errorCode || isSuccess(errorCode))
+      errorCode = null;
+    this.emit('inReplyTo:' + inReplyTo, errorCode, envelope);
   }
 };
 
@@ -107,7 +116,7 @@ function getCurrentTime() {
   return new Date().toISOString();
 }
 
-Connection.prototype.emitMessage = function(type, body, callback) {
+Connection.prototype.emitMessage = function(type, body, callback, timeout) {
   var id = createId();
   var envelope = {
     id:         id,
@@ -118,7 +127,16 @@ Connection.prototype.emitMessage = function(type, body, callback) {
     body:       body
   };
   if (callback) {
-    this.once('inReplyTo:' + id, callback);
+    var event = 'inReplyTo:' + id;
+    this.once(event, callback);
+    if (timeout) {
+      setTimeout((function() {
+        if (this.listeners(event)) {
+          this.removeAllListeners(event);
+          callback(ERROR_GATEWAY_TIMEOUT, null);
+        }
+      }).bind(this), timeout);
+    }
   }
   this._sender.emit('message', envelope);
   return envelope;

  Modified: lib/rest-adaptor.js (+10 -4)
===================================================================
--- lib/rest-adaptor.js    2013-01-23 16:04:00 +0900 (aef68b8)
+++ lib/rest-adaptor.js    2013-01-23 17:01:29 +0900 (604677a)
@@ -8,10 +8,16 @@ function createHandler(type,
 
   return (function(request, response) {
     var message = requestBuilder(request);
-    connection.emitMessage(type, message, function(responseMessage) {
-      var body = responseMessage.body;
-      response.contentType('application/json');
-      response.send(body, 200);
+    connection.emitMessage(type, message, function(error, responseMessage) {
+      if (error) {
+        var body = responseMessage && responseMessage.body || null;
+        response.contentType('application/json');
+        response.send(body, error);
+      } else {
+        var body = responseMessage.body;
+        response.contentType('application/json');
+        response.send(body, 200);
+      }
     });
   });
 }

  Modified: test/backend-adaptor.test.js (+3 -3)
===================================================================
--- test/backend-adaptor.test.js    2013-01-23 16:04:00 +0900 (dbdd628)
+++ test/backend-adaptor.test.js    2013-01-23 17:01:29 +0900 (9d975db)
@@ -52,12 +52,12 @@ suite('Connection', function() {
       type:       'testResponse',
       body:       'first call'
     };
-    callback.takes(message);
+    callback.takes(null, message);
     receiver.emitMessage(message);
     callback.assert();
 
     message.body = 'second call';
-    callback.takes(message);
+    callback.takes(null, message);
     receiver.emitMessage(message);
     callback.assert();
 
@@ -88,7 +88,7 @@ suite('Connection', function() {
       type:       'testResponse',
       body:       'first call'
     };
-    callback.takes(response);
+    callback.takes(null, response);
     receiver.emitMessage(response);
     callback.assert();
 

  Modified: test/test-utils.js (+4 -4)
===================================================================
--- test/test-utils.js    2013-01-23 16:04:00 +0900 (3c4812c)
+++ test/test-utils.js    2013-01-23 17:01:29 +0900 (28fafe6)
@@ -54,16 +54,16 @@ exports.createMockedReceiver = createMockedReceiver;
 
 function createMockedMessageCallback() {
   var mockedCallback = nodemock;
-  var callback = function(message) {
-    mockedCallback.receive(message);
+  var callback = function(error, message) {
+    mockedCallback.receive(error, message);
   };
-  callback.takes = function(message) {
+  callback.takes = function(error, message) {
     callback.assert = function() {
       mockedCallback.assertThrows();
     };
     mockedCallback = mockedCallback
                        .mock('receive')
-                       .takes(message);
+                       .takes(error, message);
   };
   callback.mock = mockedCallback;
   return callback;
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index