@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
183 lines (182 loc) • 7.25 kB
TypeScript
/**
* @module CircuitBreaker
*/
import { type CircuitBreakerFactoryCreateSettings, type ICircuitBreaker, type ICircuitBreakerFactory, type ICircuitBreakerAdapter, type CircuitBreakerTrigger, type ICircuitBreakerListenable } from "../../../../circuit-breaker/contracts/_module.js";
import { type EventBusInput } from "../../../../event-bus/contracts/_module.js";
import { type IExecutionContext } from "../../../../execution-context/contracts/_module.js";
import { type INamespace } from "../../../../namespace/contracts/_module.js";
import { type ISerderRegister } from "../../../../serde/contracts/_module.js";
import { type ITimeSpan } from "../../../../time-span/contracts/_module.js";
import { type ErrorPolicy, type OneOrMore, type WaitUntil } from "../../../../utilities/_module.js";
/**
* Base configuration shared by all `CircuitBreakerFactory` variants.
*
* IMPORT_PATH: `"@daiso-tech/core/circuit-breaker"`
* @group Derivables
*/
export type CircuitBreakerFactorySettingsBase = {
/**
* @default
* ```ts
* import { NoOpNamespace } from "@daiso-tech/core/namespace";
*
* new NoOpNamespace()
* ```
*/
namespace?: INamespace;
/**
* You can provide an {@link IEventBus | `IEventBus`} or an {@link IEventBusAdapter | `IEventBusAdapter`} instance to handle the component's events.
* If you provide an adapter, it will be automatically wrapped in an {@link EventBus | `EventBus`} instance.
*
* @default
* ```ts
* import { NoOpEventBusAdapter } from "@daiso-tech/core/event-bus/no-op-event-bus-adapter";
*
* new NoOpEventBusAdapter()
* ```
*/
eventBus?: EventBusInput;
/**
* You can set the default `ErrorPolicy`
*
* @default
* ```ts
* (_error: unknown) => true
* ```
*/
defaultErrorPolicy?: ErrorPolicy;
/**
* You can set the default slow call threshold.
*
* @default
* ```ts
* import { TimeSpan } from "@daiso-tech/core/time-span";
*
* TimeSpan.fromSeconds(10);
* ```
*/
defaultSlowCallTime?: ITimeSpan;
/**
* You set the default trigger.
*
* @default
* ```ts
* import { CIRCUIT_BREAKER_TRIGGER} from "@daiso-tech/core/circuit-breaker/contracts";
*
* CIRCUIT_BREAKER_TRIGGER.BOTH
* ```
*/
defaultTrigger?: CircuitBreakerTrigger;
/**
* If true, metric tracking will run asynchronously in the background and won't block the function utilizing the circuit breaker logic.
* @default true
*/
enableAsyncTracking?: boolean;
/**
* You can pass an {@link ISerderRegister | `ISerderRegister`} instance to the {@link CircuitBreakerFactory | `CircuitBreakerFactory`} to register the circuit breaker's serialization and deserialization logic for the provided adapter.
* @default
* ```ts
* import { Serde } from "@daiso-tech/core/serde";
* import { NoOpSerdeAdapter } from "@daiso-tech/core/serde/no-op-serde-adapter";
*
* new Serde(new NoOpSerdeAdapter())
* ```
*/
serde?: OneOrMore<ISerderRegister>;
/**
* The serde transformer name used to identify circuit-breaker serializers and deserializers when there are adapters with the same name.
* @default ""
*/
serdeTransformerName?: string;
/**
* You can pass the `waitUntil` function to handle background promises.
* This is required when working with environments like Cloudflare Workers or Vercel Functions to ensure tasks complete after the response is sent.
* @default
* ```ts
* import { defaultWaitUntil } from "@daiso-tech/core/utilities"
* ```
*/
waitUntil?: WaitUntil;
/**
* You can pass {@link IExecutionContext | `IExecutionContext`} that will be used by context-aware adapters.
* @default
* ```ts
* import { ExecutionContext } from "@daiso-tech/core/execution-context"
* import { NoOpExecutionContextAdapter } from "@daiso-tech/core/execution-context/no-op-execution-context-adapter"
*
* new ExecutionContext(new NoOpExecutionContextAdapter())
* ```
*/
executionContext?: IExecutionContext;
};
/**
* Configuration for `CircuitBreakerFactory`.
* Extends {@link CircuitBreakerFactorySettingsBase | `CircuitBreakerFactorySettingsBase`} with a required adapter.
*
* IMPORT_PATH: `"@daiso-tech/core/circuit-breaker"`
* @group Derivables
*/
export type CircuitBreakerFactorySettings = CircuitBreakerFactorySettingsBase & {
/**
* The underlying circuit-breaker adapter that handles state persistence.
*/
adapter: ICircuitBreakerAdapter;
};
/**
* `CircuitBreakerFactory` class can be derived from any {@link ICircuitBreakerAdapter | `ICircuitBreakerAdapter`}.
*
* Note the {@link ICircuitBreaker | `ICircuitBreaker`} instances created by the `CircuitBreakerFactory` class are serializable and deserializable,
* allowing them to be seamlessly transferred across different servers, processes, and databases.
* This can be done directly using {@link ISerderRegister | `ISerderRegister`} or indirectly through components that rely on {@link ISerderRegister | `ISerderRegister`} internally.
*
* IMPORT_PATH: `"@daiso-tech/core/circuit-breaker"`
* @group Derivables
*/
export declare class CircuitBreakerFactory implements ICircuitBreakerFactory {
private readonly namespace;
private readonly eventBus;
private readonly adapter;
private readonly defaultSlowCallTime;
private readonly defaultTrigger;
private readonly defaultErrorPolicy;
private readonly serde;
private readonly serdeTransformerName;
private readonly enableAsyncTracking;
private readonly waitUntil;
private readonly executionContext;
/**
* @example
* ```ts
* import { KyselyCircuitBreakerStorageAdapter } from "@daiso-tech/core/circuit-breaker/kysely-circuit-breaker-storage-adapter";
* import { DatabaseCircuitBreakerAdapter } from "@daiso-tech/core/circuit-breaker/database-circuit-breaker-adapter";
* import { Serde } from "@daiso-tech/core/serde";
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"
* import Sqlite from "better-sqlite3";
* import { Kysely, SqliteDialect } from "kysely";
*
* const serde = new Serde(new SuperJsonSerdeAdapter());
* const circuitBreakerStorageAdapter = new KyselyCircuitBreakerStorageAdapter({
* kysely: new Kysely({
* dialect: new SqliteDialect({
* database: new Sqlite("local.db"),
* }),
* }),
* serde
* });
* // You need initialize the adapter once before using it.
* await circuitBreakerStorageAdapter.init();
*
* const circuitBreakerAdapter = new DatabaseCircuitBreakerAdapter({
* adapter: circuitBreakerStorageAdapter
* });
*
* const circuitBreakerFactory = new CircuitBreakerFactory({
* adapter: circuitBreakerAdapter
* })
* ```
*/
constructor(settings: CircuitBreakerFactorySettings);
private registerToSerde;
get events(): ICircuitBreakerListenable;
create(key: string, settings?: CircuitBreakerFactoryCreateSettings): ICircuitBreaker;
}