Skip to content

Commit

Permalink
Improve code reuse and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Feb 19, 2024
1 parent a5cc51b commit 78ab10e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
22 changes: 17 additions & 5 deletions packages/node-core/src/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,30 @@ export abstract class ApiService<A = any, SA = any, B extends Array<any> = any[]
private timeouts: Record<string, NodeJS.Timeout | undefined> = {};

async fetchBlocks(heights: number[], numAttempts = MAX_RECONNECT_ATTEMPTS): Promise<B> {
return this.retryFetch(async () => {
// Get the latest fetch function from the provider
const apiInstance = this.connectionPoolService.api;
return apiInstance.fetchBlocks(heights);
}, numAttempts);
}

protected async retryFetch(fn: () => Promise<B>, numAttempts = MAX_RECONNECT_ATTEMPTS): Promise<B> {
let reconnectAttempts = 0;
let lastError: Error | null = null;
while (reconnectAttempts < numAttempts) {
try {
// Get the latest fetch function from the provider
const apiInstance = this.connectionPoolService.api;
return await apiInstance.fetchBlocks(heights);
return await fn();
} catch (e: any) {
logger.error(e, `Failed to fetch blocks ${heights[0]}...${heights[heights.length - 1]}`);

lastError = e;
reconnectAttempts++;
}
}
if (lastError !== null) {
logger.error(
`Maximum number of retries (${numAttempts}) reached. See the following error for the underlying reason.`
);
throw lastError;
}
throw new Error(`Maximum number of retries (${numAttempts}) reached.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export abstract class BlockDispatcher<B, DS>
// Do nothing, fetching the block was flushed, this could be caused by forked blocks or dynamic datasources
return;
}
logger.warn(e, 'Failed to enqueue fetched block to process');
logger.error(e, 'Failed to enqueue fetched block to process');
process.exit(1);
});

Expand Down
23 changes: 6 additions & 17 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,27 +263,16 @@ export class ApiService
return `api.rpc.${ext?.section ?? '*'}.${ext?.method ?? '*'}`;
}

// Overrides the super function because of the specVer
async fetchBlocks(
heights: number[],
overallSpecVer?: number,
numAttempts = MAX_RECONNECT_ATTEMPTS,
): Promise<LightBlockContent[]> {
let reconnectAttempts = 0;
while (reconnectAttempts < numAttempts) {
try {
const apiInstance = this.connectionPoolService.api;
return await apiInstance.fetchBlocks(heights, overallSpecVer);
} catch (e: any) {
logger.error(
e,
`Failed to fetch blocks ${heights[0]}...${
heights[heights.length - 1]
}`,
);

reconnectAttempts++;
}
}
throw new Error(`Maximum number of retries (${numAttempts}) reached.`);
return this.retryFetch(async () => {
// Get the latest fetch function from the provider
const apiInstance = this.connectionPoolService.api;
return apiInstance.fetchBlocks(heights, overallSpecVer);
}, numAttempts);
}
}

0 comments on commit 78ab10e

Please sign in to comment.