1 |
|
2 |
|
3 | import { __awaiter, __extends, __generator } from "tslib";
|
4 | import { BaseRequestPolicy, } from "./requestPolicy";
|
5 | import { Constants } from "../util/constants";
|
6 | import { delay } from "../util/utils";
|
7 | var StatusCodes = Constants.HttpConstants.StatusCodes;
|
8 | var DEFAULT_RETRY_COUNT = 3;
|
9 | export function throttlingRetryPolicy(maxRetries) {
|
10 | if (maxRetries === void 0) { maxRetries = DEFAULT_RETRY_COUNT; }
|
11 | return {
|
12 | create: function (nextPolicy, options) {
|
13 | return new ThrottlingRetryPolicy(nextPolicy, options, maxRetries);
|
14 | },
|
15 | };
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var ThrottlingRetryPolicy = (function (_super) {
|
24 | __extends(ThrottlingRetryPolicy, _super);
|
25 | function ThrottlingRetryPolicy(nextPolicy, options, retryLimit) {
|
26 | var _this = _super.call(this, nextPolicy, options) || this;
|
27 | _this.retryLimit = retryLimit;
|
28 | return _this;
|
29 | }
|
30 | ThrottlingRetryPolicy.prototype.sendRequest = function (httpRequest) {
|
31 | return __awaiter(this, void 0, void 0, function () {
|
32 | var _this = this;
|
33 | return __generator(this, function (_a) {
|
34 | return [2 , this._nextPolicy.sendRequest(httpRequest.clone()).then(function (response) {
|
35 | return _this.retry(httpRequest, response, 0);
|
36 | })];
|
37 | });
|
38 | });
|
39 | };
|
40 | ThrottlingRetryPolicy.prototype.retry = function (httpRequest, httpResponse, retryCount) {
|
41 | return __awaiter(this, void 0, void 0, function () {
|
42 | var retryAfterHeader, delayInMs, res;
|
43 | return __generator(this, function (_a) {
|
44 | switch (_a.label) {
|
45 | case 0:
|
46 | if (httpResponse.status !== StatusCodes.TooManyRequests) {
|
47 | return [2 , httpResponse];
|
48 | }
|
49 | retryAfterHeader = httpResponse.headers.get(Constants.HeaderConstants.RETRY_AFTER);
|
50 | if (!(retryAfterHeader && retryCount < this.retryLimit)) return [3 , 3];
|
51 | delayInMs = ThrottlingRetryPolicy.parseRetryAfterHeader(retryAfterHeader);
|
52 | if (!delayInMs) return [3 , 3];
|
53 | return [4 , delay(delayInMs)];
|
54 | case 1:
|
55 | _a.sent();
|
56 | return [4 , this._nextPolicy.sendRequest(httpRequest)];
|
57 | case 2:
|
58 | res = _a.sent();
|
59 | return [2 , this.retry(httpRequest, res, retryCount + 1)];
|
60 | case 3: return [2 , httpResponse];
|
61 | }
|
62 | });
|
63 | });
|
64 | };
|
65 | ThrottlingRetryPolicy.parseRetryAfterHeader = function (headerValue) {
|
66 | var retryAfterInSeconds = Number(headerValue);
|
67 | if (Number.isNaN(retryAfterInSeconds)) {
|
68 | return ThrottlingRetryPolicy.parseDateRetryAfterHeader(headerValue);
|
69 | }
|
70 | else {
|
71 | return retryAfterInSeconds * 1000;
|
72 | }
|
73 | };
|
74 | ThrottlingRetryPolicy.parseDateRetryAfterHeader = function (headerValue) {
|
75 | try {
|
76 | var now = Date.now();
|
77 | var date = Date.parse(headerValue);
|
78 | var diff = date - now;
|
79 | return Number.isNaN(diff) ? undefined : diff;
|
80 | }
|
81 | catch (error) {
|
82 | return undefined;
|
83 | }
|
84 | };
|
85 | return ThrottlingRetryPolicy;
|
86 | }(BaseRequestPolicy));
|
87 | export { ThrottlingRetryPolicy };
|
88 |
|
\ | No newline at end of file |