Skip to content

Commit 43738e8

Browse files
committed
feat: compute maxRetries and backup for Cockatiel Polling policy
1 parent 753a881 commit 43738e8

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

packages/shield-controller/src/backend.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
ShieldBackend,
1818
} from './types';
1919
import { PollingWithCockatielPolicy } from './polling-with-policy';
20-
import { HttpError } from '@metamask/controller-utils';
20+
import { ConstantBackoff, HttpError } from '@metamask/controller-utils';
2121

2222
export type InitCoverageCheckRequest = {
2323
txParams: [
@@ -58,10 +58,6 @@ export type GetCoverageResultResponse = {
5858
export class ShieldRemoteBackend implements ShieldBackend {
5959
readonly #getAccessToken: () => Promise<string>;
6060

61-
readonly #getCoverageResultTimeout: number;
62-
63-
readonly #getCoverageResultPollInterval: number;
64-
6561
readonly #baseUrl: string;
6662

6763
readonly #fetch: typeof globalThis.fetch;
@@ -82,11 +78,15 @@ export class ShieldRemoteBackend implements ShieldBackend {
8278
fetch: typeof globalThis.fetch;
8379
}) {
8480
this.#getAccessToken = getAccessToken;
85-
this.#getCoverageResultTimeout = getCoverageResultTimeout;
86-
this.#getCoverageResultPollInterval = getCoverageResultPollInterval;
8781
this.#baseUrl = baseUrl;
8882
this.#fetch = fetchFn;
89-
this.#pollingPolicy = new PollingWithCockatielPolicy();
83+
84+
const { backoff, maxRetries } = computePollingIntervalAndRetryCount(getCoverageResultTimeout, getCoverageResultPollInterval);
85+
86+
this.#pollingPolicy = new PollingWithCockatielPolicy({
87+
backoff,
88+
maxRetries,
89+
});
9090
}
9191

9292
async checkCoverage(req: CheckCoverageRequest): Promise<CoverageResult> {
@@ -309,3 +309,20 @@ export function parseSignatureRequestMethod(
309309

310310
return signatureRequest.type;
311311
}
312+
313+
314+
/**
315+
* Compute the polling interval and retry count for the Cockatiel policy based on the timeout and poll interval given.
316+
*
317+
* @param timeout - The timeout in milliseconds.
318+
* @param pollInterval - The poll interval in milliseconds.
319+
* @returns The polling interval and retry count.
320+
*/
321+
function computePollingIntervalAndRetryCount(timeout: number, pollInterval: number) {
322+
const backoff = new ConstantBackoff(pollInterval);
323+
const maxRetries = Math.floor(timeout / pollInterval) + 1;
324+
return {
325+
backoff,
326+
maxRetries,
327+
};
328+
}

0 commit comments

Comments
 (0)