Skip to content

Commit fbf2c6b

Browse files
DanilTezinpolRk
authored andcommitted
Extract backoff constants and add changeset
1 parent 5f42319 commit fbf2c6b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ydbjs/retry': patch
3+
---
4+
5+
Fix TimeoutOverflowWarning caused by unbounded exponential backoff. Replace `exponential(ms)` with `backoff(base, max)` which caps the delay via `Math.min(2^attempt * base, max)`, preventing `Infinity` from being passed to `setTimeout`.

packages/retry/src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export * from './config.js'
1414
export * from './context.js'
1515
export * as strategies from './strategy.js'
1616

17+
const BACKOFF_OVERLOAD_BASE_MS = 1000
18+
const BACKOFF_OVERLOAD_MAX_MS = 60_000
19+
const BACKOFF_DEFAULT_BASE_MS = 10
20+
const BACKOFF_DEFAULT_MAX_MS = 30_000
21+
1722
let dbg = loggers.retry
1823

1924
export async function retry<R>(
@@ -159,14 +164,14 @@ export const defaultRetryConfig: RetryConfig = {
159164
}
160165

161166
if (ctx.error instanceof YDBError && ctx.error.code === StatusIds_StatusCode.OVERLOADED) {
162-
return backoff(1000, 60_000)(ctx, cfg)
167+
return backoff(BACKOFF_OVERLOAD_BASE_MS, BACKOFF_OVERLOAD_MAX_MS)(ctx, cfg)
163168
}
164169

165170
if (ctx.error instanceof ClientError && ctx.error.code === Status.RESOURCE_EXHAUSTED) {
166-
return backoff(1000, 60_000)(ctx, cfg)
171+
return backoff(BACKOFF_OVERLOAD_BASE_MS, BACKOFF_OVERLOAD_MAX_MS)(ctx, cfg)
167172
}
168173

169-
return backoff(10, 30_000)(ctx, cfg)
174+
return backoff(BACKOFF_DEFAULT_BASE_MS, BACKOFF_DEFAULT_MAX_MS)(ctx, cfg)
170175
},
171176
}
172177

@@ -185,7 +190,7 @@ export const defaultStreamRetryConfig: RetryConfig = {
185190
ctx.error instanceof ClientError &&
186191
(ctx.error.code === Status.CANCELLED || ctx.error.code === Status.UNAVAILABLE)
187192
) {
188-
return backoff(10, 30_000)(ctx, cfg)
193+
return backoff(BACKOFF_DEFAULT_BASE_MS, BACKOFF_DEFAULT_MAX_MS)(ctx, cfg)
189194
}
190195

191196
return (defaultRetryConfig.strategy as RetryStrategy)(ctx, cfg)

0 commit comments

Comments
 (0)