Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

Connection Pooling mit node-mysql

Update (Februar 2013): node-mysql unterstützt inzwischen Pooling, siehe Dokumentation.

Beispiel mit dem integrierten Pool:

var pool = require('mysql').createPool(opts); pool.getConnection(function(err, conn) { conn.query('select 1+1', function(err, res) { conn.release(); }); }); 

Lösungen vor 2013:

Sie konnten node-pool oder mysql-pool nutzen oder einen eigenen einfachen Round-Robin-Pool schreiben:

function Pool(num_conns) { this.pool = []; for(var i=0; i < num_conns; ++i) this.pool.push(createConnection()); // your new Client + auth this.last = 0; } Pool.prototype.get = function() { var cli = this.pool[this.last]; this.last++; if (this.last == this.pool.length) // cyclic increment this.last = 0; return cli; } 

So lässt sich hoffentlich erreichen, dass alle Query-Callbacks innerhalb einer Sekunde ausgeführt werden:

var p = new Pool(16); for (var i=0; i < 10; ++i) { p.get().query('select sleep(1)', function() { console.log('ready'); } ); // server blocks for 1 second }