push retry sending messages if no response was recieved

This commit is contained in:
Bassel 2023-05-19 15:30:29 +03:00
parent aa749f4022
commit 2d38a9394a
4 changed files with 40 additions and 1 deletions

View File

@ -30,6 +30,8 @@ export const createDispatcher = (ws: WebSocket) => {
return new Promise<U | null>((resolve, reject) => {
const uuid = randomUUID();
const payload = { ...message, uuid };
let numberOfTimesMessageWasSent = 0;
let responseWasReceived = false;
if (options.attachTransfer) {
Object.assign(payload, { transferID: state.transfer?.id });
@ -43,7 +45,23 @@ export const createDispatcher = (ws: WebSocket) => {
}
});
const sendPeriodically = () => {
setTimeout(() => {
if (!responseWasReceived) {
if (numberOfTimesMessageWasSent < 5) {
numberOfTimesMessageWasSent += 1;
ws.send(stringifiedPayload);
sendPeriodically();
} else {
reject(new ProviderError('error', 'Request timed out'));
}
}
}, 50000);
};
const onResponse = (raw: RawData) => {
responseWasReceived = true;
numberOfTimesMessageWasSent = 0;
const response: server.Message<U> = JSON.parse(raw.toString());
if (response.uuid === uuid) {
if (response.error) {
@ -57,6 +75,7 @@ export const createDispatcher = (ws: WebSocket) => {
};
ws.once('message', onResponse);
sendPeriodically();
});
};

View File

@ -19,6 +19,12 @@ export interface Handler {
get startedAt(): TransferState['startedAt'];
set startedAt(id: TransferState['startedAt']);
// Add message UUIDs
addUUID(uuid: string): void;
// Check if a message UUID exists
hasUUID(uuid: string): boolean;
/**
* Returns whether a transfer is currently in progress or not
*/

View File

@ -205,8 +205,13 @@ export const createPushController = handlerControllerFactory<Partial<PushHandler
await this.respond(undefined, new Error('Missing uuid in message'));
}
const { uuid, type } = msg;
if (proto.hasUUID(msg.uuid)) {
await this.respond(msg.uuid);
return;
}
const { uuid, type } = msg;
proto.addUUID(uuid);
// Regular command message (init, end, status)
if (type === 'command') {
const { command } = msg;

View File

@ -79,6 +79,7 @@ export const handlerControllerFactory =
return async (ctx: Context) => {
handleWSUpgrade(wss, ctx, (ws) => {
const state: TransferState = { id: undefined };
const messageuuids = new Set<string>();
const prototype: Handler = {
// Transfer ID
@ -99,6 +100,14 @@ export const handlerControllerFactory =
state.startedAt = timestamp;
},
addUUID(uuid) {
messageuuids.add(uuid);
},
hasUUID(uuid) {
return messageuuids.has(uuid);
},
isTransferStarted() {
return this.transferID !== undefined && this.startedAt !== undefined;
},