Skip to content

Commit 74ed43d

Browse files
author
Brian Chen
authored
Standardize transaction retries to attempts (#5075)
1 parent 5d31e21 commit 74ed43d

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

packages/firestore/src/core/transaction_runner.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ import { isNullOrUndefined } from '../util/types';
2525

2626
import { Transaction } from './transaction';
2727

28-
const RETRY_COUNT = 5;
28+
export const DEFAULT_MAX_ATTEMPTS_COUNT = 5;
2929

3030
/**
3131
* TransactionRunner encapsulates the logic needed to run and retry transactions
3232
* with backoff.
3333
*/
3434
export class TransactionRunner<T> {
35-
private retries = RETRY_COUNT;
35+
private attemptsRemaining = DEFAULT_MAX_ATTEMPTS_COUNT;
3636
private backoff: ExponentialBackoff;
3737

3838
constructor(
@@ -49,6 +49,7 @@ export class TransactionRunner<T> {
4949

5050
/** Runs the transaction and sets the result on deferred. */
5151
run(): void {
52+
this.attemptsRemaining -= 1;
5253
this.runWithBackOff();
5354
}
5455

@@ -99,8 +100,8 @@ export class TransactionRunner<T> {
99100
}
100101

101102
private handleTransactionError(error: Error): void {
102-
if (this.retries > 0 && this.isRetryableTransactionError(error)) {
103-
this.retries -= 1;
103+
if (this.attemptsRemaining > 0 && this.isRetryableTransactionError(error)) {
104+
this.attemptsRemaining -= 1;
104105
this.asyncQueue.enqueueAndForget(() => {
105106
this.runWithBackOff();
106107
return Promise.resolve();

packages/firestore/test/integration/api_internal/transaction.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import * as firestore from '@firebase/firestore-types';
1919
import { expect } from 'chai';
2020

21+
import { DEFAULT_MAX_ATTEMPTS_COUNT } from '../../../src/core/transaction_runner';
2122
import { TimerId } from '../../../src/util/async_queue';
2223
import { Deferred } from '../../util/promise';
2324
import * as integrationHelpers from '../util/helpers';
@@ -183,6 +184,7 @@ apiDescribe(
183184
.then(() => doc.get())
184185
.then(snapshot => {
185186
expect(snapshot.data()!['count']).to.equal(1234 + counter);
187+
expect(counter).to.equal(DEFAULT_MAX_ATTEMPTS_COUNT);
186188
});
187189
});
188190
});

packages/firestore/test/integration/util/internal_helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export function getDefaultDatabaseInfo(): DatabaseInfo {
4949
DEFAULT_SETTINGS.host!,
5050
!!DEFAULT_SETTINGS.ssl,
5151
!!DEFAULT_SETTINGS.experimentalForceLongPolling,
52-
!!DEFAULT_SETTINGS.experimentalAutoDetectLongPolling
52+
!!DEFAULT_SETTINGS.experimentalAutoDetectLongPolling,
53+
/*use FetchStreams= */ false
5354
);
5455
}
5556

0 commit comments

Comments
 (0)