Skip to content
Open
Show file tree
Hide file tree
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: 19 additions & 5 deletions backend/src/lib/rpc-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,27 @@ export class RpcPool {
for (const endpoint of this.availableEndpoints()) {
const controller = new AbortController();
const started = Date.now();
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
try {
const result = await Promise.race([
operation(endpoint.server, controller.signal),
new Promise<never>((_, reject) => setTimeout(() => {
controller.abort();
reject(new Error(`${label} timed out after ${this.timeoutMs}ms`));
}, this.timeoutMs)),
new Promise<never>((_, reject) => {
timeoutHandle = setTimeout(() => {
controller.abort();
reject(new Error(`${label} timed out after ${this.timeoutMs}ms`));
}, this.timeoutMs);
}),
]);
this.recordSuccess(endpoint, Date.now() - started);
return result;
} catch (error) {
lastError = error;
this.recordFailure(endpoint, error, Date.now() - started);
logger.warn(`[RpcPool] ${label} failed on ${endpoint.url}; failing over`, { error });
} finally {
if (timeoutHandle !== undefined) {
clearTimeout(timeoutHandle);
}
}
}
throw lastError instanceof Error ? lastError : new Error(`${label} failed on all RPC endpoints`);
Expand Down Expand Up @@ -125,15 +132,22 @@ export class RpcPool {

private async probeAll(): Promise<void> {
await Promise.all(this.endpoints.map(async (endpoint) => {
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
try {
const started = Date.now();
await Promise.race([
Promise.all([endpoint.server.getHealth(), endpoint.server.getLatestLedger()]),
new Promise<never>((_, reject) => setTimeout(() => reject(new Error('health probe timeout')), this.timeoutMs)),
new Promise<never>((_, reject) => {
timeoutHandle = setTimeout(() => reject(new Error('health probe timeout')), this.timeoutMs);
}),
]);
this.recordSuccess(endpoint, Date.now() - started);
} catch (error) {
this.recordFailure(endpoint, error, this.timeoutMs);
} finally {
if (timeoutHandle !== undefined) {
clearTimeout(timeoutHandle);
}
}
}));
}
Expand Down
Loading
Loading