Skip to content

Commit eb93c26

Browse files
authored
fix: enforce limit cap for exponential retry strategy to prevent overflow (#201)
fixes #199 - Moved limit validation into the exponential strategy branch - Updated tests to verify behavior with high limits and enforce limit <= 50 for exponential - Removed redundant limit check outside exponential context to clarify validation scope
1 parent 84091aa commit eb93c26

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎pkgs/edge-worker/src/queue/validateRetryConfig.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ export function validateRetryConfig(config: RetryConfig): void {
2424
if (config.limit < 0) {
2525
throw new Error('limit must be greater than or equal to 0');
2626
}
27-
// Prevent overflow in Math.pow(2, limit-1)
28-
if (config.limit > 50) {
29-
throw new Error('limit must not exceed 50');
30-
}
3127

3228
// Validate baseDelay
3329
if (!Number.isInteger(config.baseDelay)) {
@@ -48,6 +44,11 @@ export function validateRetryConfig(config: RetryConfig): void {
4844
throw new Error('maxDelay is only valid for exponential strategy');
4945
}
5046
} else if (config.strategy === 'exponential') {
47+
// Prevent overflow in Math.pow(2, limit-1)
48+
if (config.limit > 50) {
49+
throw new Error('For exponential strategy, limit must not exceed 50 to prevent calculation overflow');
50+
}
51+
5152
// Validate maxDelay if provided
5253
if (config.maxDelay !== undefined) {
5354
if (!Number.isInteger(config.maxDelay)) {

‎pkgs/edge-worker/tests/unit/validateRetryConfig.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,34 @@ Deno.test('validateRetryConfig - retry config validation', async (t) => {
165165
baseDelay: 3,
166166
});
167167
});
168+
169+
await t.step('should allow fixed strategy with very high retry limits', () => {
170+
// Should not throw - fixed strategy has no mathematical overflow issues
171+
validateRetryConfig({
172+
strategy: 'fixed',
173+
limit: 100000,
174+
baseDelay: 10,
175+
});
176+
});
177+
178+
await t.step('should throw for exponential strategy with limit > 50', () => {
179+
assertThrows(
180+
() => validateRetryConfig({
181+
strategy: 'exponential',
182+
limit: 51,
183+
baseDelay: 1,
184+
}),
185+
Error,
186+
'For exponential strategy, limit must not exceed 50 to prevent calculation overflow'
187+
);
188+
});
189+
190+
await t.step('should allow exponential strategy with limit = 50', () => {
191+
// Should not throw - exactly at the limit
192+
validateRetryConfig({
193+
strategy: 'exponential',
194+
limit: 50,
195+
baseDelay: 1,
196+
});
197+
});
168198
});

0 commit comments

Comments
 (0)