Kouhei Sutou
kou****@clear*****
Fri Oct 17 17:35:16 JST 2014
ConnectionPoolとかどうですかねぇ。 スレッドプールみたいな感じで。 Connectionsだと単なるConnectionのコレクションなイメージがす るんですけど、再接続とかそういうコネクションの管理もすると思 うので、もう少し賢い感じがする名前がいいなぁと思いました。 In <c61258b601b78f5daa828ce0780ec45cce89c8e9 �� jenkins.clear-code.com> "[Groonga-commit] droonga/express-droonga �� c61258b [master] Add module to support multiple connections" on Fri, 17 Oct 2014 16:34:20 +0900, YUKI Hiroshi <null+groonga �� clear-code.com> wrote: > YUKI Hiroshi 2014-10-17 16:34:20 +0900 (Fri, 17 Oct 2014) > > New Revision: c61258b601b78f5daa828ce0780ec45cce89c8e9 > https://github.com/droonga/express-droonga/commit/c61258b601b78f5daa828ce0780ec45cce89c8e9 > > Message: > Add module to support multiple connections > > Added files: > lib/droonga-protocol/connections.js > > Added: lib/droonga-protocol/connections.js (+62 -0) 100755 > =================================================================== > --- /dev/null > +++ lib/droonga-protocol/connections.js 2014-10-17 16:34:20 +0900 (11badd4) > @@ -0,0 +1,62 @@ > +/** > + * var connections = new Connections({ tag: 'droonga', > + * defaultDataset: 'example', > + * hostNames: ['127.0.0.1', ...], > + * port: 24224, > + * receiveHostName: '127.0.0.1', > + * receivePort: 10030 }); > + */ > + > +var Connection = require('./connection').Connection; > +var ConsoleLogger = require('../console-logger').ConsoleLogger; > + > +function Connections(params) { > + this._params = params || {}; > + > + if (!this._params.logger) > + this._params.logger = new ConsoleLogger(); > + > + var hostNames = this._params.hostNames || > + this._params.hostName || > + Connection.DEFAULT_FLUENT_HOST_NAME; > + if (!Array.isArray(hostNames)) > + this._params.hostNames = [hostNames]; > + > + if (this._params.hostNames.length == 0) > + throw new Error('Connections: you must give one or more host name(s)!'); > + > + this._connections = {}; > + this._instantiate(); > +} > + > +Connections.prototype = { > + _instantiate: function() { > + this._params.hostNames.forEach(function(hostName) { > + if (this._connections[hostName]) > + return; > + > + this._connections[hostName] = new Connection({ > + tag: this._params.tag > + defaultDataset: this._params.defaultDataset > + hostName: hostName > + port: this._params.port > + receiveHostName: this._params.receiveHostName > + receivePort: this._params.receivePort > + }); > + }, this); > + > + this._nextIndex = 0; > + }, > + > + get: function() { > + var hostName = this._params.hostNames[this._nextIndex]; > + > + this._nextIndex++; > + if (this._nextIndex == this._params.hostNames.length) > + this._nextIndex = 0; > + > + return this._connections[hostName]; > + } > +}; > + > +exports.Connections = Connections;