-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefault.ts
99 lines (92 loc) · 2.85 KB
/
Default.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { RetryPolicy } from '../core.js'
import { join } from '../core.js'
import { Breaker } from './Breaker.js'
import { FastTrack } from './FastTrack.js'
import { PollyJitter } from './Jitter.js'
import { Logger } from './Logger.js'
import { Timeout } from './Timeout.js'
export type DefaultOptions = {
/**
* If true, runs the first re-attempt immediately, skipping the initial delay.
* @default false
*/
fastTrack?: boolean | undefined
/**
* The initial delay in milliseconds. Defaults to 150ms.
* @default 150
*/
initialDelay?: number | undefined
/**
* Logger function to use when giving up on retries. Passing `false` disables this type of
* logging. Defaults to `console.error`.
* @default console.error
*/
logError?: typeof console.error | boolean | undefined
/**
* Logger function to use when retrying. Passing `false` disables this type of logging. Defaults
* to `console.warn`.
* @default console.warn
*/
logWarn?: typeof console.warn | boolean | undefined
/**
* The maximum number of attempts to make. Defaults to 10.
* @default 10
*/
maxAttempts?: number | undefined
/**
* The maximum delay between attempts in milliseconds. Defaults to 30 seconds.
* @default 30000
*/
maxDelay?: number | undefined
/**
* The time in milliseconds after which to give up. Defaults to 30 seconds.
* @default 30000
*/
timeout?: number | undefined
/**
* Convenience property to chain another policy after the default ones. This
* is useful when you want to add custom logic on top of the default policies.
* This is equivalent to `policy = join(Default(), next)`.
*/
next?: RetryPolicy
}
const noop = () => {}
const loggerOrNoop = (logger: typeof noop | boolean | undefined) => {
if (typeof logger === 'function') {
return logger
}
if (logger === true || logger === undefined) {
return undefined
}
return noop
}
/**
* The default retry policy used by `retryyy`. It includes:
*
* - Logger: logs retry attempts and failed operations to the console.
* - Timeout: give up after a certain amount of time has passed.
* - Breaker: stop retrying after a certain number of attempts.
* - FastTrack: run the first re-attempt immediately if `fastTrack` is `true`.
* - PollyJitter: advanced exponential backoff strategy with jitter.
*/
export const Default = (options?: DefaultOptions): RetryPolicy =>
join(
[
Logger({
error: loggerOrNoop(options?.logError),
warn: loggerOrNoop(options?.logWarn),
}),
Timeout({
after: options?.timeout,
}),
Breaker({
max: options?.maxAttempts,
}),
options?.fastTrack ? FastTrack() : undefined,
PollyJitter({
initial: options?.initialDelay,
max: options?.maxDelay,
}),
options?.next,
].filter(Boolean) as [RetryPolicy, RetryPolicy],
)