YUKI Hiroshi
null+****@clear*****
Wed Oct 22 16:28:58 JST 2014
YUKI Hiroshi 2014-10-22 16:28:58 +0900 (Wed, 22 Oct 2014) New Revision: ba35df9e2272afac3212eb295d58167e1aca6d75 https://github.com/droonga/express-droonga/commit/ba35df9e2272afac3212eb295d58167e1aca6d75 Message: Support dynamic connection Modified files: lib/droonga-protocol/connection-pool.js Modified: lib/droonga-protocol/connection-pool.js (+58 -39) =================================================================== --- lib/droonga-protocol/connection-pool.js 2014-10-22 15:57:56 +0900 (706093e) +++ lib/droonga-protocol/connection-pool.js 2014-10-22 16:28:58 +0900 (39c3445) @@ -18,48 +18,48 @@ function ConnectionPool(params) { if (!this._params.logger) this._params.logger = new ConsoleLogger(); - var hostNames = this._params.hostNames || + this._hostNames = []; + this._connections = {}; + + this.hostNames = this._params.hostNames || this._params.hostName || Connection.DEFAULT_FLUENT_HOST_NAME; - if (!Array.isArray(hostNames)) - hostNames = [hostNames]; - - var uniqueHostNames = {}; - hostNames.forEach(function(hostName) { - if (hostName in uniqueHostNames) { - return false; - } - else { - uniqueHostNames[hostName] = true; - return true; - } - }); - this.hostNames = this._params.hostNames = Object.keys(uniqueHostNames); - - if (this.hostNames.length == 0) - throw new Error('ConnectionPool: you must give one or more host name(s)!'); - - this._connections = {}; - this._instantiate(); } ConnectionPool.prototype = { - _instantiate: function() { - this.hostNames.forEach(function(hostName) { - if (this._connections[hostName]) + get hostNames() { + return this._hostNames; + }, + set hostNames(hostNames) { + if (!Array.isArray(hostNames)) + hostNames = [hostNames]; + + var uniqueHostNames = {}; + hostNames.forEach(function(hostName) { + if (hostName in uniqueHostNames) { + return false; + } + else { + uniqueHostNames[hostName] = true; + return true; + } + }); + var newHostNames = Object.keys(uniqueHostNames); + + this._hostNames.forEach(function(hostName) { + if (newHostNames.indexOf(hostName) > -1) 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.closeFor(hostName); }, this); - this.nextIndex = Math.floor(Math.random() * this.hostNames.length); + this._hostNames = newHostNames; + this.nextIndex = Math.floor(Math.random() * this._hostNames.length); + + if (this._hostNames.length == 0) + throw new Error('ConnectionPool: you must give one or more host name(s)!'); + + return this._hostNames; }, get: function() { @@ -69,18 +69,37 @@ ConnectionPool.prototype = { if (this.nextIndex == this.hostNames.length) this.nextIndex = 0; - return this._connections[hostName]; + return this.getConnectionFor(hostName); + }, + + getConnectionFor: function(hostName) { + if (this._connections[hostName]) + return 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 + }); }, closeAll: function() { - this.hostNames.forEach(function(hostName) { - var connection = this._connections[hostName]; - connection.close(); - }, this); + this._hostNames.forEach(this.closeFor, this); + }, + + closeFor: function(hostName) { + var connection = this._connections[hostName]; + if (!connection) + return; + connection.close(); + delete this._connections[hostName]; }, get count() { - return this.hostNames.length; + return this._hostNames.length; } }; -------------- next part -------------- HTML����������������������������...Download