Skip to content
Effect Days 2026 Get your ticket

RateLimiter

17 exports Added in v1.0.0 Source

Accessors

makeSleep

Added in v1.0.0 Source

Access a function that sleeps when the rate limit is exceeded.

Signature

declare const makeSleep: Effect.Effect<(options: {
readonly algorithm?: "fixed-window" | "token-bucket";
readonly key: string;
readonly limit: number;
readonly tokens?: number;
readonly window: Duration.DurationInput;
}) => Effect.Effect<ConsumeResult, RateLimitStoreError>, never, RateLimiter>

Example

import { RateLimiter } from "@effect/experimental"
import { Effect } from "effect"
export default Effect.gen(function*() {
// Access the `sleep` function from the RateLimiter module
const sleep = yield* RateLimiter.makeSleep
// Use the `sleep` function with specific rate limiting parameters.
// This will only sleep if the rate limit has been exceeded.
yield* sleep({
key: "some-key",
limit: 10,
window: "5 seconds",
algorithm: "fixed-window"
})
})

Access a function that applies rate limiting to an effect.

Signature

declare const makeWithRateLimiter: Effect.Effect<(options: {
readonly algorithm?: "fixed-window" | "token-bucket";
readonly key: string;
readonly limit: number;
readonly onExceeded?: "delay" | "fail";
readonly tokens?: number;
readonly window: Duration.DurationInput;
}) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | RateLimiterError, R>, never, RateLimiter>

Example

import { RateLimiter } from "@effect/experimental"
import { Effect } from "effect"
Effect.gen(function*() {
// Access the `withLimiter` function from the RateLimiter module
const withLimiter = yield* RateLimiter.makeWithRateLimiter
// Apply a rate limiter to an effect
yield* Effect.log("Making a request with rate limiting").pipe(
withLimiter({
key: "some-key",
limit: 10,
onExceeded: "delay",
window: "5 seconds",
algorithm: "fixed-window"
})
)
})

Constructors

make

Added in v1.0.0 Source

Signature

declare const make: Effect.Effect<RateLimiter, never, RateLimiterStore>

Errors

ErrorTypeId

Added in v1.0.0 Source

Signature

declare const ErrorTypeId: ErrorTypeId

ErrorTypeId type

Added in v1.0.0 Source

Signature

type ErrorTypeId = "~@effect/experimental/RateLimiter/RateLimiterError"

Signature

declare const RateLimiterError: Union<readonly Array<Constraint>>

RateLimiterError type

Added in v1.0.0 Source

Signature

type RateLimiterError = RateLimitExceeded | RateLimitStoreError

Signature

declare class RateLimitExceeded extends {
readonly _tag: "RateLimiterError";
readonly key: string;
readonly limit: number;
readonly remaining: number;
readonly retryAfter: Duration;
} & YieldableError<this> {
constructor(...args: [props: {
readonly _tag?: "RateLimiterError";
readonly key: string;
readonly limit: number;
readonly remaining: number;
readonly retryAfter: Duration;
}, options?: MakeOptions]);
readonly "~@effect/experimental/RateLimiter/RateLimiterError": "~@effect/experimental/RateLimiter/RateLimiterError";
readonly reason: "Exceeded";
message: string;
}

Signature

declare class RateLimitStoreError extends {
readonly _tag: "RateLimiterError";
readonly cause?: unknown;
readonly message: string;
} & YieldableError<this> {
constructor(...args: [props: {
readonly _tag?: "RateLimiterError";
readonly cause?: unknown;
readonly message: string;
}, options?: MakeOptions]);
readonly "~@effect/experimental/RateLimiter/RateLimiterError": "~@effect/experimental/RateLimiter/RateLimiterError";
readonly reason: "StoreError";
}

Layers

layer

Added in v1.0.0 Source

Signature

declare const layer: Layer.Layer<RateLimiter, never, RateLimiterStore>

Models

ConsumeResult interface

Added in v1.0.0 Source

Signature

interface ConsumeResult {
readonly delay: Duration;
readonly limit: number;
readonly remaining: number;
readonly resetAfter: Duration;
}

RateLimiter interface

Added in v1.0.0 Source

Signature

interface RateLimiter {
readonly "~@effect/experimental/RateLimiter": "~@effect/experimental/RateLimiter";
readonly consume: (options: {
readonly algorithm?: "fixed-window" | "token-bucket";
readonly key: string;
readonly limit: number;
readonly onExceeded?: "delay" | "fail";
readonly tokens?: number;
readonly window: DurationInput;
}) => Effect<ConsumeResult, RateLimiterError>;
}

RateLimiterStore

Signature

declare const layerStoreMemory: Layer.Layer<RateLimiterStore>

Signature

declare class RateLimiterStore extends any {
constructor();
}

Tags

RateLimiter

Added in v1.0.0 Source

Signature

declare const RateLimiter: Tag<RateLimiter, RateLimiter>

Type IDs

TypeId

Added in v1.0.0 Source

Signature

declare const TypeId: TypeId

TypeId type

Added in v1.0.0 Source

Signature

type TypeId = "~@effect/experimental/RateLimiter"