solarwinds-apm
Version:
OpenTelemetry-based SolarWinds APM library
96 lines (80 loc) • 2.45 kB
text/typescript
/*
Copyright 2023-2025 SolarWinds Worldwide, LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
export interface BackoffOptions {
/** Initial backoff time */
initial: number
/** Max backoff time */
max?: number
/** Multiplier to apply to each subsequent timeout */
multiplier: number
/** Number of retries before giving up */
retries?: number
}
export class Backoff {
readonly
initial: number
current: number
max: number
}
readonly
readonly
initial: number
current: number
}
constructor(options: BackoffOptions) {
if (options.initial <= 0) {
throw new TypeError("initial backup value should be positive")
}
if (options.max !== undefined && options.max < options.initial) {
throw new TypeError("max backoff should be greater than initial")
}
if (options.retries !== undefined && options.retries <= 0) {
throw new Error("max retries should be positive")
}
this.
initial: options.initial,
current: options.initial,
max: options.max ?? Number.POSITIVE_INFINITY,
}
this.
if (options.retries) {
this.
initial: options.retries,
current: options.retries,
}
}
}
/** Resets the backoff to its initial state */
reset(): void {
this.
if (this.
this.
}
}
/** Returns the current backoff value or false if out of retries */
backoff(): number | false {
let current: number | false = this.
this.
current * this.
this.
)
if (this.
if (this.
current = false
} else {
this.
}
}
return current
}
}