Skip to content

Commit 539ed9b

Browse files
authored
JavaScript: Updates timer util to integrate a backoff for long running retries. (#7116)
1 parent a28443e commit 539ed9b

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

javascriptv3/example_code/cross-services/wkflw-pools-triggers/tests/scenario-auto-confirm.integration.test.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ describe("Scenario - AutoConfirm", () => {
3434
Capabilities: [Capability.CAPABILITY_NAMED_IAM],
3535
}),
3636
);
37-
await retry({ intervalInMs: 2000, maxRetries: 60 }, async () => {
38-
const { Stacks } = await cloudformationClient.send(
39-
new DescribeStacksCommand({ StackName: stackName }),
40-
);
41-
const stack = Stacks[0];
42-
if (stack.StackStatus !== "CREATE_COMPLETE") {
43-
throw new Error("Stack creation incomplete.");
44-
}
45-
});
37+
await retry(
38+
{ intervalInMs: 2000, maxRetries: 60, backoff: 1000 },
39+
async () => {
40+
const { Stacks } = await cloudformationClient.send(
41+
new DescribeStacksCommand({ StackName: stackName }),
42+
);
43+
const stack = Stacks[0];
44+
if (stack.StackStatus !== "CREATE_COMPLETE") {
45+
throw new Error("Stack creation incomplete.");
46+
}
47+
},
48+
);
4649
});
4750

4851
afterAll(async () => {

javascriptv3/example_code/libs/utils/util-timers.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const wait = (seconds) =>
1010

1111
/**
1212
* @template T
13-
* @param {{ intervalInMs: number, maxRetries: number, swallowError?: boolean, quiet?: boolean }} config
13+
* @param {{ intervalInMs: number, maxRetries: number, backoff?: number, swallowError?: boolean, quiet?: boolean }} config
1414
* @param {() => Promise<T>} fn
1515
* @returns {Promise<T>}
1616
*/
1717
const retry = (config, fn) =>
1818
new Promise((resolve, reject) => {
19-
const { intervalInMs = 500, maxRetries = 10 } = config;
19+
const { intervalInMs = 500, maxRetries = 10, backoff = 0 } = config;
2020
fn()
2121
.then(resolve)
2222
.catch((err) => {
@@ -32,10 +32,14 @@ const retry = (config, fn) =>
3232
config.swallowError ? resolve() : reject(err);
3333
} else {
3434
setTimeout(() => {
35-
retry({ ...config, maxRetries: maxRetries - 1 }, fn).then(
36-
resolve,
37-
reject,
38-
);
35+
retry(
36+
{
37+
...config,
38+
intervalInMs: intervalInMs + backoff,
39+
maxRetries: maxRetries - 1,
40+
},
41+
fn,
42+
).then(resolve, reject);
3943
}, intervalInMs);
4044
}
4145
});

0 commit comments

Comments
 (0)