Skip to content

Commit 51b4c00

Browse files
committed
fix(ts-sdk): change default backoff from 1.0 to 2.0 for exponential retry
Default backoff=1.0 resulted in fixed delay intervals (no exponential backoff). Changed to backoff=2.0 so delays double on each retry, which is the standard exponential backoff behavior. Addresses PR alibaba#492 review feedback.
1 parent 058ef7e commit 51b4c00

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

rock/ts-sdk/src/utils/retry.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ describe('retryAsync', () => {
5353
// Should have waited at least 50ms (0.05s)
5454
expect(elapsed).toBeGreaterThanOrEqual(40);
5555
});
56+
57+
test('should use exponential backoff by default (backoff=2.0)', async () => {
58+
// Test that default backoff is 2.0, not 1.0
59+
// With backoff=2.0 and delaySeconds=0.05:
60+
// - After 1st failure: wait 0.05s
61+
// - After 2nd failure: wait 0.10s (0.05 * 2)
62+
// - Total: ~0.15s (150ms)
63+
// With backoff=1.0:
64+
// - After 1st failure: wait 0.05s
65+
// - After 2nd failure: wait 0.05s (0.05 * 1)
66+
// - Total: ~0.10s (100ms)
67+
const fn = jest
68+
.fn()
69+
.mockRejectedValueOnce(new Error('fail 1'))
70+
.mockRejectedValueOnce(new Error('fail 2'))
71+
.mockRejectedValue(new Error('fail 3'));
72+
73+
const startTime = Date.now();
74+
await retryAsync(fn, {
75+
maxAttempts: 3,
76+
delaySeconds: 0.05,
77+
// NOT passing backoff - testing default value
78+
}).catch(() => {}); // Ignore final error
79+
const elapsed = Date.now() - startTime;
80+
81+
// With exponential backoff (2.0), should wait at least 140ms (0.05 + 0.10 = 0.15s)
82+
// With linear backoff (1.0), would only wait about 100ms (0.05 + 0.05 = 0.10s)
83+
expect(elapsed).toBeGreaterThanOrEqual(140);
84+
});
5685
});
5786

5887
describe('sleep', () => {

rock/ts-sdk/src/utils/retry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function retryAsync<T>(
2222
const {
2323
maxAttempts = 3,
2424
delaySeconds = 1.0,
25-
backoff = 1.0,
25+
backoff = 2.0,
2626
jitter = false,
2727
} = options;
2828

0 commit comments

Comments
 (0)