From c265e7d019d445add2d2cfb7cfcbdeee059684d3 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 29 Mar 2022 13:47:32 +0200 Subject: wallet: make retries more robust and consistent --- .../taler-wallet-core/src/headless/NodeHttpLib.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'packages/taler-wallet-core/src/headless') diff --git a/packages/taler-wallet-core/src/headless/NodeHttpLib.ts b/packages/taler-wallet-core/src/headless/NodeHttpLib.ts index df25a1092..5290bd441 100644 --- a/packages/taler-wallet-core/src/headless/NodeHttpLib.ts +++ b/packages/taler-wallet-core/src/headless/NodeHttpLib.ts @@ -20,6 +20,7 @@ * Imports. */ import { + DEFAULT_REQUEST_TIMEOUT_MS, Headers, HttpRequestLibrary, HttpRequestOptions, @@ -65,13 +66,16 @@ export class NodeHttpLib implements HttpRequestLibrary { `request to origin ${parsedUrl.origin} was throttled`, ); } - let timeout: number | undefined; + let timeoutMs: number | undefined; if (typeof opt?.timeout?.d_ms === "number") { - timeout = opt.timeout.d_ms; + timeoutMs = opt.timeout.d_ms; + } else { + timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS; } + // FIXME: Use AbortController / etc. to handle cancellation let resp: AxiosResponse; try { - resp = await Axios({ + let respPromise = Axios({ method, url: url, responseType: "arraybuffer", @@ -79,9 +83,13 @@ export class NodeHttpLib implements HttpRequestLibrary { validateStatus: () => true, transformResponse: (x) => x, data: body, - timeout, + timeout: timeoutMs, maxRedirects: 0, }); + if (opt?.cancellationToken) { + respPromise = opt.cancellationToken.racePromise(respPromise); + } + resp = await respPromise; } catch (e: any) { throw TalerError.fromDetail( TalerErrorCode.WALLET_NETWORK_ERROR, @@ -94,11 +102,13 @@ export class NodeHttpLib implements HttpRequestLibrary { } const makeText = async (): Promise => { + opt?.cancellationToken?.throwIfCancelled(); const respText = new Uint8Array(resp.data); return bytesToString(respText); }; const makeJson = async (): Promise => { + opt?.cancellationToken?.throwIfCancelled(); let responseJson; const respText = await makeText(); try { @@ -130,6 +140,7 @@ export class NodeHttpLib implements HttpRequestLibrary { return responseJson; }; const makeBytes = async () => { + opt?.cancellationToken?.throwIfCancelled(); if (typeof resp.data.byteLength !== "number") { throw Error("expected array buffer"); } @@ -150,6 +161,7 @@ export class NodeHttpLib implements HttpRequestLibrary { bytes: makeBytes, }; } + async get(url: string, opt?: HttpRequestOptions): Promise { return this.fetch(url, { method: "GET", -- cgit v1.2.3