throttlingRetryPolicy.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. import * as tslib_1 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. export function throttlingRetryPolicy() {
  9. return {
  10. create: function (nextPolicy, options) {
  11. return new ThrottlingRetryPolicy(nextPolicy, options);
  12. }
  13. };
  14. }
  15. /**
  16. * To learn more, please refer to
  17. * https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-request-limits,
  18. * https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits and
  19. * https://docs.microsoft.com/en-us/azure/virtual-machines/troubleshooting/troubleshooting-throttling-errors
  20. */
  21. var ThrottlingRetryPolicy = /** @class */ (function (_super) {
  22. tslib_1.__extends(ThrottlingRetryPolicy, _super);
  23. function ThrottlingRetryPolicy(nextPolicy, options, _handleResponse) {
  24. var _this = _super.call(this, nextPolicy, options) || this;
  25. _this._handleResponse = _handleResponse || _this._defaultResponseHandler;
  26. return _this;
  27. }
  28. ThrottlingRetryPolicy.prototype.sendRequest = function (httpRequest) {
  29. return tslib_1.__awaiter(this, void 0, void 0, function () {
  30. var _this = this;
  31. return tslib_1.__generator(this, function (_a) {
  32. return [2 /*return*/, this._nextPolicy.sendRequest(httpRequest.clone()).then(function (response) {
  33. if (response.status !== StatusCodes.TooManyRequests) {
  34. return response;
  35. }
  36. else {
  37. return _this._handleResponse(httpRequest, response);
  38. }
  39. })];
  40. });
  41. });
  42. };
  43. ThrottlingRetryPolicy.prototype._defaultResponseHandler = function (httpRequest, httpResponse) {
  44. return tslib_1.__awaiter(this, void 0, void 0, function () {
  45. var retryAfterHeader, delayInMs;
  46. var _this = this;
  47. return tslib_1.__generator(this, function (_a) {
  48. retryAfterHeader = httpResponse.headers.get(Constants.HeaderConstants.RETRY_AFTER);
  49. if (retryAfterHeader) {
  50. delayInMs = ThrottlingRetryPolicy.parseRetryAfterHeader(retryAfterHeader);
  51. if (delayInMs) {
  52. return [2 /*return*/, delay(delayInMs).then(function (_) { return _this._nextPolicy.sendRequest(httpRequest); })];
  53. }
  54. }
  55. return [2 /*return*/, httpResponse];
  56. });
  57. });
  58. };
  59. ThrottlingRetryPolicy.parseRetryAfterHeader = function (headerValue) {
  60. var retryAfterInSeconds = Number(headerValue);
  61. if (Number.isNaN(retryAfterInSeconds)) {
  62. return ThrottlingRetryPolicy.parseDateRetryAfterHeader(headerValue);
  63. }
  64. else {
  65. return retryAfterInSeconds * 1000;
  66. }
  67. };
  68. ThrottlingRetryPolicy.parseDateRetryAfterHeader = function (headerValue) {
  69. try {
  70. var now = Date.now();
  71. var date = Date.parse(headerValue);
  72. var diff = date - now;
  73. return Number.isNaN(diff) ? undefined : diff;
  74. }
  75. catch (error) {
  76. return undefined;
  77. }
  78. };
  79. return ThrottlingRetryPolicy;
  80. }(BaseRequestPolicy));
  81. export { ThrottlingRetryPolicy };
  82. //# sourceMappingURL=throttlingRetryPolicy.js.map