Skip to content

Commit 09a8d80

Browse files
authored
make WS provider timeout configurable (#4758)
* make WS provider timeout configurable * minor
1 parent 5637579 commit 09a8d80

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/rpc-provider/src/ws/index.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const TEST_WS_URL = 'ws://localhost-index.spec.ts:9977';
1212
let provider: WsProvider | null;
1313
let mock: Mock;
1414

15-
function createWs (requests: Request[], autoConnect = 1000, headers?: Record<string, string>): WsProvider {
15+
function createWs (requests: Request[], autoConnect = 1000, headers?: Record<string, string>, timeout?: number): WsProvider {
1616
mock = mockWs(requests, TEST_WS_URL);
17-
provider = new WsProvider(TEST_WS_URL, autoConnect, headers);
17+
provider = new WsProvider(TEST_WS_URL, autoConnect, headers, timeout);
1818

1919
return provider;
2020
}
@@ -40,6 +40,13 @@ describe('Ws', (): void => {
4040
it('allows you to initialize the provider with custom headers', () => {
4141
createWs([], 100, { foo: 'bar' });
4242
});
43+
44+
it('allows you to set custom timeout value for handlers', () => {
45+
const CUSTOM_TIMEOUT_S = 90;
46+
const CUSTOM_TIMEOUT_MS = CUSTOM_TIMEOUT_S * 1000;
47+
48+
createWs([], 100, { foo: 'bar' }, CUSTOM_TIMEOUT_MS);
49+
});
4350
});
4451

4552
describe('Endpoint Parsing', (): void => {

packages/rpc-provider/src/ws/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ const ALIASES: { [index: string]: string } = {
4242

4343
const RETRY_DELAY = 2_500;
4444

45-
const TIMEOUT_S = 60;
46-
const TIMEOUT_MS = TIMEOUT_S * 1000;
45+
const DEFAULT_TIMEOUT_MS = 60 * 1000;
4746
const TIMEOUT_INTERVAL = 5_000;
4847

4948
const MEGABYTE = 1024 * 1024;
@@ -111,11 +110,14 @@ export class WsProvider implements ProviderInterface {
111110

112111
#websocket: WebSocket | null;
113112

113+
#timeout: number;
114+
114115
/**
115116
* @param {string | string[]} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`, may provide an array of endpoint strings.
116117
* @param {boolean} autoConnect Whether to connect automatically or not.
118+
* @param {number} [timeout] Custom timeout value
117119
*/
118-
constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record<string, string> = {}) {
120+
constructor (endpoint: string | string[] = defaults.WS_URL, autoConnectMs: number | false = RETRY_DELAY, headers: Record<string, string> = {}, timeout?: number) {
119121
const endpoints = Array.isArray(endpoint)
120122
? endpoint
121123
: [endpoint];
@@ -137,6 +139,7 @@ export class WsProvider implements ProviderInterface {
137139
active: { requests: 0, subscriptions: 0 },
138140
total: { bytesRecv: 0, bytesSent: 0, cached: 0, requests: 0, subscriptions: 0, timeout: 0 }
139141
};
142+
this.#timeout = timeout || DEFAULT_TIMEOUT_MS;
140143

141144
if (autoConnectMs > 0) {
142145
this.connectWithRetry().catch((): void => {
@@ -210,7 +213,7 @@ export class WsProvider implements ProviderInterface {
210213
this.#websocket.onopen = this.#onSocketOpen;
211214

212215
// timeout any handlers that have not had a response
213-
this.#timeoutId = setInterval(() => this.#timeout(), TIMEOUT_INTERVAL);
216+
this.#timeoutId = setInterval(() => this.#timeoutHandlers(), TIMEOUT_INTERVAL);
214217
} catch (error) {
215218
l.error(error);
216219

@@ -557,16 +560,16 @@ export class WsProvider implements ProviderInterface {
557560
})).catch(l.error);
558561
};
559562

560-
#timeout = (): void => {
563+
#timeoutHandlers = (): void => {
561564
const now = Date.now();
562565
const ids = Object.keys(this.#handlers);
563566

564567
for (let i = 0; i < ids.length; i++) {
565568
const handler = this.#handlers[ids[i]];
566569

567-
if ((now - handler.start) > TIMEOUT_MS) {
570+
if ((now - handler.start) > this.#timeout) {
568571
try {
569-
handler.callback(new Error(`No response received from RPC endpoint in ${TIMEOUT_S}s`), undefined);
572+
handler.callback(new Error(`No response received from RPC endpoint in ${this.#timeout / 1000}s`), undefined);
570573
} catch {
571574
// ignore
572575
}

0 commit comments

Comments
 (0)