-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-retry-policy.ts
162 lines (146 loc) · 3.96 KB
/
simple-retry-policy.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {Duration, seconds} from '@/duration';
import {RetryContext} from '@/retry-context';
import {RetryPolicy} from '@/policy/retry-policy';
import {ErrorConstructor} from '@/policy/retry-policy';
/**
* Basic retry policy: wait constant duration milliseconds in this class.
*/
export class SimpleRetryPolicy implements RetryPolicy {
private errorsNotRetryOn: ErrorConstructor[] = [];
/**
* @param {number} _durationMsec
* @param {number} _maxAttempts
*/
constructor(
private _durationMsec: Duration,
private _maxAttempts: number,
) {}
/**
* Creates a new Simple Retry policy with default settings
* * interval duration ... 1 second
* * max attempts ... 5 times
* @return {RetryPolicy}
*/
static ofDefaults(): RetryPolicy {
return new SimpleRetryPolicy(seconds(1), 4);
}
/**
* Add an error to the list not retrying on it.
* @param {Error} e
*/
notRetryOn(e: ErrorConstructor): void {
this.errorsNotRetryOn.push(e);
}
/**
* Return true if a passed error should not be retryed.
* @param {Error} e
* @return {boolean}
*/
shouldNotRetry(e: Error): boolean {
return this.errorsNotRetryOn.some((v) => e instanceof v);
}
/**
* @param {SimpleRetryPolicy} retryPolicy
* @return {boolean}
*/
equals(retryPolicy: SimpleRetryPolicy): boolean {
return this._maxAttempts === retryPolicy._maxAttempts &&
this._durationMsec === retryPolicy.durationMsec;
}
/**
* @return {string}
*/
toString(): string {
return `SimpleRetryPolicy(
durationMsec=${this._durationMsec}), maxAttemps=${this._maxAttempts}
)`;
}
/**
*
* @param {RetryContext} retryContext
* @return {boolean} if a passed context can continue retrying
*/
canRetry(retryContext: RetryContext): boolean {
return !retryContext.lastError ||
retryContext.attemptsCount < this._maxAttempts;
}
/**
*
* @param {RetryContext} retryContext
* @return {number} retrun next delay milliseconds
*/
getNextDelay(): Duration {
return this._durationMsec;
}
/**
*/
get maxAttempts(): number {
return this._maxAttempts;
}
/**
*/
get durationMsec(): Duration {
return this._durationMsec;
}
}
export namespace SimpleRetryPolicy {
export const newBuilder = (): SimpleRetryPolicy.Builder => {
return new SimpleRetryPolicy.Builder();
};
/**
* Builder for {@link SimpleRetryPolicy}
*/
export class Builder {
private _duration?: Duration;
private _maxAttempts?: number;
private errorsNotRetryOn: ErrorConstructor[] = [];
/**
* Set duration of interval to builder
* @param {Duration} duration
* @return {Builder}
*/
duration(duration: Duration): Builder {
this._duration = duration;
return this;
}
/**
* Set max attempts to builder
* @param {number} attempts
* @return {Builder}
*/
maxAttempts(attempts: number): Builder {
this._maxAttempts = attempts;
return this;
}
/**
* Set an error not retry on to builder
* @param {ErrorConstructor} e
* @return {Builder}
*/
notRetryOn(e: ErrorConstructor): Builder {
this.notRetrysOn([e]);
return this;
}
/**
* Set an error not retry on to builder
* @param {ErrorConstructor[]} constructors
* @return {Builder}
*/
notRetrysOn(constructors: ErrorConstructor[]): Builder {
constructors.forEach((c) => this.errorsNotRetryOn.push(c));
return this;
}
/**
* Create new {@link SimpleRetryPolicy} with properties.
* Properties that is not set will be default settings.
* @return {SimpleRetryPolicy}
*/
build(): SimpleRetryPolicy {
const duration = this._duration ? this._duration : seconds(1);
const maxAttempts = this._maxAttempts ? this._maxAttempts : 4;
const policy = new SimpleRetryPolicy(duration, maxAttempts);
this.errorsNotRetryOn.forEach((e) => policy.notRetryOn(e));
return policy;
}
}
}