Skip to content

Commit

Permalink
add request level timeout handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Sep 5, 2024
1 parent b14f093 commit bf69c9b
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 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,51 @@ 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> => {
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<number> => {
const tendermintClient = createPromiseClient(
TendermintProxyService,
createGrpcWebTransport({ baseUrl: grpcEndpoint, defaultTimeoutMs: 2000 }),
createGrpcWebTransport({ baseUrl: grpcEndpoint }),
);

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

0 comments on commit bf69c9b

Please sign in to comment.