Skip to main content
Glama
retry-queue.js7.57 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import { isNumber, isUndefined } from './utils/type-utils'; import { logger } from './utils/logger'; import { window } from './utils/globals'; import { extendURLParams } from './request'; import { addEventListener } from './utils'; var thirtyMinutes = 30 * 60 * 1000; /** * Generates a jitter-ed exponential backoff delay in milliseconds * * The base value is 6 seconds, which is doubled with each retry * up to the maximum of 30 minutes * * Each value then has +/- 50% jitter * * Giving a range of 6 seconds up to 45 minutes */ export function pickNextRetryDelay(retriesPerformedSoFar) { var rawBackoffTime = 3000 * Math.pow(2, retriesPerformedSoFar); var minBackoff = rawBackoffTime / 2; var cappedBackoffTime = Math.min(thirtyMinutes, rawBackoffTime); var jitterFraction = Math.random() - 0.5; // A random number between -0.5 and 0.5 var jitter = jitterFraction * (cappedBackoffTime - minBackoff); return Math.ceil(cappedBackoffTime + jitter); } var RetryQueue = /** @class */ (function () { function RetryQueue(instance) { var _this = this; this.instance = instance; this.isPolling = false; // flag to continue to recursively poll or not this.pollIntervalMs = 3000; this.queue = []; this.queue = []; this.areWeOnline = true; if (!isUndefined(window) && 'onLine' in window.navigator) { this.areWeOnline = window.navigator.onLine; addEventListener(window, 'online', function () { _this.areWeOnline = true; _this.flush(); }); addEventListener(window, 'offline', function () { _this.areWeOnline = false; }); } } RetryQueue.prototype.retriableRequest = function (_a) { var _this = this; var retriesPerformedSoFar = _a.retriesPerformedSoFar, options = __rest(_a, ["retriesPerformedSoFar"]); if (isNumber(retriesPerformedSoFar) && retriesPerformedSoFar > 0) { options.url = extendURLParams(options.url, { retry_count: retriesPerformedSoFar }); } this.instance._send_request(__assign(__assign({}, options), { callback: function (response) { var _a; if (response.statusCode !== 200 && (response.statusCode < 400 || response.statusCode >= 500)) { if ((retriesPerformedSoFar !== null && retriesPerformedSoFar !== void 0 ? retriesPerformedSoFar : 0) < 10) { _this.enqueue(__assign({ retriesPerformedSoFar: retriesPerformedSoFar }, options)); return; } } (_a = options.callback) === null || _a === void 0 ? void 0 : _a.call(options, response); } })); }; RetryQueue.prototype.enqueue = function (requestOptions) { var retriesPerformedSoFar = requestOptions.retriesPerformedSoFar || 0; requestOptions.retriesPerformedSoFar = retriesPerformedSoFar + 1; var msToNextRetry = pickNextRetryDelay(retriesPerformedSoFar); var retryAt = Date.now() + msToNextRetry; this.queue.push({ retryAt: retryAt, requestOptions: requestOptions }); var logMessage = "Enqueued failed request for retry in ".concat(msToNextRetry); if (!navigator.onLine) { logMessage += ' (Browser is offline)'; } logger.warn(logMessage); if (!this.isPolling) { this.isPolling = true; this.poll(); } }; RetryQueue.prototype.poll = function () { var _this = this; this.poller && clearTimeout(this.poller); this.poller = setTimeout(function () { if (_this.areWeOnline && _this.queue.length > 0) { _this.flush(); } _this.poll(); }, this.pollIntervalMs); }; RetryQueue.prototype.flush = function () { var e_1, _a; var now = Date.now(); var notToFlush = []; var toFlush = this.queue.filter(function (item) { if (item.retryAt < now) { return true; } notToFlush.push(item); return false; }); this.queue = notToFlush; if (toFlush.length > 0) { try { for (var toFlush_1 = __values(toFlush), toFlush_1_1 = toFlush_1.next(); !toFlush_1_1.done; toFlush_1_1 = toFlush_1.next()) { var requestOptions = toFlush_1_1.value.requestOptions; this.retriableRequest(requestOptions); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (toFlush_1_1 && !toFlush_1_1.done && (_a = toFlush_1.return)) _a.call(toFlush_1); } finally { if (e_1) throw e_1.error; } } } }; RetryQueue.prototype.unload = function () { var e_2, _a; if (this.poller) { clearTimeout(this.poller); this.poller = undefined; } try { for (var _b = __values(this.queue), _c = _b.next(); !_c.done; _c = _b.next()) { var requestOptions = _c.value.requestOptions; try { // we've had send beacon in place for at least 2 years // eslint-disable-next-line compat/compat this.instance._send_request(__assign(__assign({}, requestOptions), { transport: 'sendBeacon' })); } catch (e) { // Note sendBeacon automatically retries, and after the first retry it will lose reference to contextual `this`. // This means in some cases `this.getConfig` will be undefined. logger.error(e); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } this.queue = []; }; return RetryQueue; }()); export { RetryQueue }; //# sourceMappingURL=retry-queue.js.map

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sadiuysal/mem0-mcp-server-ts'

If you have feedback or need assistance with the MCP directory API, please join our Discord server