ws-requestor: queue outgoing messages if we are in the process of connecting to the remote wss server

This commit is contained in:
Dave Horton
2022-05-02 13:09:23 -04:00
parent 944b8a29ca
commit eb0f55e0e3
2 changed files with 32 additions and 2 deletions

View File

@@ -16,6 +16,8 @@ class WsRequestor extends BaseRequestor {
this.maliciousClient = false;
this.closedByUs = false;
this.backoffMs = 500;
this.connectInProgress = false;
this.queuedMsg = [];
assert(this._isAbsoluteUrl(this.url));
@@ -41,6 +43,10 @@ class WsRequestor extends BaseRequestor {
this.logger.info({url: this.url}, 'WsRequestor:request - discarding msg to malicious client');
return;
}
if (this.closedByUs) {
this.logger.debug(`WsRequestor:request - discarding ${type} because we closed the socket`);
return;
}
if (type === 'session:new') this.call_sid = params.callSid;
@@ -53,6 +59,12 @@ class WsRequestor extends BaseRequestor {
/* connect if necessary */
if (!this.ws) {
if (this.connectInProgress) {
this.logger.debug(`WsRequestor:request - queueing ${type} message since we are connecting`);
this.queuedMsg.push({type, hook, params, httpHeaders});
return;
}
this.logger.debug('WsRequestor:request - connecting since we do not have a connection');
if (this.connections >= MAX_RECONNECTS) {
throw new Error(`max attempts connecting to ${this.url}`);
}
@@ -83,6 +95,15 @@ class WsRequestor extends BaseRequestor {
...b3
};
/* send any queueed messages */
if (this.queuedMsg.length > 0) {
for (const {type, hook, params, httpHeaders} of this.queuedMsg) {
this.logger.debug(`WsRequestor:request - preparing queued ${type} for sending`);
setImmediate(this.request.bind(this, type, hook, params, httpHeaders));
}
this.queuedMsg.length = 0;
}
//this.logger.debug({obj}, `websocket: sending (${url})`);
/* simple notifications */