* Retry a KV operation with exponential backoff * @param {() => Promise<any>} operation - The async operation to retry * @param {string} operationName - Name of the operation for logging * @param {number} maxRetries - Maximum number of retries (default: 3) * @returns {Promise<any>} The result of
(operation, operationName, maxRetries = 3)
| 76 | * @returns {Promise<any>} The result of the operation |
| 77 | */ |
| 78 | async function retryKVOperation(operation, operationName, maxRetries = 3) { |
| 79 | let lastError |
| 80 | let retries = maxRetries |
| 81 | |
| 82 | while (retries > 0) { |
| 83 | try { |
| 84 | return await operation() |
| 85 | } catch (err) { |
| 86 | lastError = err |
| 87 | retries-- |
| 88 | if (retries > 0) { |
| 89 | const delay = (maxRetries - retries + 1) * 5 // 5s, 10s, 15s backoff |
| 90 | console.log( |
| 91 | `KV ${operationName} failed, retrying in ${delay}s. Error:`, |
| 92 | err.message |
| 93 | ) |
| 94 | await new Promise((resolve) => setTimeout(resolve, delay * 1000)) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | throw new Error( |
| 100 | `Failed to ${operationName} after ${maxRetries} retries: ${lastError?.message}` |
| 101 | ) |
| 102 | } |
| 103 | |
| 104 | const testFilters = { |
| 105 | development: new RegExp('^(test/(development|e2e))'), |
no test coverage detected