Merge pull request #14 from jogli5er/upstream_master
Pool getPort race condition
This commit is contained in:
commit
37a58f929a
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
node_modules
|
node_modules
|
||||||
npm-debug.log
|
*.log
|
||||||
.env
|
.env
|
||||||
.vscode
|
.vscode
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
|
1593
package-lock.json
generated
1593
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -31,6 +31,8 @@
|
||||||
"socks-proxy-agent": "^4.0.1"
|
"socks-proxy-agent": "^4.0.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"arrify": "^2.0.1",
|
||||||
|
"async-mutex": "^0.1.4",
|
||||||
"bluebird": "^3.5.2",
|
"bluebird": "^3.5.2",
|
||||||
"del": "^3.0.0",
|
"del": "^3.0.0",
|
||||||
"eventemitter3": "^3.1.0",
|
"eventemitter3": "^3.1.0",
|
||||||
|
|
|
@ -27,7 +27,9 @@ const Promise = require("bluebird");
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const WeightedList = require('js-weighted-list');
|
const WeightedList = require('js-weighted-list');
|
||||||
|
|
||||||
|
const getPort = require('get-port');
|
||||||
const TorProcess = require('./TorProcess');
|
const TorProcess = require('./TorProcess');
|
||||||
|
const { Mutex } = require('async-mutex');
|
||||||
|
|
||||||
Promise.promisifyAll(fs);
|
Promise.promisifyAll(fs);
|
||||||
|
|
||||||
|
@ -462,6 +464,19 @@ class TorPool extends EventEmitter {
|
||||||
|
|
||||||
if (typeof(instances) === 'number') {
|
if (typeof(instances) === 'number') {
|
||||||
instances = Array.from(Array(instances)).map(() => ({}));
|
instances = Array.from(Array(instances)).map(() => ({}));
|
||||||
|
const lock = new Mutex();
|
||||||
|
instances = await Promise.all(instances.map(async(instance) => {
|
||||||
|
instance.ports = {};
|
||||||
|
let release = await lock.acquire();
|
||||||
|
try {
|
||||||
|
instance.ports.dns_port = await getPort();
|
||||||
|
instance.ports.socks_port = await getPort();
|
||||||
|
instance.ports.control_port = await getPort();
|
||||||
|
} finally {
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
return await this.add(instances);
|
return await this.add(instances);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,7 @@ class TorProcess extends EventEmitter {
|
||||||
definition.Config = definition.Config || {};
|
definition.Config = definition.Config || {};
|
||||||
|
|
||||||
this._definition = definition;
|
this._definition = definition;
|
||||||
|
this._ports = definition.ports || {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to the Tor executable.
|
* Path to the Tor executable.
|
||||||
|
@ -263,10 +264,12 @@ class TorProcess extends EventEmitter {
|
||||||
* @returns {Promise<ChildProcess>} - The process that has been created.
|
* @returns {Promise<ChildProcess>} - The process that has been created.
|
||||||
*/
|
*/
|
||||||
async create() {
|
async create() {
|
||||||
this._ports = {};
|
let dnsPort = this._ports.dns_port || await getPort();
|
||||||
let dnsPort = this._ports.dns_port = await getPort();
|
let socksPort = this._ports.socks_port || await getPort();
|
||||||
let socksPort = this._ports.socks_port = await getPort();
|
let controlPort = this._ports.control_port || await getPort();
|
||||||
let controlPort = this._ports.control_port = await getPort();
|
this.logger.info(`[tor-${this.instance_name}]: DNS PORT = ${dnsPort}`);
|
||||||
|
this.logger.info(`[tor-${this.instance_name}]: SOCKS PORT = ${socksPort}`);
|
||||||
|
this.logger.info(`[tor-${this.instance_name}]: CONTROL PORT = ${controlPort}`);
|
||||||
Object.freeze(this._ports);
|
Object.freeze(this._ports);
|
||||||
|
|
||||||
let options = {
|
let options = {
|
||||||
|
@ -403,7 +406,7 @@ class TorProcess extends EventEmitter {
|
||||||
* @returns {Error}
|
* @returns {Error}
|
||||||
*/
|
*/
|
||||||
this.emit('error', new Error(msg));
|
this.emit('error', new Error(msg));
|
||||||
this.logger.error(`[tor-${this.instance_name}]: ${msg}`);
|
this.logger.error(`[tor-${this.instance_name}]: ${text}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (text.indexOf('[notice]') !== -1) {
|
else if (text.indexOf('[notice]') !== -1) {
|
||||||
|
|
Loading…
Reference in a new issue