Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onboarding: request level timeouts #189

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions apps/extension/src/hooks/latest-block-height.ts
Copy link
Contributor

@turbocrime turbocrime Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this could be simplified.

notably, each PromiseClient method accepts options as a second parameter, including both timeoutMs and an AbortSignal.

interface CallOptions {
  /**
   * Timeout in milliseconds.
   *
   * Set to <= 0 to disable the default timeout.
   */
  timeoutMs?: number;

  /**
   * An optional AbortSignal to cancel the call.
   * If cancelled, an error with Code.Canceled is raised.
   */
  signal?: AbortSignal;
  
  headers?: HeadersInit;
  onHeader?(headers: Headers): void;
  onTrailer?(trailers: Headers): void;
  contextValues?: ContextValues;
}

pre-onboarding, you won't have access to the normal fullnode queriers from services. so constructing transports is definitely the right idea.

will come back with some suggestions.

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