1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import { Context } from "../IContext";
|
13 | import { FetchOptions } from "../IFetchOptions";
|
14 | import { RequestMethod } from "../RequestMethod";
|
15 | import { Middleware } from "./IMiddleware";
|
16 | import { MiddlewareControl } from "./MiddlewareControl";
|
17 | import { getRequestHeader, setRequestHeader } from "./MiddlewareUtil";
|
18 | import { RetryHandlerOptions } from "./options/RetryHandlerOptions";
|
19 | import { FeatureUsageFlag, TelemetryHandlerOptions } from "./options/TelemetryHandlerOptions";
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export class RetryHandler implements Middleware {
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | private static RETRY_STATUS_CODES: number[] = [
|
33 | 429,
|
34 | 503,
|
35 | 504,
|
36 | ];
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 | private static RETRY_ATTEMPT_HEADER = "Retry-Attempt";
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | private static RETRY_AFTER_HEADER = "Retry-After";
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 | private nextMiddleware: Middleware;
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 | private options: RetryHandlerOptions;
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | public constructor(options: RetryHandlerOptions = new RetryHandlerOptions()) {
|
72 | this.options = options;
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | private isRetry(response: Response): boolean {
|
83 | return RetryHandler.RETRY_STATUS_CODES.indexOf(response.status) !== -1;
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | private isBuffered(request: RequestInfo, options: FetchOptions | undefined): boolean {
|
94 | const method = typeof request === "string" ? options.method : (request as Request).method;
|
95 | const isPutPatchOrPost: boolean = method === RequestMethod.PUT || method === RequestMethod.PATCH || method === RequestMethod.POST;
|
96 | if (isPutPatchOrPost) {
|
97 | const isStream = getRequestHeader(request, options, "Content-Type") === "application/octet-stream";
|
98 | if (isStream) {
|
99 | return false;
|
100 | }
|
101 | }
|
102 | return true;
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | private getDelay(response: Response, retryAttempts: number, delay: number): number {
|
114 | const getRandomness = () => Number(Math.random().toFixed(3));
|
115 | const retryAfter = response.headers !== undefined ? response.headers.get(RetryHandler.RETRY_AFTER_HEADER) : null;
|
116 | let newDelay: number;
|
117 | if (retryAfter !== null) {
|
118 | if (Number.isNaN(Number(retryAfter))) {
|
119 | newDelay = Math.round((new Date(retryAfter).getTime() - Date.now()) / 1000);
|
120 | } else {
|
121 | newDelay = Number(retryAfter);
|
122 | }
|
123 | } else {
|
124 |
|
125 | newDelay = retryAttempts >= 2 ? this.getExponentialBackOffTime(retryAttempts) + delay + getRandomness() : delay + getRandomness();
|
126 | }
|
127 | return Math.min(newDelay, this.options.getMaxDelay() + getRandomness());
|
128 | }
|
129 |
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | private getExponentialBackOffTime(attempts: number): number {
|
137 | return Math.round((1 / 2) * (2 ** attempts - 1));
|
138 | }
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | private async sleep(delaySeconds: number): Promise<void> {
|
148 | const delayMilliseconds = delaySeconds * 1000;
|
149 | return new Promise((resolve) => setTimeout(resolve, delayMilliseconds));
|
150 | }
|
151 |
|
152 | private getOptions(context: Context): RetryHandlerOptions {
|
153 | let options: RetryHandlerOptions;
|
154 | if (context.middlewareControl instanceof MiddlewareControl) {
|
155 | options = context.middlewareControl.getMiddlewareOptions(this.options.constructor) as RetryHandlerOptions;
|
156 | }
|
157 | if (typeof options === "undefined") {
|
158 | options = Object.assign(new RetryHandlerOptions(), this.options);
|
159 | }
|
160 | return options;
|
161 | }
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | private async executeWithRetry(context: Context, retryAttempts: number, options: RetryHandlerOptions): Promise<void> {
|
173 | await this.nextMiddleware.execute(context);
|
174 | if (retryAttempts < options.maxRetries && this.isRetry(context.response) && this.isBuffered(context.request, context.options) && options.shouldRetry(options.delay, retryAttempts, context.request, context.options, context.response)) {
|
175 | ++retryAttempts;
|
176 | setRequestHeader(context.request, context.options, RetryHandler.RETRY_ATTEMPT_HEADER, retryAttempts.toString());
|
177 | const delay = this.getDelay(context.response, retryAttempts, options.delay);
|
178 | await this.sleep(delay);
|
179 | return await this.executeWithRetry(context, retryAttempts, options);
|
180 | } else {
|
181 | return;
|
182 | }
|
183 | }
|
184 |
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | public async execute(context: Context): Promise<void> {
|
193 | const retryAttempts = 0;
|
194 | const options: RetryHandlerOptions = this.getOptions(context);
|
195 | TelemetryHandlerOptions.updateFeatureUsageFlag(context, FeatureUsageFlag.RETRY_HANDLER_ENABLED);
|
196 | return await this.executeWithRetry(context, retryAttempts, options);
|
197 | }
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | public setNext(next: Middleware): void {
|
206 | this.nextMiddleware = next;
|
207 | }
|
208 | }
|