mirror of
https://github.com/jambonz/sbc-sip-sidecar.git
synced 2025-12-19 04:27:46 +00:00
create regbog in batch with delay, filter inique gateway before creating regbot (#95)
* create regbog in batch with delay, filter inique gateway before creating regbot * update review comment * do not register outbound if no sip password was provided --------- Co-authored-by: Dave Horton <daveh@beachdognet.com>
This commit is contained in:
@@ -30,7 +30,8 @@ const JAMBONES_CLUSTER_ID = process.env.JAMBONES_CLUSTER_ID ;
|
||||
const JAMBONES_REGBOT_CONTACT_USE_IP = process.env.JAMBONES_REGBOT_CONTACT_USE_IP;
|
||||
const JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL = process.env.JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL;
|
||||
const JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL = process.env.JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL;
|
||||
const JAMBONES_REGBOT_INITIAL_DELAY_MS = process.env.JAMBONES_REGBOT_INITIAL_DELAY_MS || 250;
|
||||
const JAMBONES_REGBOT_BATCH_SLEEP_MS = process.env.JAMBONES_REGBOT_BATCH_SLEEP_MS || 250;
|
||||
const JAMBONES_REGBOT_BATCH_SIZE = process.env.JAMBONES_REGBOT_BATCH_SIZE || 10;
|
||||
const OPTIONS_RESPONSE_REMOVE = process.env.OPTIONS_RESPONSE_REMOVE?.split(',').map(Number) || [];
|
||||
const REGISTER_RESPONSE_REMOVE = process.env.REGISTER_RESPONSE_REMOVE?.split(',').map(Number) || [];
|
||||
|
||||
@@ -59,7 +60,8 @@ module.exports = {
|
||||
JAMBONES_REGBOT_CONTACT_USE_IP,
|
||||
JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL,
|
||||
JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL,
|
||||
JAMBONES_REGBOT_INITIAL_DELAY_MS,
|
||||
JAMBONES_REGBOT_BATCH_SLEEP_MS,
|
||||
JAMBONES_REGBOT_BATCH_SIZE,
|
||||
OPTIONS_RESPONSE_REMOVE,
|
||||
REGISTER_RESPONSE_REMOVE
|
||||
};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const debug = require('debug')('jambonz:sbc-registrar');
|
||||
const {
|
||||
JAMBONES_CLUSTER_ID,
|
||||
JAMBONES_REGBOT_INITIAL_DELAY_MS,
|
||||
JAMBONES_REGBOT_BATCH_SLEEP_MS,
|
||||
JAMBONES_REGBOT_BATCH_SIZE,
|
||||
} = require('./config');
|
||||
const short = require('short-uuid');
|
||||
const Regbot = require('./regbot');
|
||||
@@ -50,6 +51,34 @@ async function getLocalSIPDomain(logger, srf) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters gateway array to remove duplicates based on ipv4, sip_realm, username and password
|
||||
* @param {Array} gateways - Array of gateway objects
|
||||
* @param {Object} logger - Logger instance to log duplicate entries
|
||||
* @returns {Array} - Filtered array with unique gateways
|
||||
*/
|
||||
function getUniqueGateways(gateways, logger) {
|
||||
const uniqueGatewayKeys = new Set();
|
||||
|
||||
return gateways.filter((gw) => {
|
||||
const key = `${gw.ipv4}:${gw.sip_realm}:${gw.username}:${gw.password}`;
|
||||
if (!gw.password) {
|
||||
logger.info({gw}, `Gateway ${key} does not have a password, ignoring`);
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we've already seen this key, it's a duplicate
|
||||
if (uniqueGatewayKeys.has(key)) {
|
||||
logger.info({gw}, `Found duplicate gateway ${key}, ignoring`);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise, add it to our Set and keep this gateway
|
||||
uniqueGatewayKeys.add(key);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = async(logger, srf) => {
|
||||
if (initialized) return;
|
||||
initialized = true;
|
||||
@@ -193,7 +222,8 @@ const updateCarrierRegbots = async(logger, srf) => {
|
||||
regbots.length = 0;
|
||||
|
||||
// start new regbots
|
||||
for (const gw of gateways) {
|
||||
let batch_count = 0;
|
||||
for (const gw of getUniqueGateways(gateways)) {
|
||||
try {
|
||||
const rb = new Regbot(logger, {
|
||||
voip_carrier_sid: gw.carrier.voip_carrier_sid,
|
||||
@@ -209,9 +239,12 @@ const updateCarrierRegbots = async(logger, srf) => {
|
||||
use_public_ip_in_contact: gw.carrier.register_public_ip_in_contact,
|
||||
});
|
||||
regbots.push(rb);
|
||||
// stagger the start of the regbots
|
||||
await sleepFor(JAMBONES_REGBOT_INITIAL_DELAY_MS);
|
||||
rb.start(srf);
|
||||
batch_count++;
|
||||
if (batch_count >= JAMBONES_REGBOT_BATCH_SIZE) {
|
||||
batch_count = 0;
|
||||
await sleepFor(JAMBONES_REGBOT_BATCH_SLEEP_MS);
|
||||
}
|
||||
} catch (err) {
|
||||
const { updateVoipCarriersRegisterStatus } = srf.locals.dbHelpers;
|
||||
updateVoipCarriersRegisterStatus(gw.carrier.voip_carrier_sid, JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user