Skip to main content

Creating circuit-breaker adapters

Implementing your custom ICircuitBreakerAdapter

In order to create an adapter you need to implement the ICircuitBreakerAdapter contract.

Implementing your custom ICircuitBreakerStorageAdapter

We provide an additional contract ICircuitBreakerStorageAdapter for building custom circuit-breaker storage adapters tailored to DatabaseCircuitBreakerAdapter and DatabaseCircuitBreakerProviderFactory.

Testing your custom ICircuitBreakerStorageAdapter

We provide a complete test suite to test your circuit-breaker storage adapter implementation. Simply use the circuitBreakerStorageTestSuite function:

  • Preconfigured Vitest test cases
  • Common edge case coverage

Usage example:

// filename: MyCircuitBreakerStorageAdapter.test.ts

import { beforeEach, describe, expect, test } from "vitest";
import { circuitBreakerStorageTestSuite } from "@daiso-tech/core/circuit-breaker/test-utilities";
import { MemoryCircuitBreakerStorageAdapter } from "./MemoryCircuitBreakerStorageAdapter.js";

describe("class: MyCircuitBreakerStorageAdapter", () => {
circuitBreakerStorageTestSuite({
createAdapter: () => new MemoryCircuitBreakerStorageAdapter(),
test,
beforeEach,
expect,
describe,
});
});

Implementing your custom ICircuitBreakerProvider class

In some cases, you may need to implement a custom CircuitBreakerProvider class to optimize performance for your specific technology stack. You can then directly implement the ICircuitBreakerProvider contract.

Further information

For further information refer to @daiso-tech/core/circuit-breaker API docs.