{"version":3,"file":"RetryingTransport.cjs","names":["OTelPerformanceManager","INITIAL_BACKOFF","MAX_BACKOFF","BACKOFF_MULTIPLIER"],"sources":["../../../src/transport/RetryingTransport/RetryingTransport.ts"],"sourcesContent":["import type {\n  ExportResponse,\n  IExporterTransport,\n} from '@opentelemetry/otlp-exporter-base';\nimport type { PerformanceManager } from '../../utils/index.ts';\nimport { OTelPerformanceManager } from '../../utils/index.ts';\nimport {\n  BACKOFF_MULTIPLIER,\n  INITIAL_BACKOFF,\n  JITTER,\n  MAX_ATTEMPTS,\n  MAX_BACKOFF,\n} from './constants.ts';\n\n/**\n * Get a pseudo-random jitter that falls in the range of [-JITTER, +JITTER]\n */\nconst getJitter = () => Math.random() * (2 * JITTER) - JITTER;\n\n// Taken directly from open-telemetry/opentelemetry-js/experimental/packages/otlp-exporter-base/src/retrying-transport.ts\n// File is not exposed externally\n\n/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nexport class RetryingTransport implements IExporterTransport {\n  public constructor(\n    private readonly _transport: IExporterTransport,\n    private readonly _perf: PerformanceManager = new OTelPerformanceManager(),\n  ) {}\n\n  public async send(\n    data: Uint8Array,\n    timeoutMillis: number,\n  ): Promise<ExportResponse> {\n    const deadline = this._perf.getNowMillis() + timeoutMillis;\n    let result = await this._transport.send(data, timeoutMillis);\n    let attempts = MAX_ATTEMPTS;\n    let nextBackoff = INITIAL_BACKOFF;\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, MAX_BACKOFF) + getJitter(),\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 - this._perf.getNowMillis();\n      if (retryInMillis > remainingTimeoutMillis) {\n        return result;\n      }\n\n      result = await this._retry(data, remainingTimeoutMillis, retryInMillis);\n    }\n\n    return result;\n  }\n\n  public shutdown() {\n    this._transport.shutdown();\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"],"mappings":";;;;;;;AAiBA,MAAM,kBAAkB,KAAK,QAAQ,GAAI,KAAA;AAoBzC,IAAa,oBAAb,MAA6D;CAC3D,YACE,YACA,QAA6C,IAAIA,wDAAAA,wBAAwB,EACzE;AAFiB,OAAA,aAAA;AACA,OAAA,QAAA;;CAGnB,MAAa,KACX,MACA,eACyB;EACzB,MAAM,WAAW,KAAK,MAAM,cAAc,GAAG;EAC7C,IAAI,SAAS,MAAM,KAAK,WAAW,KAAK,MAAM,cAAc;EAC5D,IAAI,WAAA;EACJ,IAAI,cAAcC,8CAAAA;AAElB,SAAO,OAAO,WAAW,eAAe,WAAW,GAAG;AACpD;GAGA,MAAM,UAAU,KAAK,IACnB,KAAK,IAAI,aAAaC,8CAAAA,YAAY,GAAG,WAAW,EAChD,EACD;AACD,iBAAc,cAAcC,8CAAAA;GAC5B,MAAM,gBAAgB,OAAO,iBAAiB;GAG9C,MAAM,yBAAyB,WAAW,KAAK,MAAM,cAAc;AACnE,OAAI,gBAAgB,uBAClB,QAAO;AAGT,YAAS,MAAM,KAAK,OAAO,MAAM,wBAAwB,cAAc;;AAGzE,SAAO;;CAGT,WAAkB;AAChB,OAAK,WAAW,UAAU;;CAG5B,OACE,MACA,eACA,UACyB;AACzB,SAAO,IAAI,SAAS,SAAS,WAAW;AACtC,oBAAiB;AACf,SAAK,WAAW,KAAK,MAAM,cAAc,CAAC,KAAK,SAAS,OAAO;MAC9D,SAAS;IACZ"}