UNPKG

@embrace-io/web-sdk

Version:
46 lines (45 loc) 1.93 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_utils_PerformanceManager_OTelPerformanceManager = require("../../utils/PerformanceManager/OTelPerformanceManager.cjs"); const require_transport_RetryingTransport_constants = require("./constants.cjs"); //#region src/transport/RetryingTransport/RetryingTransport.ts /** * Get a pseudo-random jitter that falls in the range of [-JITTER, +JITTER] */ const getJitter = () => Math.random() * 40 - 20; var RetryingTransport = class { _transport; _perf; constructor(_transport, _perf = new require_utils_PerformanceManager_OTelPerformanceManager.OTelPerformanceManager()) { this._transport = _transport; this._perf = _perf; } async send(data, timeoutMillis) { const deadline = this._perf.getNowMillis() + timeoutMillis; let result = await this._transport.send(data, timeoutMillis); let attempts = 5; let nextBackoff = require_transport_RetryingTransport_constants.INITIAL_BACKOFF; while (result.status === "retryable" && attempts > 0) { attempts--; const backoff = Math.max(Math.min(nextBackoff, require_transport_RetryingTransport_constants.MAX_BACKOFF) + getJitter(), 0); nextBackoff = nextBackoff * require_transport_RetryingTransport_constants.BACKOFF_MULTIPLIER; const retryInMillis = result.retryInMillis ?? backoff; const remainingTimeoutMillis = deadline - this._perf.getNowMillis(); if (retryInMillis > remainingTimeoutMillis) return result; result = await this._retry(data, remainingTimeoutMillis, retryInMillis); } return result; } shutdown() { this._transport.shutdown(); } _retry(data, timeoutMillis, inMillis) { return new Promise((resolve, reject) => { setTimeout(() => { this._transport.send(data, timeoutMillis).then(resolve, reject); }, inMillis); }); } }; //#endregion exports.RetryingTransport = RetryingTransport; //# sourceMappingURL=RetryingTransport.cjs.map