Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
fix: running queues after stop
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw committed Apr 3, 2019
1 parent 805d1ad commit fae1f15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/dialer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

Expand Down Expand Up @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions src/dialer/queueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fae1f15

Please # to comment.