|
1 | 1 | const { getE2eTestProject } = require('../../app/e2e/helpers'); |
2 | 2 |
|
3 | 3 | 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(); |
16 | 34 | // console.error('received: ' + JSON.stringify(result)); |
17 | 35 | return result; |
18 | 36 | }; |
0 commit comments