From 010b4d27784b5dbbe9155372ea4aeb3267b41419 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 13 Dec 2022 14:52:38 -0500 Subject: [PATCH] bugfix: db caching had side affects of using closed http requestors --- lib/middleware.js | 25 +++++++++++++------------ lib/session/call-session.js | 1 + lib/utils/http-requestor.js | 4 +++- lib/utils/place-outdial.js | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/middleware.js b/lib/middleware.js index 18ba04cc..32468124 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -225,29 +225,30 @@ module.exports = function(srf, logger) { * create a requestor that we will use for all http requests we make during the call. * also create a notifier for call status events (if not needed, its a no-op). */ + + /* allow for caching data - when caching treat retrieved data as immutable */ + const app2 = process.env.JAMBONES_MYSQL_REFRESH_TTL ? JSON.parse(JSON.stringify(app)) : app; if ('WS' === app.call_hook?.method || app.call_hook?.url.startsWith('ws://') || app.call_hook?.url.startsWith('wss://')) { - app.requestor = new WsRequestor(logger, account_sid, app.call_hook, accountInfo.account.webhook_secret) ; - app.notifier = app.requestor; - app.call_hook.method = 'WS'; + app2.requestor = new WsRequestor(logger, account_sid, app.call_hook, accountInfo.account.webhook_secret) ; + app2.notifier = app.requestor; + app2.call_hook.method = 'WS'; } else { - app.requestor = new HttpRequestor(logger, account_sid, app.call_hook, accountInfo.account.webhook_secret); - if (app.call_status_hook) app.notifier = new HttpRequestor(logger, account_sid, app.call_status_hook, + app2.requestor = new HttpRequestor(logger, account_sid, app.call_hook, accountInfo.account.webhook_secret); + if (app.call_status_hook) app2.notifier = new HttpRequestor(logger, account_sid, app.call_status_hook, accountInfo.account.webhook_secret); - else app.notifier = {request: () => {}}; + else app2.notifier = {request: () => {}}; } - req.locals.application = app; - const obj = Object.assign({}, app); - delete obj.requestor; - delete obj.notifier; + req.locals.application = app2; + // eslint-disable-next-line no-unused-vars - const {call_hook, call_status_hook, ...appInfo} = obj; // mask sensitive data like user/pass on webhook + const {call_hook, call_status_hook, ...appInfo} = app; // mask sensitive data like user/pass on webhook logger.info({app: appInfo}, `retrieved application for incoming call to ${req.locals.calledNumber}`); req.locals.callInfo = new CallInfo({ req, - app, + app: app2, direction: CallDirection.Inbound, traceId: rootSpan.traceId }); diff --git a/lib/session/call-session.js b/lib/session/call-session.js index c2ee45b7..476dcdbd 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -1308,6 +1308,7 @@ class CallSession extends Emitter { } this.tmpFiles.clear(); this.requestor && this.requestor.close(); + this.notifier && this.notifier.close(); this.rootSpan && this.rootSpan.end(); } diff --git a/lib/utils/http-requestor.js b/lib/utils/http-requestor.js index 6dd1f35b..888f2bb6 100644 --- a/lib/utils/http-requestor.js +++ b/lib/utils/http-requestor.js @@ -31,6 +31,8 @@ class HttpRequestor extends BaseRequestor { if (u.port) this._baseUrl = `${u.protocol}://${u.resource}:${u.port}`; else this._baseUrl = `${u.protocol}://${u.resource}`; this._protocol = u.protocol; + this._resource = u.resource; + this._port = u.port; this._search = u.search; this._usePools = process.env.HTTP_POOL && parseInt(process.env.HTTP_POOL); @@ -98,7 +100,7 @@ class HttpRequestor extends BaseRequestor { } else { const u = parseUrl(url); - if (u.resource === this._resource && u.protocol === this._protocol) { + if (u.resource === this._resource && u.port === this._port && u.protocol === this._protocol) { client = this.client; path = u.pathname; query = u.query; diff --git a/lib/utils/place-outdial.js b/lib/utils/place-outdial.js index 8d3c7481..1ad43b46 100644 --- a/lib/utils/place-outdial.js +++ b/lib/utils/place-outdial.js @@ -412,7 +412,7 @@ class SingleDialer extends Emitter { this.callInfo.updateCallStatus(callStatus, sipStatus, sipReason); if (typeof duration === 'number') this.callInfo.duration = duration; try { - this.requestor.request('call:status', this.application.call_status_hook, this.callInfo.toJSON()); + this.notifier.request('call:status', this.application.call_status_hook, this.callInfo.toJSON()); } catch (err) { this.logger.info(err, `SingleDialer:_notifyCallStatusChange error sending ${callStatus} ${sipStatus}`); }