RateLimiter
Accessors
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" })})makeWithRateLimiter
Added in v1.0.0
Source
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
Errors
ErrorTypeId
Added in v1.0.0
Source
Signature
declare const ErrorTypeId: ErrorTypeIdErrorTypeId type
Added in v1.0.0
Source
Signature
type ErrorTypeId = "~@effect/experimental/RateLimiter/RateLimiterError"RateLimiterError
Added in v1.0.0
Source
Signature
declare const RateLimiterError: Union<readonly Array<Constraint>>RateLimiterError type
Added in v1.0.0
Source
Signature
type RateLimiterError = RateLimitExceeded | RateLimitStoreErrorRateLimitExceeded
Added in v1.0.0
Source
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;}RateLimitStoreError
Added in v1.0.0
Source
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
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
layerStoreMemory
Added in v1.0.0
Source
Signature
declare const layerStoreMemory: Layer.Layer<RateLimiterStore>RateLimiterStore
Added in v1.0.0
Source
Signature
declare class RateLimiterStore extends any { constructor();}