Skip to content

Commit 1897b9c

Browse files
Nana-ECebadierelukelee-sl
authored
Added the abort controller to cancel requests. (#1067) (#1077)
Uses the axios controller abort to cancel requests on timeouts. --------- Signed-off-by: ebadiere <[email protected]> Signed-off-by: Nana Essilfie-Conduah <[email protected]> Co-authored-by: Eric Badiere <[email protected]> Co-authored-by: lukelee-sl <[email protected]>
1 parent 80eaa25 commit 1897b9c

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export class MirrorNodeClient {
7979
private static CONTRACT_RESULT_LOGS_PROPERTY = 'logs';
8080
private static CONTRACT_STATE_PROPERTY = 'state';
8181

82+
83+
8284
private static ORDER = {
8385
ASC: 'asc',
8486
DESC: 'desc'
@@ -117,7 +119,7 @@ export class MirrorNodeClient {
117119
headers: {
118120
'Content-Type': 'application/json'
119121
},
120-
timeout: 10 * 1000
122+
timeout: parseInt(process.env.MIRROR_NODE_TIMEOUT || '10000')
121123
});
122124
//@ts-ignore
123125
axiosRetry(axiosClient, {
@@ -192,19 +194,22 @@ export class MirrorNodeClient {
192194
const start = Date.now();
193195
const requestIdPrefix = formatRequestIdMessage(requestId);
194196
let ms;
197+
const controller = new AbortController();
195198
try {
196199
let response;
197-
const headers = {
200+
201+
const axiosRequestConfig = {
198202
headers:{
199203
'requestId': requestId || ''
200-
}
204+
},
205+
signal: controller.signal
201206
};
202207

203208
if (method === 'GET') {
204-
response = await this.restClient.get(path, headers);
209+
response = await this.restClient.get(path, axiosRequestConfig);
205210
}
206211
else {
207-
response = await this.web3Client.post(path, data, headers);
212+
response = await this.web3Client.post(path, data, axiosRequestConfig);
208213
}
209214

210215
const ms = Date.now() - start;
@@ -215,7 +220,7 @@ export class MirrorNodeClient {
215220
ms = Date.now() - start;
216221
const effectiveStatusCode = error.response?.status || MirrorNodeClientError.ErrorCodes[error.code] || MirrorNodeClient.unknownServerErrorHttpStatusCode;
217222
this.mirrorResponseHistogram.labels(pathLabel, effectiveStatusCode).observe(ms);
218-
this.handleError(error, path, effectiveStatusCode, method, allowedErrorStatuses, requestId);
223+
this.handleError(error, path, effectiveStatusCode, method, controller, allowedErrorStatuses, requestId);
219224
}
220225
return null;
221226
}
@@ -229,7 +234,12 @@ export class MirrorNodeClient {
229234
return this.request(path, pathLabel, 'POST', data, allowedErrorStatuses, requestId);
230235
}
231236

232-
handleError(error: any, path: string, effectiveStatusCode: number, method: REQUEST_METHODS, allowedErrorStatuses?: number[], requestId?: string) {
237+
handleError(error: any, path: string, effectiveStatusCode: number, method: REQUEST_METHODS, controller: AbortController, allowedErrorStatuses?: number[], requestId?: string) {
238+
const mirrorError = new MirrorNodeClientError(error, effectiveStatusCode);
239+
if(mirrorError.isTimeout()){
240+
controller.abort();
241+
}
242+
233243
const requestIdPrefix = formatRequestIdMessage(requestId);
234244
if (allowedErrorStatuses && allowedErrorStatuses.length) {
235245
if (error.response && allowedErrorStatuses.indexOf(effectiveStatusCode) !== -1) {
@@ -240,8 +250,6 @@ export class MirrorNodeClient {
240250

241251
this.logger.error(new Error(error.message), `${requestIdPrefix} [${method}] ${path} ${effectiveStatusCode} status`);
242252

243-
const mirrorError = new MirrorNodeClientError(error, effectiveStatusCode);
244-
245253
// we only need contract revert errors here as it's not the same as not supported
246254
if (mirrorError.isContractReverted() && !mirrorError.isNotSupported() && !mirrorError.isNotSupportedSystemContractOperaton()) {
247255
throw predefined.CONTRACT_REVERT(mirrorError.errorMessage);

0 commit comments

Comments
 (0)