mirror of
https://github.com/jambonz/sbc-outbound.git
synced 2026-10-04 02:04:19 +00:00
Feature/sp limits (#51)
* add account and service provider call limits * add custom headers when rejecting calls due to max calls limit
This commit is contained in:
@@ -39,7 +39,8 @@ const {
|
|||||||
lookupAccountBySid,
|
lookupAccountBySid,
|
||||||
lookupAccountCapacitiesBySid,
|
lookupAccountCapacitiesBySid,
|
||||||
lookupSipGatewaysByCarrier,
|
lookupSipGatewaysByCarrier,
|
||||||
lookupCarrierBySid
|
lookupCarrierBySid,
|
||||||
|
queryCallLimits
|
||||||
} = require('@jambonz/db-helpers')({
|
} = require('@jambonz/db-helpers')({
|
||||||
host: process.env.JAMBONES_MYSQL_HOST,
|
host: process.env.JAMBONES_MYSQL_HOST,
|
||||||
user: process.env.JAMBONES_MYSQL_USER,
|
user: process.env.JAMBONES_MYSQL_USER,
|
||||||
@@ -77,7 +78,8 @@ srf.locals = {...srf.locals,
|
|||||||
lookupAccountBySid,
|
lookupAccountBySid,
|
||||||
lookupAccountCapacitiesBySid,
|
lookupAccountCapacitiesBySid,
|
||||||
lookupSipGatewaysByCarrier,
|
lookupSipGatewaysByCarrier,
|
||||||
lookupCarrierBySid
|
lookupCarrierBySid,
|
||||||
|
queryCallLimits
|
||||||
},
|
},
|
||||||
realtimeDbHelpers: {
|
realtimeDbHelpers: {
|
||||||
createHash,
|
createHash,
|
||||||
|
|||||||
+36
-4
@@ -10,7 +10,8 @@ module.exports = (srf, logger, opts) => {
|
|||||||
const registrar = new Registrar(opts);
|
const registrar = new Registrar(opts);
|
||||||
const {
|
const {
|
||||||
lookupAccountCapacitiesBySid,
|
lookupAccountCapacitiesBySid,
|
||||||
lookupAccountBySid
|
lookupAccountBySid,
|
||||||
|
queryCallLimits
|
||||||
} = srf.locals.dbHelpers;
|
} = srf.locals.dbHelpers;
|
||||||
|
|
||||||
const initLocals = async(req, res, next) => {
|
const initLocals = async(req, res, next) => {
|
||||||
@@ -132,9 +133,40 @@ module.exports = (srf, logger, opts) => {
|
|||||||
return req.srf.endSession(req);
|
return req.srf.endSession(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (process.env.JAMBONES_TRACK_ACCOUNT_CALLS) {
|
||||||
//TODO: check SP limits
|
const {account_limit, sp_limit} = await queryCallLimits(service_provider_sid, account_sid);
|
||||||
|
if (account_limit > 0 && calls > account_limit) {
|
||||||
|
logger.info({calls, account_limit}, 'checkLimits: account limits exceeded');
|
||||||
|
writeAlerts({
|
||||||
|
alert_type: AlertType.ACCOUNT_CALL_LIMIT,
|
||||||
|
service_provider_sid: service_provider_sid,
|
||||||
|
account_sid,
|
||||||
|
count: calls
|
||||||
|
}).catch((err) => logger.info({err}, 'checkLimits: error writing alert'));
|
||||||
|
res.send(503, 'Max Account Calls In Progress', {
|
||||||
|
headers: {
|
||||||
|
'X-Account-Sid': account_sid,
|
||||||
|
'X-Call-Limit': account_limit
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return req.srf.endSession(req);
|
||||||
|
}
|
||||||
|
if (sp_limit > 0 && callsSP > sp_limit) {
|
||||||
|
logger.info({callsSP, sp_limit}, 'checkLimits: service provider limits exceeded');
|
||||||
|
writeAlerts({
|
||||||
|
alert_type: AlertType.SP_CALL_LIMIT,
|
||||||
|
service_provider_sid: service_provider_sid,
|
||||||
|
count: callsSP
|
||||||
|
}).catch((err) => logger.info({err}, 'checkLimits: error writing alert'));
|
||||||
|
res.send(503, 'Max Service Provider Calls In Progress', {
|
||||||
|
headers: {
|
||||||
|
'X-Service-Provider-Sid': service_provider_sid,
|
||||||
|
'X-Call-Limit': sp_limit
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return req.srf.endSession(req);
|
||||||
|
}
|
||||||
|
}
|
||||||
next();
|
next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error({err}, 'error checking limits error for inbound call');
|
logger.error({err}, 'error checking limits error for inbound call');
|
||||||
|
|||||||
Generated
+7
-7
@@ -9,7 +9,7 @@
|
|||||||
"version": "v0.7.6",
|
"version": "v0.7.6",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@jambonz/db-helpers": "^0.6.18",
|
"@jambonz/db-helpers": "^0.6.19",
|
||||||
"@jambonz/http-health-check": "^0.0.1",
|
"@jambonz/http-health-check": "^0.0.1",
|
||||||
"@jambonz/mw-registrar": "0.2.2",
|
"@jambonz/mw-registrar": "0.2.2",
|
||||||
"@jambonz/realtimedb-helpers": "^0.4.29",
|
"@jambonz/realtimedb-helpers": "^0.4.29",
|
||||||
@@ -561,9 +561,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@jambonz/db-helpers": {
|
"node_modules/@jambonz/db-helpers": {
|
||||||
"version": "0.6.18",
|
"version": "0.6.19",
|
||||||
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.6.18.tgz",
|
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.6.19.tgz",
|
||||||
"integrity": "sha512-9wTkOxqIbNd95+0yznGFPPTRih/xPRnX7ajrJMxzKqzzHQ/CUQgOfjfGcxmOBx3UxaWBmxyHQeYA3oZMPPyX+Q==",
|
"integrity": "sha512-mR9PqPY4aEoeTtUuZI1+4r+k1BFKoe0Dl9pyeAEvm7KSlRMHCVWbOzVE8WfoifC9dZ9541iW5pseqqip3QMkyw==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cidr-matcher": "^2.1.1",
|
"cidr-matcher": "^2.1.1",
|
||||||
"debug": "^4.3.3",
|
"debug": "^4.3.3",
|
||||||
@@ -5521,9 +5521,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"@jambonz/db-helpers": {
|
"@jambonz/db-helpers": {
|
||||||
"version": "0.6.18",
|
"version": "0.6.19",
|
||||||
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.6.18.tgz",
|
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.6.19.tgz",
|
||||||
"integrity": "sha512-9wTkOxqIbNd95+0yznGFPPTRih/xPRnX7ajrJMxzKqzzHQ/CUQgOfjfGcxmOBx3UxaWBmxyHQeYA3oZMPPyX+Q==",
|
"integrity": "sha512-mR9PqPY4aEoeTtUuZI1+4r+k1BFKoe0Dl9pyeAEvm7KSlRMHCVWbOzVE8WfoifC9dZ9541iW5pseqqip3QMkyw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"cidr-matcher": "^2.1.1",
|
"cidr-matcher": "^2.1.1",
|
||||||
"debug": "^4.3.3",
|
"debug": "^4.3.3",
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@
|
|||||||
"jslint": "eslint app.js lib"
|
"jslint": "eslint app.js lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@jambonz/db-helpers": "^0.6.18",
|
"@jambonz/db-helpers": "^0.6.19",
|
||||||
"@jambonz/http-health-check": "^0.0.1",
|
"@jambonz/http-health-check": "^0.0.1",
|
||||||
"@jambonz/mw-registrar": "0.2.2",
|
"@jambonz/mw-registrar": "0.2.2",
|
||||||
"@jambonz/realtimedb-helpers": "^0.4.29",
|
"@jambonz/realtimedb-helpers": "^0.4.29",
|
||||||
|
|||||||
Reference in New Issue
Block a user