Socket request timeout handling, with retries.

This commit is contained in:
Håvar Aambø Fosstveit
2020-05-19 14:09:27 +02:00
parent b0e57756cb
commit 0f184a3a29
6 changed files with 109 additions and 12 deletions
+31 -7
View File
@@ -1,6 +1,7 @@
import Logger from './Logger';
import hark from 'hark';
import { getSignalingUrl } from './urlFactory';
import { SocketTimeoutError } from './utils';
import * as requestActions from './actions/requestActions';
import * as meActions from './actions/meActions';
import * as roomActions from './actions/roomActions';
@@ -574,7 +575,7 @@ export default class RoomClient
if (called)
return;
called = true;
callback(new Error('Request timeout.'));
callback(new SocketTimeoutError('Request timed out'));
},
ROOM_OPTIONS.requestTimeout
);
@@ -590,13 +591,13 @@ export default class RoomClient
};
}
sendRequest(method, data)
_sendRequest(method, data)
{
return new Promise((resolve, reject) =>
{
if (!this._signalingSocket)
{
reject('No socket connection.');
reject('No socket connection');
}
else
{
@@ -606,19 +607,42 @@ export default class RoomClient
this.timeoutCallback((err, response) =>
{
if (err)
{
reject(err);
}
else
{
resolve(response);
}
})
);
}
});
}
async sendRequest(method, data)
{
logger.debug('sendRequest() [method:"%s", data:"%o"]', method, data);
const {
requestRetries = 3
} = window.config;
for (let tries = 0; tries < requestRetries; tries++)
{
try
{
return await this._sendRequest(method, data);
}
catch (error)
{
if (
error instanceof SocketTimeoutError &&
tries < requestRetries
)
logger.warn('sendRequest() | timeout, retrying [attempt:"%s"]', tries);
else
throw error;
}
}
}
async changeDisplayName(displayName)
{
logger.debug('changeDisplayName() [displayName:"%s"]', displayName);
+19 -1
View File
@@ -16,4 +16,22 @@ export const idle = (callback, delay) =>
handle = setTimeout(callback, delay);
};
};
};
/**
* Error produced when a socket request has a timeout.
*/
export class SocketTimeoutError extends Error
{
constructor(message)
{
super(message);
this.name = 'SocketTimeoutError';
if (Error.hasOwnProperty('captureStackTrace')) // Just in V8.
Error.captureStackTrace(this, SocketTimeoutError);
else
this.stack = (new Error(message)).stack;
}
}