From bf69c9b110356a33dab29c05710a4206c42b4759 Mon Sep 17 00:00:00 2001 From: Tal Derei Date: Wed, 4 Sep 2024 21:46:25 -0700 Subject: [PATCH 1/2] add request level timeout handling --- .../src/hooks/latest-block-height.ts | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/apps/extension/src/hooks/latest-block-height.ts b/apps/extension/src/hooks/latest-block-height.ts index 6dd35982..b78de00d 100644 --- a/apps/extension/src/hooks/latest-block-height.ts +++ b/apps/extension/src/hooks/latest-block-height.ts @@ -23,7 +23,7 @@ const fetchBlockHeightWithFallback = async (endpoints: string[]): Promise endpoint !== randomGrpcEndpoint); @@ -31,11 +31,43 @@ const fetchBlockHeightWithFallback = async (endpoints: string[]): Promise => { + return new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + reject(new Error('Request timed out')); + }, timeoutMs); + + const tendermintClient = createPromiseClient( + TendermintProxyService, + createGrpcWebTransport({ baseUrl: grpcEndpoint }), + ); + + tendermintClient + .getStatus({}) + .then(result => { + if (!result.syncInfo) { + reject(new Error('No syncInfo in getStatus result')); + } + clearTimeout(timeout); + resolve(Number(result.syncInfo?.latestBlockHeight)); + }) + .catch(() => { + clearTimeout(timeout); + reject(new Error('RPC request timed out while fetching block height')); + }); + }); +}; + +// Fetch the block height from a specific RPC endpoint. export const fetchBlockHeight = async (grpcEndpoint: string): Promise => { const tendermintClient = createPromiseClient( TendermintProxyService, - createGrpcWebTransport({ baseUrl: grpcEndpoint, defaultTimeoutMs: 2000 }), + createGrpcWebTransport({ baseUrl: grpcEndpoint }), ); const result = await tendermintClient.getStatus({}); From 7dd4f55f84e8a50b702d51c0d0611f30facc7c0f Mon Sep 17 00:00:00 2001 From: Tal Derei Date: Fri, 6 Sep 2024 00:23:35 -0700 Subject: [PATCH 2/2] add request-level timeout --- .../src/hooks/latest-block-height.ts | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/apps/extension/src/hooks/latest-block-height.ts b/apps/extension/src/hooks/latest-block-height.ts index b78de00d..a170c2da 100644 --- a/apps/extension/src/hooks/latest-block-height.ts +++ b/apps/extension/src/hooks/latest-block-height.ts @@ -37,30 +37,16 @@ export const fetchBlockHeightWithTimeout = async ( grpcEndpoint: string, timeoutMs = 5000, ): Promise => { - return new Promise((resolve, reject) => { - const timeout = setTimeout(() => { - reject(new Error('Request timed out')); - }, timeoutMs); - - const tendermintClient = createPromiseClient( - TendermintProxyService, - createGrpcWebTransport({ baseUrl: grpcEndpoint }), - ); + const tendermintClient = createPromiseClient( + TendermintProxyService, + createGrpcWebTransport({ baseUrl: grpcEndpoint }), + ); - tendermintClient - .getStatus({}) - .then(result => { - if (!result.syncInfo) { - reject(new Error('No syncInfo in getStatus result')); - } - clearTimeout(timeout); - resolve(Number(result.syncInfo?.latestBlockHeight)); - }) - .catch(() => { - clearTimeout(timeout); - reject(new Error('RPC request timed out while fetching block height')); - }); - }); + const result = await tendermintClient.getStatus({}, { signal: AbortSignal.timeout(timeoutMs) }); + if (!result.syncInfo) { + throw new Error('No syncInfo in getStatus result'); + } + return Number(result.syncInfo.latestBlockHeight); }; // Fetch the block height from a specific RPC endpoint.