diff --git a/lib/net/pool.js b/lib/net/pool.js index f6479859..c601e5ca 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -114,6 +114,7 @@ function Pool(options) { this.uid = 0; this.createServer = null; this.locker = new Locker(); + this.dnsLock = new Locker(); this.proxyServer = null; this.auth = null; this.identityKey = null; @@ -1539,16 +1540,40 @@ Pool.prototype.addOutbound = function addOutbound() { * @private */ -Pool.prototype.fillPeers = function fillPeers() { +Pool.prototype.fillPeers = co(function* fillPeers() { + var unlock = yield this.dnsLock.lock(); + try { + return yield this._fillPeers(); + } finally { + unlock(); + } +}); + +/** + * Attempt to refill the pool with peers (no lock). + * @private + */ + +Pool.prototype._fillPeers = co(function* fillPeers() { + var need = this.maxOutbound - this.peers.outbound; var i; + if (need <= 0) + return; + this.logger.debug('Refilling peers (%d/%d).', this.peers.outbound, this.maxOutbound); - for (i = 0; i < this.maxOutbound - 1; i++) + if (this.hosts.size() < need) { + this.logger.warning('Very few hosts available.'); + this.logger.warning('Hitting DNS seeds again.'); + yield this.hosts.discover(this.maxOutbound * 4); + } + + for (i = 0; i < need; i++) this.addOutbound(); -}; +}); /** * Remove a peer from any list. Drop all load requests. @@ -2316,15 +2341,18 @@ HostList.prototype.addSeed = function addSeed(hostname) { * @returns {Promise} */ -HostList.prototype.discover = co(function* discover() { +HostList.prototype.discover = co(function* discover(total) { var i, seed; + if (!total) + total = 16; + for (i = 0; i < this.seeds.length; i++) { seed = this.seeds[i]; yield this.populate(seed); - if (this.list.size >= this.maxOutbound * 2) + if (this.list.size >= total) break; } });