allows instances to be removed from pool
This commit is contained in:
parent
5825212e7f
commit
c08946cc24
|
@ -25,6 +25,7 @@ class ControlServer {
|
|||
socket.on('createInstances', (instances, callback) => { this.torPool.create(instances, (error, instances) => {
|
||||
callback(error)
|
||||
}); });
|
||||
socket.on('removeInstances', (instances, callback) => { this.torPool.remove(instances, callback); });
|
||||
socket.on('newIps', () => { this.torPool.new_ips(); });
|
||||
socket.on('nextInstance', () => { this.torPool.next(); });
|
||||
socket.on('closeInstances', () => { this.torPool.exit(); });
|
||||
|
|
|
@ -60,6 +60,13 @@ class TorPool extends EventEmitter {
|
|||
}, (callback || (() => {})));
|
||||
}
|
||||
|
||||
remove(instances, callback) {
|
||||
let instances_to_remove = this._instances.splice(0, instances);
|
||||
async.each(instances_to_remove, (instance, next) => {
|
||||
instance.exit(next);
|
||||
}, callback);
|
||||
}
|
||||
|
||||
next() {
|
||||
this._instances = this._instances.rotate(1);
|
||||
return this.instances[0];
|
||||
|
|
|
@ -22,8 +22,12 @@ class TorProcess extends EventEmitter {
|
|||
}, (config || { }));
|
||||
}
|
||||
|
||||
exit() {
|
||||
exit(callback) {
|
||||
this.process.once('exit', (code) => {
|
||||
callback && callback(null, code);
|
||||
});
|
||||
this.process.kill('SIGKILL');
|
||||
|
||||
}
|
||||
|
||||
new_ip() {
|
||||
|
|
16
test/test.js
16
test/test.js
|
@ -184,8 +184,8 @@ describe('TorProcess', function () {
|
|||
describe('TorPool', function () {
|
||||
var TorPool = TorRouter.TorPool;
|
||||
var pool = new TorPool('tor');
|
||||
this.timeout(Infinity);
|
||||
describe('#create', function () {
|
||||
this.timeout(Infinity);
|
||||
it('should create two instances without any problems', function (done) {
|
||||
pool.create(2, function (error) {
|
||||
if (error) return done(error);
|
||||
|
@ -193,9 +193,19 @@ describe('TorPool', function () {
|
|||
});
|
||||
});
|
||||
|
||||
after(function () {
|
||||
pool.exit();
|
||||
});
|
||||
|
||||
describe('#remove', function () {
|
||||
it('should remove 2 instances from the pool', function (done) {
|
||||
pool.remove(2, function (error) {
|
||||
if (error) return done(error);
|
||||
done((pool.instances.length) && new Error('pool has two instances'));
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
after(function () {
|
||||
pool.exit();
|
||||
});
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue