Skip to content

Commit 6f9c629

Browse files
authored
add configurable backup for outbound reg failure (#128)
1 parent 9cacbdb commit 6f9c629

File tree

5 files changed

+65
-35
lines changed

5 files changed

+65
-35
lines changed

lib/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const JAMBONES_REGBOT_BATCH_SIZE = process.env.JAMBONES_REGBOT_BATCH_SIZE || 10;
4242
const OPTIONS_RESPONSE_REMOVE = process.env.OPTIONS_RESPONSE_REMOVE?.split(',').map(Number) || [];
4343
const REGISTER_RESPONSE_REMOVE = process.env.REGISTER_RESPONSE_REMOVE?.split(',').map(Number) || [];
4444
const JAMBONES_REGBOT_USER_AGENT = process.env.JAMBONES_REGBOT_USER_AGENT ;
45+
const JAMBONES_REGBOT_FAILURE_RETRY_INTERVAL = process.env.JAMBONES_REGBOT_FAILURE_RETRY_INTERVAL;
4546

4647
module.exports = {
4748
JAMBONES_MYSQL_HOST,
@@ -77,5 +78,6 @@ module.exports = {
7778
JAMBONES_REGBOT_BATCH_SIZE,
7879
OPTIONS_RESPONSE_REMOVE,
7980
REGISTER_RESPONSE_REMOVE,
80-
JAMBONES_REGBOT_USER_AGENT
81+
JAMBONES_REGBOT_USER_AGENT,
82+
JAMBONES_REGBOT_FAILURE_RETRY_INTERVAL
8183
};

lib/regbot.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const {
44
JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL,
55
JAMBONES_REGBOT_CONTACT_USE_IP,
66
REGISTER_RESPONSE_REMOVE,
7-
JAMBONES_REGBOT_USER_AGENT
7+
JAMBONES_REGBOT_USER_AGENT,
8+
JAMBONES_REGBOT_FAILURE_RETRY_INTERVAL
89
} = require('./config');
910
const {isValidDomainOrIP, isValidIPv4} = require('./utils');
1011
const DEFAULT_EXPIRES = (parseInt(JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL) || 3600);
1112
const MIN_EXPIRES = (parseInt(JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL) || 30);
13+
const FAILURE_RETRY_INTERVAL = (parseInt(JAMBONES_REGBOT_FAILURE_RETRY_INTERVAL) || 300);
1214
const assert = require('assert');
1315
const version = require('../package.json').version;
1416
const useragent = JAMBONES_REGBOT_USER_AGENT || `Jambonz ${version}`;
@@ -143,7 +145,7 @@ class Regbot {
143145
if (res.status !== 200) {
144146
this.status = 'fail';
145147
this.logger.info(`${this.aor}: got ${res.status} registering to ${this.ipv4}:${this.port}`);
146-
this.timer = setTimeout(this.register.bind(this, srf), 30 * 1000);
148+
this.timer = setTimeout(this.register.bind(this, srf), FAILURE_RETRY_INTERVAL * 1000);
147149
if (REGISTER_RESPONSE_REMOVE.includes(res.status)) {
148150
const { updateCarrierBySid, lookupCarrierBySid } = srf.locals.dbHelpers;
149151
await updateCarrierBySid(this.voip_carrier_sid, {requires_register: false});
@@ -245,7 +247,7 @@ class Regbot {
245247
});
246248
} catch (err) {
247249
this.logger.error({ err }, `${this.aor}: Error registering to ${this.ipv4}:${this.port}`);
248-
this.timer = setTimeout(this.register.bind(this, srf), 60 * 1000);
250+
this.timer = setTimeout(this.register.bind(this, srf), FAILURE_RETRY_INTERVAL * 1000);
249251
updateVoipCarriersRegisterStatus(this.voip_carrier_sid, JSON.stringify({
250252
status: 'fail',
251253
reason: err

lib/sip-trunk-register.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,30 @@ async function getLocalSIPDomain(logger, srf) {
6363
*/
6464
function getUniqueGateways(gateways, logger) {
6565
const uniqueGatewayKeys = new Set();
66+
const duplicateCounts = new Map();
67+
const uniqueGateways = [];
6668

67-
return gateways.filter((gw) => {
69+
for (const gw of gateways) {
6870
const key = `${gw.ipv4}:${gw.sip_realm}:${gw.carrier?.register_username}:${gw.carrier?.register_password}`;
6971
if (!gw.carrier?.register_password) {
7072
logger.info({gw}, `Gateway ${key} does not have a password, ignoring`);
71-
return false;
73+
continue;
7274
}
7375

74-
// If we've already seen this key, it's a duplicate
7576
if (uniqueGatewayKeys.has(key)) {
76-
logger.info({gw}, `Found duplicate gateway ${key}, ignoring`);
77-
return false;
77+
duplicateCounts.set(key, (duplicateCounts.get(key) || 1) + 1);
78+
} else {
79+
uniqueGatewayKeys.add(key);
80+
uniqueGateways.push(gw);
7881
}
82+
}
83+
84+
// Log summary for each duplicate gateway
85+
for (const [key, count] of duplicateCounts) {
86+
logger.info({key, count}, `Found ${count} duplicate gateways for ${key}, ignoring duplicates`);
87+
}
7988

80-
// Otherwise, add it to our Set and keep this gateway
81-
uniqueGatewayKeys.add(key);
82-
return true;
83-
});
89+
return uniqueGateways;
8490
}
8591

8692
module.exports = async(logger, srf) => {
@@ -222,7 +228,7 @@ const updateCarrierRegbots = async(logger, srf) => {
222228
if (hasChanged) {
223229

224230
debug('updateCarrierRegbots: got new or changed carriers');
225-
logger.info({gws}, 'updateCarrierRegbots: got new or changed carriers');
231+
logger.info({count: gws.length}, 'updateCarrierRegbots: got new or changed carriers');
226232

227233
// Clear and repopulate arrays in chunks to avoid argument limit
228234
carriers.length = 0;

package-lock.json

Lines changed: 40 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"debug": "^4.4.3",
3939
"drachtio-mw-registration-parser": "^0.1.2",
4040
"drachtio-mw-response-time": "^1.0.2",
41-
"drachtio-srf": "^5.0.5",
41+
"drachtio-srf": "^5.0.18",
4242
"pino": "^10.1.0",
4343
"short-uuid": "^4.2.2"
4444
},

0 commit comments

Comments
 (0)