import { BrokenCircuitError, CircuitState, ExponentialBackoff, ConstantBackoff, handleAll, handleWhen } from "cockatiel"; import type { CircuitBreakerPolicy, Event as CockatielEvent, IBackoffFactory, IPolicy, Policy, RetryPolicy } from "cockatiel"; export { BrokenCircuitError, CircuitState, ConstantBackoff, ExponentialBackoff, handleAll, handleWhen, }; export type { CockatielEvent }; /** * The options for `createServicePolicy`. */ export type CreateServicePolicyOptions = { /** * The backoff strategy to use. Mainly useful for testing so that a constant * backoff can be used when mocking timers. Defaults to an instance of * ExponentialBackoff. */ backoff?: IBackoffFactory; /** * The length of time (in milliseconds) to pause retries of the action after * the number of failures reaches `maxConsecutiveFailures`. */ circuitBreakDuration?: number; /** * The length of time (in milliseconds) that governs when the service is * regarded as degraded (affecting when `onDegraded` is called). */ degradedThreshold?: number; /** * The maximum number of times that the service is allowed to fail before * pausing further retries. */ maxConsecutiveFailures?: number; /** * The maximum number of times that a failing service should be re-invoked * before giving up. */ maxRetries?: number; /** * The policy used to control when the service should be retried based on * either the result of the service or an error that it throws. For instance, * you could use this to retry only certain errors. See `handleWhen` and * friends from Cockatiel for more. */ retryFilterPolicy?: Policy; }; /** * The service policy object. */ export type ServicePolicy = IPolicy & { /** * The Cockatiel circuit breaker policy that the service policy uses * internally. */ circuitBreakerPolicy: CircuitBreakerPolicy; /** * The Cockatiel retry policy that the service policy uses internally. */ retryPolicy: RetryPolicy; /** * A function which is called when the number of times that the service fails * in a row meets the set maximum number of consecutive failures. */ onBreak: CircuitBreakerPolicy['onBreak']; /** * A function which is called in two circumstances: 1) when the service * succeeds before the maximum number of consecutive failures is reached, but * takes more time than the `degradedThreshold` to run, or 2) if the service * never succeeds before the retry policy gives up and before the maximum * number of consecutive failures has been reached. */ onDegraded: CockatielEvent; /** * A function which will be called by the retry policy each time the service * fails and the policy kicks off a timer to re-run the service. This is * primarily useful in tests where we are mocking timers. */ onRetry: RetryPolicy['onRetry']; }; /** * The maximum number of times that a failing service should be re-run before * giving up. */ export declare const DEFAULT_MAX_RETRIES = 3; /** * The maximum number of times that the service is allowed to fail before * pausing further retries. This is set to a value such that if given a * service that continually fails, the policy needs to be executed 3 times * before further retries are paused. */ export declare const DEFAULT_MAX_CONSECUTIVE_FAILURES: number; /** * The default length of time (in milliseconds) to temporarily pause retries of * the service after enough consecutive failures. */ export declare const DEFAULT_CIRCUIT_BREAK_DURATION: number; /** * The default length of time (in milliseconds) that governs when the service is * regarded as degraded (affecting when `onDegraded` is called). */ export declare const DEFAULT_DEGRADED_THRESHOLD = 5000; /** * Constructs an object exposing an `execute` method which, given a function — * hereafter called the "service" — will retry that service with ever increasing * delays until it succeeds. If the policy detects too many consecutive * failures, it will block further retries until a designated time period has * passed; this particular behavior is primarily designed for services that wrap * API calls so as not to make needless HTTP requests when the API is down and * to be able to recover when the API comes back up. In addition, hooks allow * for responding to certain events, one of which can be used to detect when an * HTTP request is performing slowly. * * Internally, this function makes use of the retry and circuit breaker policies * from the [Cockatiel](https://www.npmjs.com/package/cockatiel) library; see * there for more. * * @param options - The options to this function. See * {@link CreateServicePolicyOptions}. * @returns The service policy. * @example * This function is designed to be used in the context of a service class like * this: * ``` ts * class Service { * constructor() { * this.#policy = createServicePolicy({ * maxRetries: 3, * retryFilterPolicy: handleWhen((error) => { * return error.message.includes('oops'); * }), * maxConsecutiveFailures: 3, * circuitBreakDuration: 5000, * degradedThreshold: 2000, * onBreak: () => { * console.log('Circuit broke'); * }, * onDegraded: () => { * console.log('Service is degraded'); * }, * }); * } * * async fetch() { * return await this.#policy.execute(async () => { * const response = await fetch('https://some/url'); * return await response.json(); * }); * } * } * ``` */ export declare function createServicePolicy(options?: CreateServicePolicyOptions): ServicePolicy; //# sourceMappingURL=create-service-policy.d.cts.map