diff --git a/src/dialer/index.js b/src/dialer/index.js index 8464d8e..dc0e46f 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -12,7 +12,8 @@ const { module.exports = function (_switch) { const dialQueueManager = new DialQueueManager(_switch) - _switch.state.on('STOPPING:enter', abort) + _switch.state.on('STARTING:enter', start) + _switch.state.on('STOPPING:enter', stop) /** * @param {DialRequest} dialRequest @@ -34,14 +35,24 @@ module.exports = function (_switch) { dialQueueManager.add({ peerInfo, protocol, useFSM, callback }) } + /** + * Signals to the dialer that it should start processing dial queues + * + * @param {function} callback + */ + function start (callback) { + dialQueueManager.start() + callback() + } + /** * Aborts all dials that are queued. This should * only be used when the Switch is being stopped * * @param {function} callback */ - function abort (callback) { - dialQueueManager.abort() + function stop (callback) { + dialQueueManager.stop() callback() } @@ -77,7 +88,8 @@ module.exports = function (_switch) { return { dial, dialFSM, - abort, + start, + stop, clearBlacklist, BLACK_LIST_ATTEMPTS: isNaN(_switch._options.blackListAttempts) ? BLACK_LIST_ATTEMPTS : _switch._options.blackListAttempts, BLACK_LIST_TTL: isNaN(_switch._options.blacklistTTL) ? BLACK_LIST_TTL : _switch._options.blacklistTTL, diff --git a/src/dialer/queueManager.js b/src/dialer/queueManager.js index 2ca5283..fab3a70 100644 --- a/src/dialer/queueManager.js +++ b/src/dialer/queueManager.js @@ -2,7 +2,7 @@ const once = require('once') const Queue = require('./queue') -const { DIAL_ABORTED } = require('../errors') +const { DIAL_ABORTED, DIAL_QUEUE_MANAGER_STOPPED } = require('../errors') const nextTick = require('async/nextTick') const retimer = require('retimer') const { QUARTER_HOUR } = require('../constants') @@ -20,6 +20,11 @@ class DialQueueManager { this._queues = {} this.switch = _switch this._cleanInterval = retimer(this._clean.bind(this), QUARTER_HOUR) + this._isRunning = false + } + + start () { + this._isRunning = true } /** @@ -70,7 +75,9 @@ class DialQueueManager { * * This causes the entire DialerQueue to be drained */ - abort () { + stop () { + this._isRunning = false + // Clear the general queue this._queue.clear() // Clear the cold call queue @@ -95,6 +102,10 @@ class DialQueueManager { add ({ peerInfo, protocol, useFSM, callback }) { callback = callback ? once(callback) : noop + if (!this._isRunning) { + return callback(DIAL_QUEUE_MANAGER_STOPPED()) + } + // Add the dial to its respective queue const targetQueue = this.getQueue(peerInfo) // If we have too many cold calls, abort the dial immediately @@ -138,6 +149,10 @@ class DialQueueManager { * Will execute up to `MAX_PARALLEL_DIALS` dials */ run () { + if (!this._isRunning) { + return + } + if (this._dialingQueues.size < this.switch.dialer.MAX_PARALLEL_DIALS) { let nextQueue = { done: true } // Check the queue first and fall back to the cold call queue diff --git a/src/errors.js b/src/errors.js index 73e0cb9..1f19a0f 100644 --- a/src/errors.js +++ b/src/errors.js @@ -11,6 +11,7 @@ module.exports = { NO_TRANSPORTS_REGISTERED: () => errCode('No transports registered, dial not possible', 'NO_TRANSPORTS_REGISTERED'), PROTECTOR_REQUIRED: () => errCode('No protector provided with private network enforced', 'PROTECTOR_REQUIRED'), UNEXPECTED_END: () => errCode('Unexpected end of input from reader.', 'UNEXPECTED_END'), + DIAL_QUEUE_MANAGER_STOPPED: () => errCode('Dial queue manager is stopped', 'DIALER_QUEUE_MANAGER_STOPPED'), maybeUnexpectedEnd: (err) => { if (err === true) { return module.exports.UNEXPECTED_END()