{"version":3,"file":"retrying-transport.mjs","names":[],"sources":["../src/retrying-transport.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { IExporterTransport } from './exporter-transport';\nimport type { ExportResponse } from './export-response';\nimport { diag } from '@opentelemetry/api';\n\nconst MAX_ATTEMPTS = 5;\nconst INITIAL_BACKOFF = 1000;\nconst MAX_BACKOFF = 5000;\nconst BACKOFF_MULTIPLIER = 1.5;\nconst JITTER = 0.2;\n\n/**\n * Get a pseudo-random jitter that falls in the range of [-JITTER, +JITTER]\n */\nfunction getJitter() {\n  return Math.random() * (2 * JITTER) - JITTER;\n}\n\nclass RetryingTransport implements IExporterTransport {\n  private _transport: IExporterTransport;\n\n  constructor(transport: IExporterTransport) {\n    this._transport = transport;\n  }\n\n  private retry(\n    data: Uint8Array,\n    timeoutMillis: number,\n    inMillis: number\n  ): Promise<ExportResponse> {\n    return new Promise((resolve, reject) => {\n      setTimeout(() => {\n        this._transport.send(data, timeoutMillis).then(resolve, reject);\n      }, inMillis);\n    });\n  }\n\n  async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {\n    let attempts = MAX_ATTEMPTS;\n    let nextBackoff = INITIAL_BACKOFF;\n\n    const deadline = Date.now() + timeoutMillis;\n    let result = await this._transport.send(data, timeoutMillis);\n\n    while (result.status === 'retryable' && attempts > 0) {\n      attempts--;\n\n      // use maximum of computed backoff and 0 to avoid negative timeouts\n      const backoff = Math.max(\n        Math.min(nextBackoff * (1 + getJitter()), MAX_BACKOFF),\n        0\n      );\n      nextBackoff = nextBackoff * BACKOFF_MULTIPLIER;\n      const retryInMillis = result.retryInMillis ?? backoff;\n\n      // return when expected retry time is after the export deadline.\n      const remainingTimeoutMillis = deadline - Date.now();\n      if (retryInMillis > remainingTimeoutMillis) {\n        diag.info(\n          `Export retry time ${Math.round(retryInMillis)}ms exceeds remaining timeout ${Math.round(\n            remainingTimeoutMillis\n          )}ms, not retrying further.`\n        );\n        return result;\n      }\n\n      diag.verbose(`Scheduling export retry in ${Math.round(retryInMillis)}ms`);\n      result = await this.retry(data, remainingTimeoutMillis, retryInMillis);\n    }\n\n    if (result.status === 'success') {\n      diag.verbose(\n        `Export succeeded after ${MAX_ATTEMPTS - attempts} retry attempts.`\n      );\n    } else if (result.status === 'retryable') {\n      diag.info(\n        `Export failed after maximum retry attempts (${MAX_ATTEMPTS}).`\n      );\n    } else {\n      diag.info(`Export failed with non-retryable error: ${result.error}`);\n    }\n\n    return result;\n  }\n\n  shutdown() {\n    return this._transport.shutdown();\n  }\n}\n\n/**\n * Creates an Exporter Transport that retries on 'retryable' response.\n */\nexport function createRetryingTransport(options: {\n  // Underlying transport to wrap.\n  transport: IExporterTransport;\n}): IExporterTransport {\n  return new RetryingTransport(options.transport);\n}\n"],"mappings":";;AASA,MAAM,eAAe;AACrB,MAAM,kBAAkB;AACxB,MAAM,cAAc;AACpB,MAAM,qBAAqB;AAC3B,MAAM,SAAS;;;;AAKf,SAAS,YAAY;CACnB,OAAO,KAAK,OAAO,KAAK,IAAI,UAAU;AACxC;AAEA,IAAM,oBAAN,MAAsD;CACpD;CAEA,YAAY,WAA+B;EACzC,KAAK,aAAa;CACpB;CAEA,MACE,MACA,eACA,UACyB;EACzB,OAAO,IAAI,SAAS,SAAS,WAAW;GACtC,iBAAiB;IACf,KAAK,WAAW,KAAK,MAAM,aAAa,CAAC,CAAC,KAAK,SAAS,MAAM;GAChE,GAAG,QAAQ;EACb,CAAC;CACH;CAEA,MAAM,KAAK,MAAkB,eAAgD;EAC3E,IAAI,WAAW;EACf,IAAI,cAAc;EAElB,MAAM,WAAW,KAAK,IAAI,IAAI;EAC9B,IAAI,SAAS,MAAM,KAAK,WAAW,KAAK,MAAM,aAAa;EAE3D,OAAO,OAAO,WAAW,eAAe,WAAW,GAAG;GACpD;GAGA,MAAM,UAAU,KAAK,IACnB,KAAK,IAAI,eAAe,IAAI,UAAU,IAAI,WAAW,GACrD,CACF;GACA,cA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