2 new revisions:

Revision: 266b18e0f6dc
Author:   Tomaz Muraus <to...@tomaz.me>
Date:     Tue Nov 29 04:11:51 2011
Log: Set a timeout for each step which is performed when a Connection to Ca...
http://code.google.com/a/apache-extras.org/p/cassandra-node/source/detail?r=266b18e0f6dc

Revision: 7cd24791bb99
Author:   Tomaz Muraus <to...@tomaz.me>
Date:     Tue Nov 29 04:24:56 2011
Log:      Add a test case for it.
http://code.google.com/a/apache-extras.org/p/cassandra-node/source/detail?r=7cd24791bb99

==============================================================================
Revision: 266b18e0f6dc
Author:   Tomaz Muraus <to...@tomaz.me>
Date:     Tue Nov 29 04:11:51 2011
Log: Set a timeout for each step which is performed when a Connection to Cassandra has been successfully established.
http://code.google.com/a/apache-extras.org/p/cassandra-node/source/detail?r=266b18e0f6dc

Added:
 /lib/util.js
Modified:
 /lib/driver.js

=======================================
--- /dev/null
+++ /lib/util.js        Tue Nov 29 04:11:51 2011
@@ -0,0 +1,15 @@
+/**
+ * Wrap a function so that the original function will only be called once,
+ * regardless of how  many times the wrapper is called.
+ * @param {Function} fn The to wrap.
+ * @return {Function} A function which will call fn the first time it is called.
+ */
+exports.fireOnce = function fireOnce(fn) {
+  var fired = false;
+  return function wrapped() {
+    if (!fired) {
+      fired = true;
+      fn.apply(null, arguments);
+    }
+  };
+};
=======================================
--- /lib/driver.js      Thu Nov 17 12:35:50 2011
+++ /lib/driver.js      Tue Nov 29 04:11:51 2011
@@ -30,6 +30,7 @@
 var async = require('async');
 var Cassandra = require('./gen-nodejs/Cassandra');
 var ttypes = require('./gen-nodejs/cassandra_types');
+var util = require('./util');

 var genericPool = require('generic-pool');

@@ -47,6 +48,14 @@

 var DEFAULT_CONNECTION_TIMEOUT = 4000;

+/** Default timeout for each of the steps (login, learn, use) which are performed
+* when the Connection to the Cassandra server has been established. */
+var DEFAULT_STEP_TIMEOUTS = {
+  'login': 1000,
+  'learn': 2000,
+  'use': 1000,
+};
+
 /** converts object to a string using toString() method if it exists. */
 function stringify(x) {
   if (x.toString) {
@@ -433,9 +442,20 @@
     };

     async.series([
-      login,
-      learn,
-      use
+      function loginStep(callback) {
+ var wrappedCallback = wrapStepCallback('login', DEFAULT_STEP_TIMEOUTS.login, callback);
+        login(wrappedCallback);
+      },
+
+      function learnStep(callback) {
+ var wrappedCallback = wrapStepCallback('learn', DEFAULT_STEP_TIMEOUTS.learn, callback);
+        learn(wrappedCallback);
+      },
+
+      function useStep(callback) {
+ var wrappedCallback = wrapStepCallback('use', DEFAULT_STEP_TIMEOUTS.use, callback);
+        use(wrappedCallback);
+      },
     ],

     function(err) {
@@ -447,6 +467,27 @@
     });
   });

+  function timeoutStep(name, timeout, callback) {
+    var timeoutId = setTimeout(function stepTimeoutHandler() {
+      var err = new Error('Step "' + name + '" timed out.');
+      err.errno = constants.ETIMEDOUT;
+
+      callback(err);
+    }, timeout);
+
+    return timeoutId;
+  }
+
+  function wrapStepCallback(name, timeout, callback) {
+    callback = util.fireOnce(callback);
+    var timeoutId = timeoutStep(name, timeout, callback);
+
+    return function wrappedCallback(err) {
+      clearTimeout(timeoutId);
+      callback(err);
+    };
+  }
+
   function connectTimeout() {
     var err = new Error('ETIMEDOUT, Operation timed out');
     err.errno = constants.ETIMEDOUT;
@@ -461,6 +502,8 @@

   // kicks off the connection process.
   this.client = thrift.createClient(Cassandra, this.con);
+
+  // set a connection timeout handler
   timeoutId = setTimeout(connectTimeout, this.timeout);
 };


==============================================================================
Revision: 7cd24791bb99
Author:   Tomaz Muraus <to...@tomaz.me>
Date:     Tue Nov 29 04:24:56 2011
Log:      Add a test case for it.
http://code.google.com/a/apache-extras.org/p/cassandra-node/source/detail?r=7cd24791bb99

Modified:
 /test/test_driver.js

=======================================
--- /test/test_driver.js        Thu Nov 17 12:23:47 2011
+++ /test/test_driver.js        Tue Nov 29 04:24:56 2011
@@ -19,6 +19,7 @@
 var assert = require('assert');
 var console = require('console');
 var EventEmitter = require('events').EventEmitter;
+var http = require('http');

 var logmagic = require('logmagic');
 var async = require('async');
@@ -771,7 +772,6 @@


 exports.testPooledConnectionFailover = function(test, assert) {
-  var server = null;
var hosts = ['google.com:8000', '127.0.0.1:6567', '127.0.0.2', '127.0.0.1:19170']; var conn = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1', use_bigints: true, 'timeout': 5000});

@@ -784,6 +784,33 @@
     }
   ],

+  function(err) {
+    conn.shutdown();
+    test.finish();
+  });
+};
+
+exports.testLearnStepTimeout = function(test, assert) {
+  var server = null;
+  var hosts = ['127.0.0.1:8688', '127.0.0.1:19170'];
+ var conn = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1', use_bigints: true, 'timeout': 5000});
+
+  async.series([
+    function startHttpServer(callback) {
+      server = http.createServer(function (req, res) {
+        res.end('test\n');
+      });
+      server.listen(8688, '127.0.0.1', callback);
+    },
+
+      function executeQueryPooledConnection(callback) {
+ conn.execute('UPDATE CfUgly SET A=1 WHERE KEY=1', [], function(err) {
+        assert.ifError(err);
+        callback();
+      });
+    }
+  ],
+
   function(err) {
     if (server) {
       server.close();

Reply via email to