Skip to content

Commit ac830e8

Browse files
authored
fix: max backoff should be 32 seconds (#792)
1 parent 648e909 commit ac830e8

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

handwritten/spanner/src/transaction-runner.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ export abstract class Runner<T> {
152152
return secondsInMs + nanosInMs;
153153
}
154154

155-
return Math.pow(2, this.attempts) * 1000 + Math.floor(Math.random() * 1000);
155+
// Max backoff should be 32 seconds.
156+
return (
157+
Math.pow(2, Math.min(this.attempts, 5)) * 1000 +
158+
Math.floor(Math.random() * 1000)
159+
);
156160
}
157161
/**
158162
* Retrieves a transaction to run against.

handwritten/spanner/test/transaction-runner.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,22 @@ describe('TransactionRunner', () => {
163163
it('should create a backoff when `retryInfo` is absent', () => {
164164
const random = Math.random();
165165

166-
runner.attempts = 5;
166+
runner.attempts = 3;
167+
sandbox.stub(global.Math, 'random').returns(random);
168+
169+
const badError = new Error('err') as ServiceError;
170+
badError.metadata = new Metadata();
171+
172+
const expectedDelay = Math.pow(2, 3) * 1000 + Math.floor(random * 1000);
173+
const delay = runner.getNextDelay(badError);
174+
175+
assert.strictEqual(delay, expectedDelay);
176+
});
177+
178+
it('should use a backoff of max 32 seconds when `retryInfo` is absent', () => {
179+
const random = Math.random();
180+
181+
runner.attempts = 10;
167182
sandbox.stub(global.Math, 'random').returns(random);
168183

169184
const badError = new Error('err') as ServiceError;

0 commit comments

Comments
 (0)