-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretries.ts
182 lines (156 loc) · 4.21 KB
/
retries.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
*/
import { AxiosError, AxiosResponse } from "axios";
export class BackoffStrategy {
initialInterval: number;
maxInterval: number;
exponent: number;
maxElapsedTime: number;
constructor(
initialInterval: number,
maxInterval: number,
exponent: number,
maxElapsedTime: number,
) {
this.initialInterval = initialInterval;
this.maxInterval = maxInterval;
this.exponent = exponent;
this.maxElapsedTime = maxElapsedTime;
}
}
export class RetryConfig {
strategy: "backoff" | "none";
backoff?: BackoffStrategy;
retryConnectionErrors: boolean;
constructor(
strategy: "backoff" | "none",
backoff?: BackoffStrategy,
retryConnectionErrors = true,
) {
this.strategy = strategy;
this.backoff = backoff;
this.retryConnectionErrors = retryConnectionErrors;
}
}
export class Retries {
config: RetryConfig;
statusCodes: string[];
constructor(config: RetryConfig, statusCodes: string[]) {
this.config = config;
this.statusCodes = statusCodes;
}
}
class PermanentError extends Error {
inner: unknown;
constructor(inner: unknown) {
super("Permanent error");
this.inner = inner;
Object.setPrototypeOf(this, PermanentError.prototype);
}
}
class TemporaryError extends Error {
res: AxiosResponse<any, any>;
constructor(res: AxiosResponse<any, any>) {
super("Temporary error");
this.res = res;
Object.setPrototypeOf(this, TemporaryError.prototype);
}
}
export async function Retry(
fn: () => Promise<AxiosResponse<any, any>>,
retries: Retries,
): Promise<AxiosResponse<any, any>> {
switch (retries.config.strategy) {
case "backoff":
return retryBackoff(
async () => {
try {
const res = await fn();
if (isRetryableResponse(res, retries.statusCodes)) {
throw new TemporaryError(res);
}
return res;
} catch (err) {
if (err instanceof AxiosError) {
if (err.response) {
if (isRetryableResponse(err.response, retries.statusCodes)) {
throw err;
}
throw new PermanentError(err);
} else if (err.request) {
throw err;
} else {
throw new PermanentError(err);
}
} else if (err instanceof TemporaryError) {
throw err;
}
throw new PermanentError(err);
}
},
retries.config.backoff?.initialInterval ?? 500,
retries.config.backoff?.maxInterval ?? 60000,
retries.config.backoff?.exponent ?? 1.5,
retries.config.backoff?.maxElapsedTime ?? 3600000,
);
default:
return await fn();
}
}
function isRetryableResponse(
res: AxiosResponse<any, any>,
statusCodes: string[],
): boolean {
for (const code of statusCodes) {
if (code.toUpperCase().includes("X")) {
const codeRange = parseInt(code[0]);
if (isNaN(codeRange)) {
throw new Error("Invalid status code range");
}
const s = res.status / 100;
if (s >= codeRange && s < codeRange + 1) {
return true;
}
} else if (res.status == parseInt(code)) {
return true;
}
}
return false;
}
async function retryBackoff(
fn: () => Promise<AxiosResponse<any, any>>,
initialInterval: number,
maxInterval: number,
exponent: number,
maxElapsedTime: number,
): Promise<AxiosResponse<any, any>> {
const start = Date.now();
let x = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
try {
return await fn();
} catch (err) {
if (err instanceof PermanentError) {
throw err.inner;
}
const now = Date.now();
if (now - start > maxElapsedTime) {
if (err instanceof TemporaryError) {
return err.res;
}
throw err;
}
const d = Math.min(
initialInterval * Math.pow(x, exponent) + Math.random() * 1000,
maxInterval,
);
await delay(d);
x++;
}
}
}
async function delay(delay: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, delay));
}