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