(callFn, retryOptions)
| 15 | err && (err.name === 'SlowDown' || err.$metadata?.httpStatusCode === 429); |
| 16 | |
| 17 | async function gcpRetryCall(callFn, retryOptions) { |
| 18 | const { |
| 19 | // 6 attempts with exponential backoff gives up to ~63s of total wait. |
| 20 | maxAttempts = 6, |
| 21 | shouldRetry = defaultShouldRetry, |
| 22 | getDelayMs = attempt => Math.pow(2, attempt) * 1000, |
| 23 | } = retryOptions || {}; |
| 24 | |
| 25 | let lastError; |
| 26 | for (let attempt = 0; attempt < maxAttempts; attempt++) { |
| 27 | try { |
| 28 | return await callFn(); |
| 29 | } catch (err) { |
| 30 | lastError = err; |
| 31 | if (!shouldRetry(err, attempt) || attempt === maxAttempts - 1) { |
| 32 | throw err; |
| 33 | } |
| 34 | const delay = getDelayMs(attempt); |
| 35 | process.stdout.write( |
| 36 | 'Retryable error from GCP, retrying in ' + |
| 37 | `${delay}ms (attempt ${attempt + 1}): ${err}\n`); |
| 38 | |
| 39 | await new Promise(resolve => setTimeout(resolve, delay)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | throw lastError; |
| 44 | } |
| 45 | |
| 46 | async function gcpRetry(gcpClient, command, retryOptions, cb) { |
| 47 | if (cb) { |
no test coverage detected