RPC timeouts (#4661)

* RPC timeouts

* for loop
This commit is contained in:
Jaco
2022-03-17 10:14:26 +02:00
committed by GitHub
parent 32f1495993
commit f108d16d1b
2 changed files with 36 additions and 1 deletions
+7
View File
@@ -1,5 +1,12 @@
# CHANGELOG
## master
Changes:
- Support for rejection of hanging RPC requests (30s timeout)
## 7.12.1 Mar 13, 2022
Changes:
+29 -1
View File
@@ -25,6 +25,7 @@ interface WsStateAwaiting {
callback: ProviderInterfaceCallback;
method: string;
params: unknown[];
start: number;
subscription?: SubscriptionHandler;
}
@@ -39,7 +40,11 @@ const ALIASES: { [index: string]: string } = {
chain_unsubscribeFinalisedHeads: 'chain_unsubscribeFinalizedHeads'
};
const RETRY_DELAY = 2500;
const RETRY_DELAY = 2_500;
const TIMEOUT_S = 30;
const TIMEOUT_MS = TIMEOUT_S * 1000;
const TIMEOUT_INTERVAL = 5_000;
const MEGABYTE = 1024 * 1024;
@@ -136,6 +141,9 @@ export class WsProvider implements ProviderInterface {
resolve(this);
});
});
// timeout any handlers that have not had a response
setInterval(() => this.#timeout(), TIMEOUT_INTERVAL);
}
/**
@@ -299,6 +307,7 @@ export class WsProvider implements ProviderInterface {
callback,
method,
params,
start: Date.now(),
subscription
};
@@ -515,4 +524,23 @@ export class WsProvider implements ProviderInterface {
}
})).catch(l.error);
};
#timeout = (): void => {
const now = Date.now();
const ids = Object.keys(this.#handlers);
for (let i = 0; i < ids.length; i++) {
const handler = this.#handlers[ids[i]];
if ((now - handler.start) > TIMEOUT_MS) {
try {
handler.callback(new Error(`No response received from RPC endpoint in ${TIMEOUT_S}s`), undefined);
} catch {
// ignore
}
delete this.#handlers[ids[i]];
}
}
};
}