RateLimitPersistence.ts overview
A RateLimiter over effect/persistence’s RateLimiter, in memory or on Redis for several instances. A rule’s blockDurationMs is a second fixed window under <key>:blocked, and resetLimit is a no-op.
Example
In memory
```ts
import assert from "node:assert/strict"
import { Effect, Layer, Result } from "effect"
import { RateLimiter as PersistenceRateLimiter } from "effect/persistence"
import { RateLimiter } from "@thomasfosterau/effect-auth/RateLimit"
import { rateLimiterPersistenceLayer } from "@thomasfosterau/effect-auth/RateLimitPersistence"
const RateLimiterLive = rateLimiterPersistenceLayer.pipe(
Layer.provide(PersistenceRateLimiter.layer),
Layer.provide(PersistenceRateLimiter.layerStoreMemory),
)
// The facade keeps the RateLimiter contract: two attempts pass, the third is refused.
const limit = { maxRequests: 2, windowMs: 60_000 }
const program = Effect.gen(function* () {
const limiter = yield* RateLimiter
yield* limiter.consumeToken("login:ada", limit)
yield* limiter.consumeToken("login:ada", limit)
const refused = yield* Effect.result(limiter.consumeToken("login:ada", limit))
assert.ok(Result.isFailure(refused))
}).pipe(Effect.provide(RateLimiterLive))
await Effect.runPromise(program)
**Example**
```ts
On Redis, for several instances
```ts
import { Layer } from "effect"
import { RateLimiter as PersistenceRateLimiter, Redis } from "effect/persistence"
import { rateLimiterPersistenceLayer } from "@thomasfosterau/effect-auth/RateLimitPersistence"
// The app's Redis client layer (for example, a node-redis adapter) is supplied
// by the app. Building this layer connects to Redis, so it is composed here and
// not run.
export const makeRateLimiterRedis = (redisLayer: Layer.Layer<Redis.Redis>) =>
rateLimiterPersistenceLayer.pipe(
Layer.provide(PersistenceRateLimiter.layer),
Layer.provide(PersistenceRateLimiter.layerStoreRedis()),
Layer.provide(redisLayer),
)
---
## Exports Grouped by Category
- [rateLimiterPersistenceLayer](#ratelimiterpersistencelayer)
---
# utils
## rateLimiterPersistence
Build the facade over the persistence limiter. The persistence `RateLimiter`
service is captured here so the returned methods keep the interface's
(`RateLimitStoreTag`) requirement satisfiable without touching the legacy
store.
A rule's `blockDurationMs` is applied: once a key exceeds its window it is
refused for the whole block, even after its window has reset.
**Signature**
```ts
declare const rateLimiterPersistence: Effect.Effect<{ readonly checkLimit: (key: string, config: RateLimitConfig) => Effect.Effect<boolean, RateLimitError | AuthError>; readonly consumeToken: (key: string, config: RateLimitConfig) => Effect.Effect<void, RateLimitError | AuthError>; readonly resetLimit: (key: string) => Effect.Effect<void, AuthError>; readonly getRemainingTokens: (key: string, config: RateLimitConfig) => Effect.Effect<number, AuthError>; }, never, PersistenceRateLimiter.RateLimiter>
rateLimiterPersistenceLayer
A Layer<RateLimiterTag, never, PersistenceRateLimiter.RateLimiter>. Provide PersistenceRateLimiter.layer plus a store (layerStoreMemory or a Redis store) to obtain a runnable limiter (the module examples show both).
Signature
declare const rateLimiterPersistenceLayer: Layer.Layer<RateLimiter, never, PersistenceRateLimiter.RateLimiter>