From 41528db630a7c9234288be243124b978787fd5cc Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 12 Nov 2024 14:57:04 -0500 Subject: [PATCH] fix call count race condition #151 (#152) * fix call count race condition #151 * logging fix --- lib/call-session.js | 16 ++++++++++------ lib/middleware.js | 4 +++- test/docker-compose-testbed.yaml | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/call-session.js b/lib/call-session.js index 7c8328c..cab5a47 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -677,12 +677,16 @@ class CallSession extends Emitter { if (process.env.JAMBONES_HOSTING || trackingOn) { const {writeCallCount, writeCallCountSP, writeCallCountApp} = this.req.srf.locals; - await nudgeCallCounts(this.logger, { - service_provider_sid: this.service_provider_sid, - account_sid: this.account_sid, - application_sid: this.application_sid - }, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) - .catch((err) => this.logger.error(err, 'Error decrementing call counts')); + if (!this.req.locals.callCountNudged) { + this.req.locals.callCountNudged = true; + this.logger.debug('decrementing call count at end of call'); + await nudgeCallCounts(this.logger, { + service_provider_sid: this.service_provider_sid, + account_sid: this.account_sid, + application_sid: this.application_sid + }, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) + .catch((err) => this.logger.error(err, 'Error decrementing call counts')); + } } /* write cdr for connected call */ diff --git a/lib/middleware.js b/lib/middleware.js index e79acca..69f9d23 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -106,7 +106,9 @@ module.exports = (srf, logger, redisClient) => { try { /* decrement count if INVITE is later rejected */ res.once('end', async({status}) => { - if (status > 200) { + if (status > 200 && !req.locals.callCountNudged) { + req.locals.callCountNudged = true; + logger.debug('decrementing call count due to call rejection'); nudgeCallCounts(logger, { service_provider_sid, account_sid, diff --git a/test/docker-compose-testbed.yaml b/test/docker-compose-testbed.yaml index 7940f50..b450e4f 100644 --- a/test/docker-compose-testbed.yaml +++ b/test/docker-compose-testbed.yaml @@ -24,7 +24,7 @@ services: ipv4_address: 172.39.0.2 sbc: - image: drachtio/drachtio-server:latest + image: drachtio/drachtio-server:0.8.26 command: drachtio --contact "sip:*;transport=udp" --loglevel debug --sofia-loglevel 9 ports: - "9060:9022/tcp"