allows instances to be removed from pool

This commit is contained in:
Zachary Boyd 2017-03-24 20:11:06 -04:00
parent 5825212e7f
commit c08946cc24
4 changed files with 26 additions and 4 deletions

View file

@ -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(); });

View file

@ -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];

View file

@ -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() {

View file

@ -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();
});
})