File size: 12,591 Bytes
31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | /**
* Coordinates rate limits through shared persistent storage.
*
* The `RateLimiter` service consumes tokens for string keys using fixed-window
* counters or token-bucket state. It can protect external APIs, enforce quotas,
* or throttle workers across fibers and processes that share the same store.
* This module includes helpers that fail when a limit is exceeded, return the
* delay needed before continuing, or wrap an effect so it waits automatically.
* It also defines the store service and in-memory or Redis-backed store layers.
*
* @since 4.0.0
*/
import * as Config from "../../Config.ts";
import * as Context from "../../Context.ts";
import * as Duration from "../../Duration.ts";
import * as Effect from "../../Effect.ts";
import * as Layer from "../../Layer.ts";
import * as Schema from "../../Schema.ts";
import * as Redis from "./Redis.ts";
/**
* Runtime type identifier for `RateLimiter` values.
*
* @category type IDs
* @since 4.0.0
*/
export declare const TypeId: TypeId;
/**
* Type-level identifier used to brand `RateLimiter` values.
*
* @category type IDs
* @since 4.0.0
*/
export type TypeId = "~effect/persistence/RateLimiter";
/**
* Service for consuming rate-limit tokens for a key using fixed-window or
* token-bucket algorithms.
*
* @category models
* @since 4.0.0
*/
export interface RateLimiter {
readonly [TypeId]: TypeId;
readonly consume: (options: {
readonly algorithm?: "fixed-window" | "token-bucket" | undefined;
readonly onExceeded?: "delay" | "fail" | undefined;
readonly window: Duration.Input;
readonly limit: number;
readonly key: string;
readonly tokens?: number | undefined;
}) => Effect.Effect<ConsumeResult, RateLimiterError>;
}
/**
* Service tag for persistent token-consumption services.
*
* **When to use**
*
* Use to access or provide rate-limit checks backed by fixed-window counters or
* token-bucket state.
*
* @category services
* @since 4.0.0
*/
export declare const RateLimiter: Context.Service<RateLimiter, RateLimiter>;
/**
* Creates a `RateLimiter` from the current `RateLimiterStore`.
*
* **Details**
*
* The limiter supports fixed-window and token-bucket algorithms and either
* fails or returns a delay when a limit is exceeded.
*
* @category constructors
* @since 4.0.0
*/
export declare const make: Effect.Effect<RateLimiter, never, RateLimiterStore>;
/**
* Provides `RateLimiter` using the current `RateLimiterStore`.
*
* @category layers
* @since 4.0.0
*/
export declare const layer: Layer.Layer<RateLimiter, never, RateLimiterStore>;
/**
* Accesses a function that applies rate limiting to an effect.
*
* **Example** (Applying rate limits to effects)
*
* ```ts
* import { Effect } from "effect"
* import { RateLimiter } from "effect/unstable/persistence"
*
* 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"
* })
* )
* })
* ```
*
* @category accessors
* @since 4.0.0
*/
export declare const makeWithRateLimiter: Effect.Effect<((options: {
readonly algorithm?: "fixed-window" | "token-bucket" | undefined;
readonly onExceeded?: "delay" | "fail" | undefined;
readonly window: Duration.Input;
readonly limit: number;
readonly key: string;
readonly tokens?: number | undefined;
}) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | RateLimiterError, R>), never, RateLimiter>;
/**
* Accesses a function that sleeps when the rate limit is exceeded.
*
* **Example** (Sleeping until rate limit permits)
*
* ```ts
* import { Effect } from "effect"
* import { RateLimiter } from "effect/unstable/persistence"
*
* 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"
* })
* })
* ```
*
* @category accessors
* @since 4.0.0
*/
export declare const makeSleep: Effect.Effect<((options: {
readonly algorithm?: "fixed-window" | "token-bucket" | undefined;
readonly window: Duration.Input;
readonly limit: number;
readonly key: string;
readonly tokens?: number | undefined;
}) => Effect.Effect<ConsumeResult, RateLimiterError>), never, RateLimiter>;
/**
* Runtime type identifier for `RateLimiterError`.
*
* @category type IDs
* @since 4.0.0
*/
export declare const ErrorTypeId: ErrorTypeId;
/**
* Type-level identifier used to brand `RateLimiterError` values.
*
* @category type IDs
* @since 4.0.0
*/
export type ErrorTypeId = "~@effect/experimental/RateLimiter/RateLimiterError";
declare const RateLimitExceeded_base: Schema.Class<RateLimitExceeded, Schema.Struct<{
readonly _tag: Schema.tag<"RateLimitExceeded">;
readonly retryAfter: Schema.DurationFromMillis;
readonly key: Schema.String;
readonly limit: Schema.Number;
readonly remaining: Schema.Number;
}>, import("../../Cause.ts").YieldableError>;
/**
* Error reason for a rate-limit check that exceeded the configured limit.
*
* **Details**
*
* Includes the affected key, limit, remaining token count, and retry delay.
*
* @category errors
* @since 4.0.0
*/
export declare class RateLimitExceeded extends RateLimitExceeded_base {
/**
* Public message used when the rate limiter rejects a request.
*
* @since 4.0.0
*/
get message(): string;
}
declare const RateLimitStoreError_base: Schema.Class<RateLimitStoreError, Schema.Struct<{
readonly _tag: Schema.tag<"RateLimitStoreError">;
readonly message: Schema.String;
readonly cause: Schema.optional<Schema.Defect>;
}>, import("../../Cause.ts").YieldableError>;
/**
* Error reason for failures in the backing `RateLimiterStore`.
*
* @category errors
* @since 4.0.0
*/
export declare class RateLimitStoreError extends RateLimitStoreError_base {
}
/**
* Union of reasons carried by `RateLimiterError`.
*
* @category errors
* @since 4.0.0
*/
export type RateLimiterErrorReason = RateLimitExceeded | RateLimitStoreError;
/**
* Schema for all reasons that can be carried by `RateLimiterError`.
*
* @category errors
* @since 4.0.0
*/
export declare const RateLimiterErrorReason: Schema.Union<[
typeof RateLimitExceeded,
typeof RateLimitStoreError
]>;
declare const RateLimiterError_base: Schema.Class<RateLimiterError, Schema.Struct<{
readonly _tag: Schema.tag<"RateLimiterError">;
readonly reason: Schema.Union<[typeof RateLimitExceeded, typeof RateLimitStoreError]>;
}>, import("../../Cause.ts").YieldableError>;
/**
* Error raised by rate limiter operations, wrapping a concrete failure
* `reason`.
*
* @category errors
* @since 4.0.0
*/
export declare class RateLimiterError extends RateLimiterError_base {
constructor(props: {
readonly reason: RateLimiterErrorReason;
});
/**
* Marks this value as a rate limiter error for runtime guards.
*
* @since 4.0.0
*/
readonly [ErrorTypeId]: ErrorTypeId;
get message(): string;
}
/**
* Metadata returned after consuming tokens from a rate limiter.
*
* @category models
* @since 4.0.0
*/
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.ServiceClass<RateLimiterStore, "effect/persistence/RateLimiter/RateLimiterStore", {
/**
* Returns the token count *after* taking the specified `tokens` and time to
* live for the `key`.
*
* If `limit` is provided, the number of taken tokens will be capped at the
* limit.
*
* In the case the limit is exceeded, the returned count will be greater
* than the limit, but the TTL will not be updated.
*/
readonly fixedWindow: (options: {
readonly key: string;
readonly tokens: number;
readonly refillRate: Duration.Duration;
readonly limit: number | undefined;
}) => Effect.Effect<readonly [count: number, ttl: number], RateLimiterError>;
/**
* 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<number, RateLimiterError>;
}>;
/**
* Defines the low-level backing store for fixed-window counters and token-bucket state.
*
* **When to use**
*
* Use to provide the shared counter storage used by persistent rate-limit
* checks.
*
* @category store
* @since 4.0.0
*/
export declare class RateLimiterStore extends RateLimiterStore_base {
}
/**
* Provides a process-local in-memory `RateLimiterStore`.
*
* @category RateLimiterStore
* @since 4.0.0
*/
export declare const layerStoreMemory: Layer.Layer<RateLimiterStore>;
/**
* Creates a Redis-backed `RateLimiterStore` using Lua scripts and the
* configured key prefix.
*
* @category RateLimiterStore
* @since 4.0.0
*/
export declare const makeStoreRedis: (options?: {
readonly prefix?: string | undefined;
} | undefined) => Effect.Effect<{
/**
* Returns the token count *after* taking the specified `tokens` and time to
* live for the `key`.
*
* If `limit` is provided, the number of taken tokens will be capped at the
* limit.
*
* In the case the limit is exceeded, the returned count will be greater
* than the limit, but the TTL will not be updated.
*/
readonly fixedWindow: (options: {
readonly key: string;
readonly tokens: number;
readonly refillRate: Duration.Duration;
readonly limit: number | undefined;
}) => Effect.Effect<readonly [count: number, ttl: number], RateLimiterError>;
/**
* 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<number, RateLimiterError>;
}, never, Redis.Redis>;
/**
* Provides a Redis-backed `RateLimiterStore` using `makeStoreRedis`.
*
* @category layers
* @since 4.0.0
*/
export declare const layerStoreRedis: (options?: {
readonly prefix?: string | undefined;
}) => Layer.Layer<RateLimiterStore, never, Redis.Redis>;
/**
* Provides a Redis-backed `RateLimiterStore` from wrapped configuration
* options.
*
* @category layers
* @since 4.0.0
*/
export declare const layerStoreRedisConfig: (options: Config.Wrap<{
readonly prefix?: string | undefined;
}>) => Layer.Layer<RateLimiterStore, Config.ConfigError, Redis.Redis>;
export {};
//# sourceMappingURL=RateLimiter.d.ts.map |