Skip to main content

Creating RateLimiter adapters

Implementing your custom IRateLimiterAdapter​

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

Implementing your custom IRateLimiterStorageAdapter​

We provide an additional contract IRateLimiterStorageAdapter for building custom rate-limiter storage adapters tailored to DatabaseRateLimiterAdapter and DatabaseRateLimiterProviderFactory.

Testing your custom IRateLimiterStorageAdapter​

We provide a complete test suite to test your rate-limiter storage adapter implementation. Simply use the rateLimiterBreakerStorageTestSuite function:

  • Preconfigured Vitest test cases
  • Common edge case coverage

Usage example:

./samples/rate-limiter-storage-test-suite.ts
import { beforeEach, describe, expect, test } from "vitest";
import { rateLimiterStorageAdapterTestSuite } from "eridu-tech/rate-limiter/test-utilities";
import { MemoryRateLimiterStorageAdapter } from "eridu-tech/rate-limiter/memory-rate-limiter-storage-adapter";

describe("class: MyRateLimiterStorageAdapter", () => {
rateLimiterStorageAdapterTestSuite({
createAdapter: () => new MemoryRateLimiterStorageAdapter(),
test,
beforeEach,
expect,
describe,
});
});

Implementing your custom IRateLimiterProvider class​

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

Further information​

For further information refer to eridu-tech/rate-limiter API docs.