Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pool test and initial min queue linear search

  • Loading branch information...
commit f9635345089e87cd14218134fa5172307606dd6c 1 parent 2d2c4d7
@sidorares sidorares authored
Showing with 48 additions and 4 deletions.
  1. +48 −4 mysql/pool.js
View
52 mysql/pool.js
@@ -1,4 +1,5 @@
var queue = require('./containers').queue;
+var sys= require('sys');
function pool(newConnectionFactory)
{
@@ -11,11 +12,11 @@ function pool(newConnectionFactory)
this.maxQueue = 2; // increase if average command time is much shorter compared to connect time
// TODO: calculate ratio on the fly? think of adaptiveMaxQueue
this.idleTimeout = 0; // TODO: also possible to make adaptive
- this.maxWaiters = -1; // TODO: infinity?
+ this.maxWaiters = 100000000; // TODO: infinity?
this.waiters = new queue();
- for (var i=0; i < minConnections; ++i)
- spawnConnection();
+ for (var i=0; i < this.minConnections; ++i)
+ this.spawnConnection();
}
pool.prototype.spawnConnection = function()
@@ -24,15 +25,21 @@ pool.prototype.spawnConnection = function()
var self = this;
// todo: Qt-style connection-slot api?
client.connection.addListener('command_end', function() { self.queueChanged(client); });
+ this.connections.push(client);
+ return client;
}
pool.prototype.queueChanged = function(client)
{
var new_size = client.commands.length;
+
+ sys.puts("New queue:" + new_size);
+
if (!this.waiters.empty() && new_size <= this.maxQueue)
{
var w = this.waiters.shift();
- w(client);
+ if (w)
+ w(client);
}
// there is no commands left for current connection
@@ -57,4 +64,41 @@ pool.prototype.get = function(onClientReady)
// if connections size less than maxConnection, spawn a new connection
// else enqueue request
// throw error if waiters queue length > maxWaiters
+
+
+ // quick hack
+ // TODO: add search using index
+ var minQueueConnection = null;
+ var minQueue = 1000000000;
+ for (var i=0; i < this.connections.length; ++i)
+ {
+ var len = this.connections[i].commands.length;
+ if (len < minQueue)
+ {
+ minQueue = len;
+ minQueueConnection = this.connections[i];
+ }
+ }
+ if (minQueue < this.maxQueue)
+ {
+ onClientReady(minQueueConnection);
+ }
+ if (this.connections.length < this.maxConnections)
+ {
+ onClientReady(this.spawnConnection());
+ }
+ if (this.waiters.length < this.maxWaiters)
+ {
+ this.waiters.push(onClientReady);
+ }
+}
+
+pool.prototype.close = function()
+{
+ for (var i=0; i < this.connections.length; ++i)
+ {
+ this.connections[i].close();
+ }
}
+
+exports.pool = pool;
Please sign in to comment.
Something went wrong with that request. Please try again.