Skip to content

Commit

Permalink
onboarding: request level timeouts (#189)
Browse files Browse the repository at this point in the history
* add request level timeout handling

* add request-level timeout
  • Loading branch information
TalDerei authored Sep 6, 2024
1 parent b14f093 commit 75af184
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions apps/extension/src/hooks/latest-block-height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,37 @@ const fetchBlockHeightWithFallback = async (endpoints: string[]): Promise<number
}

try {
return await fetchBlockHeight(randomGrpcEndpoint);
return await fetchBlockHeightWithTimeout(randomGrpcEndpoint);
} catch (e) {
// Remove the current endpoint from the list and retry with remaining endpoints
const remainingEndpoints = endpoints.filter(endpoint => endpoint !== randomGrpcEndpoint);
return fetchBlockHeightWithFallback(remainingEndpoints);
}
};

// Fetch the block height from a specific RPC endpoint with a timeout to prevent hanging requests.
// Fetch the block height from a specific RPC endpoint with a request-level timeout that superceeds
// the channel transport-level timeout to prevent hanging requests.
export const fetchBlockHeightWithTimeout = async (
grpcEndpoint: string,
timeoutMs = 5000,
): Promise<number> => {
const tendermintClient = createPromiseClient(
TendermintProxyService,
createGrpcWebTransport({ baseUrl: grpcEndpoint }),
);

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.
export const fetchBlockHeight = async (grpcEndpoint: string): Promise<number> => {
const tendermintClient = createPromiseClient(
TendermintProxyService,
createGrpcWebTransport({ baseUrl: grpcEndpoint, defaultTimeoutMs: 2000 }),
createGrpcWebTransport({ baseUrl: grpcEndpoint }),
);

const result = await tendermintClient.getStatus({});
Expand Down

0 comments on commit 75af184

Please sign in to comment.