Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

RetryPolicy.ts

41 lines1.0 KB

1import { NonRetriableError } from '../types'
2import { BackoffCalculator } from './BackoffCalculator'
3import type { RetryPolicy } from '../types'
4​
5export class DefaultRetryPolicy implements RetryPolicy {
6 private backoffCalculator: BackoffCalculator
7 private maxRetries: number
8​
9 constructor(maxRetries = Number.POSITIVE_INFINITY, jitter = true) {
10 this.backoffCalculator = new BackoffCalculator(jitter)
11 this.maxRetries = maxRetries
12 }
13​
14 calculateDelay(retryCount: number): number {
15 return this.backoffCalculator.calculate(retryCount)
16 }
17​
18 shouldRetry(error: Error, retryCount: number): boolean {
19 if (retryCount >= this.maxRetries) {
20 return false
21 }
22​
23 if (error instanceof NonRetriableError) {
24 return false
25 }
26​
27 if (error.name === `AbortError`) {
28 return false
29 }
30​
31 if (error.message.includes(`401`) || error.message.includes(`403`)) {
32 return false
33 }
34​
35 if (error.message.includes(`422`) || error.message.includes(`400`)) {
36 return false
37 }
38​
39 return true
40 }