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

Commit 281b2f0

Browse files
author
Alan Shaw
committed
fix: running queues after stop
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 0fc53dd commit 281b2f0

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/dialer/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const {
1212
module.exports = function (_switch) {
1313
const dialQueueManager = new DialQueueManager(_switch)
1414

15-
_switch.state.on('STOPPING:enter', abort)
15+
_switch.state.on('STARTING:enter', start)
16+
_switch.state.on('STOPPING:enter', stop)
1617

1718
/**
1819
* @param {DialRequest} dialRequest
@@ -34,14 +35,24 @@ module.exports = function (_switch) {
3435
dialQueueManager.add({ peerInfo, protocol, useFSM, callback })
3536
}
3637

38+
/**
39+
* Signals to the dialer that it should start processing dial queues
40+
*
41+
* @param {function} callback
42+
*/
43+
function start (callback) {
44+
dialQueueManager.start()
45+
callback()
46+
}
47+
3748
/**
3849
* Aborts all dials that are queued. This should
3950
* only be used when the Switch is being stopped
4051
*
4152
* @param {function} callback
4253
*/
43-
function abort (callback) {
44-
dialQueueManager.abort()
54+
function stop (callback) {
55+
dialQueueManager.stop()
4556
callback()
4657
}
4758

@@ -77,7 +88,8 @@ module.exports = function (_switch) {
7788
return {
7889
dial,
7990
dialFSM,
80-
abort,
91+
start,
92+
stop,
8193
clearBlacklist,
8294
BLACK_LIST_ATTEMPTS: isNaN(_switch._options.blackListAttempts) ? BLACK_LIST_ATTEMPTS : _switch._options.blackListAttempts,
8395
BLACK_LIST_TTL: isNaN(_switch._options.blacklistTTL) ? BLACK_LIST_TTL : _switch._options.blacklistTTL,

src/dialer/queueManager.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const once = require('once')
44
const Queue = require('./queue')
5-
const { DIAL_ABORTED } = require('../errors')
5+
const { DIAL_ABORTED, DIAL_QUEUE_MANAGER_STOPPED } = require('../errors')
66
const nextTick = require('async/nextTick')
77
const retimer = require('retimer')
88
const { QUARTER_HOUR } = require('../constants')
@@ -20,6 +20,11 @@ class DialQueueManager {
2020
this._queues = {}
2121
this.switch = _switch
2222
this._cleanInterval = retimer(this._clean.bind(this), QUARTER_HOUR)
23+
this._isRunning = false
24+
}
25+
26+
start () {
27+
this._isRunning = true
2328
}
2429

2530
/**
@@ -70,7 +75,9 @@ class DialQueueManager {
7075
*
7176
* This causes the entire DialerQueue to be drained
7277
*/
73-
abort () {
78+
stop () {
79+
this._isRunning = false
80+
7481
// Clear the general queue
7582
this._queue.clear()
7683
// Clear the cold call queue
@@ -95,6 +102,10 @@ class DialQueueManager {
95102
add ({ peerInfo, protocol, useFSM, callback }) {
96103
callback = callback ? once(callback) : noop
97104

105+
if (!this._isRunning) {
106+
return callback(DIAL_QUEUE_MANAGER_STOPPED())
107+
}
108+
98109
// Add the dial to its respective queue
99110
const targetQueue = this.getQueue(peerInfo)
100111
// If we have too many cold calls, abort the dial immediately
@@ -138,6 +149,10 @@ class DialQueueManager {
138149
* Will execute up to `MAX_PARALLEL_DIALS` dials
139150
*/
140151
run () {
152+
if (!this._isRunning) {
153+
return
154+
}
155+
141156
if (this._dialingQueues.size < this.switch.dialer.MAX_PARALLEL_DIALS) {
142157
let nextQueue = { done: true }
143158
// Check the queue first and fall back to the cold call queue

src/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
NO_TRANSPORTS_REGISTERED: () => errCode('No transports registered, dial not possible', 'NO_TRANSPORTS_REGISTERED'),
1212
PROTECTOR_REQUIRED: () => errCode('No protector provided with private network enforced', 'PROTECTOR_REQUIRED'),
1313
UNEXPECTED_END: () => errCode('Unexpected end of input from reader.', 'UNEXPECTED_END'),
14+
DIAL_QUEUE_MANAGER_STOPPED: () => errCode('Dial queue manager is stopped', 'DIALER_QUEUE_MANAGER_STOPPED'),
1415
maybeUnexpectedEnd: (err) => {
1516
if (err === true) {
1617
return module.exports.UNEXPECTED_END()

0 commit comments

Comments
 (0)