/** * @since 1.0.0 */ import * as Context from "effect/Context"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; /** * @since 1.0.0 * @category Type IDs */ export declare const TypeId: TypeId; /** * @since 1.0.0 * @category Type IDs */ export type TypeId = "~@effect/experimental/RateLimiter"; /** * @since 1.0.0 * @category Models */ export interface RateLimiter { readonly [TypeId]: TypeId; readonly consume: (options: { readonly algorithm?: "fixed-window" | "token-bucket" | undefined; readonly onExceeded?: "delay" | "fail" | undefined; readonly window: Duration.DurationInput; readonly limit: number; readonly key: string; readonly tokens?: number | undefined; }) => Effect.Effect; } /** * @since 1.0.0 * @category Tags */ export declare const RateLimiter: Context.Tag; /** * @since 1.0.0 * @category Constructors */ export declare const make: Effect.Effect; /** * @since 1.0.0 * @category Layers */ export declare const layer: Layer.Layer; /** * Access a function that applies rate limiting to an effect. * * ```ts * 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" * }) * ) * }) * ``` * * @since 1.0.0 * @category Accessors */ export declare const makeWithRateLimiter: Effect.Effect<((options: { readonly algorithm?: "fixed-window" | "token-bucket" | undefined; readonly onExceeded?: "delay" | "fail" | undefined; readonly window: Duration.DurationInput; readonly limit: number; readonly key: string; readonly tokens?: number | undefined; }) => (effect: Effect.Effect) => Effect.Effect), never, RateLimiter>; /** * Access a function that sleeps when the rate limit is exceeded. * * ```ts * 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" * }) * }) * ``` * * @since 1.0.0 * @category Accessors */ export declare const makeSleep: Effect.Effect<((options: { readonly algorithm?: "fixed-window" | "token-bucket" | undefined; readonly window: Duration.DurationInput; readonly limit: number; readonly key: string; readonly tokens?: number | undefined; }) => Effect.Effect), never, RateLimiter>; /** * @since 1.0.0 * @category Errors */ export declare const ErrorTypeId: ErrorTypeId; /** * @since 1.0.0 * @category Errors */ export type ErrorTypeId = "~@effect/experimental/RateLimiter/RateLimiterError"; declare const RateLimitExceeded_base: Schema.TaggedErrorClass; } & { retryAfter: typeof Schema.DurationFromMillis; key: typeof Schema.String; limit: typeof Schema.Number; remaining: typeof Schema.Number; }>; /** * @since 1.0.0 * @category Errors */ export declare class RateLimitExceeded extends RateLimitExceeded_base { /** * @since 1.0.0 */ readonly [ErrorTypeId]: ErrorTypeId; /** * @since 1.0.0 */ readonly reason = "Exceeded"; /** * @since 1.0.0 */ get message(): string; } declare const RateLimitStoreError_base: Schema.TaggedErrorClass; } & { message: typeof Schema.String; cause: Schema.optional; }>; /** * @since 1.0.0 * @category Errors */ export declare class RateLimitStoreError extends RateLimitStoreError_base { /** * @since 1.0.0 */ readonly [ErrorTypeId]: ErrorTypeId; /** * @since 1.0.0 */ readonly reason = "StoreError"; } /** * @since 1.0.0 * @category Errors */ export declare const RateLimiterError: Schema.Union<[typeof RateLimitExceeded, typeof RateLimitStoreError]>; /** * @since 1.0.0 * @category Errors */ export type RateLimiterError = RateLimitExceeded | RateLimitStoreError; /** * @since 1.0.0 * @category Models */ export interface ConsumeResult { /** * The amount of delay to wait before making the next request, when the rate * limiter is using the "delay" `onExceeded` strategy. * * It will be Duration.zero if the request is allowed immediately. */ readonly delay: Duration.Duration; /** * The maximum number of requests allowed in the current window. */ readonly limit: number; /** * The number of remaining requests in the current window. */ readonly remaining: number; /** * The time until the rate limit fully resets. */ readonly resetAfter: Duration.Duration; } declare const RateLimiterStore_base: Context.TagClass Effect.Effect; /** * Returns the current remaining tokens for the `key` after consuming the * specified amount of tokens. * * If `allowOverflow` is true, the number of tokens can drop below zero. * * In the case of no overflow, the returned token count will only be * negative if the requested tokens exceed the available tokens, but the * real token count will not be persisted below zero. */ readonly tokenBucket: (options: { readonly key: string; readonly tokens: number; readonly limit: number; readonly refillRate: Duration.Duration; readonly allowOverflow: boolean; }) => Effect.Effect; }>; /** * @since 1.0.0 * @category RateLimiterStore */ export declare class RateLimiterStore extends RateLimiterStore_base { } /** * @since 1.0.0 * @category RateLimiterStore */ export declare const layerStoreMemory: Layer.Layer; export {}; //# sourceMappingURL=RateLimiter.d.ts.map