Skip to content

Commit 47448e9

Browse files
committed
test(remote-config): retry rate-limited cloud helper a few times
1 parent d4cfec7 commit 47448e9

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
const { getE2eTestProject } = require('../../app/e2e/helpers');
22

33
exports.updateTemplate = async function updateTemplate(operations) {
4-
// console.error('remoteConfig::helpers::updateTemplate');
5-
const response = await fetch(
6-
'https://us-central1-' +
7-
getE2eTestProject() +
8-
'.cloudfunctions.net/testFunctionRemoteConfigUpdateV2',
9-
{
10-
method: 'post',
11-
body: JSON.stringify({ data: operations }),
12-
headers: { 'Content-Type': 'application/json' },
13-
},
14-
);
15-
const result = await response.json();
4+
let doc = undefined;
5+
let retries = 0;
6+
let maxRetries = 5;
7+
// We handle 429 errors in a retry loop
8+
while ((doc === undefined || doc.status === 429) && retries < maxRetries) {
9+
doc = await fetch(
10+
// 'https://httpbin.org/status/429',
11+
'https://us-central1-' +
12+
getE2eTestProject() +
13+
'.cloudfunctions.net/testFunctionRemoteConfigUpdateV2',
14+
{
15+
method: 'post',
16+
body: JSON.stringify({ data: operations }),
17+
headers: { 'Content-Type': 'application/json' },
18+
},
19+
);
20+
if (doc.status === 429) {
21+
// We have been delayed by concurrency limits or rate limits
22+
// We'll sleep for a little bit then try again.
23+
const delayRequired = 10;
24+
await new Promise(r => setTimeout(r, delayRequired * 1000));
25+
}
26+
retries++;
27+
}
28+
29+
// did we eventually have success? If not, error.
30+
if (retries === maxRetries && doc.status !== 200) {
31+
throw new Error('Unable to execute cloud remote config helper function');
32+
}
33+
const result = await doc.json();
1634
// console.error('received: ' + JSON.stringify(result));
1735
return result;
1836
};

0 commit comments

Comments
 (0)