RateLimiter.ts overview
Since v1.0.0
Exports Grouped by Category
Accessors
makeSleep
Access a function that sleeps when the rate limit is exceeded.
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"
})
})
Signature
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<ConsumeResult, RateLimitStoreError>,
never,
RateLimiter
>
Since v1.0.0
makeWithRateLimiter
Access a function that applies rate limiting to an effect.
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"
})
)
})
Signature
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
}) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | RateLimiterError, R>,
never,
RateLimiter
>
Since v1.0.0
Constructors
make
Signature
declare const make: Effect.Effect<RateLimiter, never, RateLimiterStore>
Since v1.0.0
Errors
ErrorTypeId
Signature
declare const ErrorTypeId: "~@effect/experimental/RateLimiter/RateLimiterError"
Since v1.0.0
ErrorTypeId (type alias)
Signature
type ErrorTypeId = "~@effect/experimental/RateLimiter/RateLimiterError"
Since v1.0.0
RateLimitExceeded (class)
Signature
declare class RateLimitExceeded
Since v1.0.0
[ErrorTypeId] (property)
Signature
readonly [ErrorTypeId]: "~@effect/experimental/RateLimiter/RateLimiterError"
Since v1.0.0
reason (property)
Signature
readonly reason: "Exceeded"
Since v1.0.0
RateLimitStoreError (class)
Signature
declare class RateLimitStoreError
Since v1.0.0
[ErrorTypeId] (property)
Signature
readonly [ErrorTypeId]: "~@effect/experimental/RateLimiter/RateLimiterError"
Since v1.0.0
reason (property)
Signature
readonly reason: "StoreError"
Since v1.0.0
RateLimiterError
Signature
declare const RateLimiterError: Schema.Union<[typeof RateLimitExceeded, typeof RateLimitStoreError]>
Since v1.0.0
RateLimiterError (type alias)
Signature
type RateLimiterError = RateLimitExceeded | RateLimitStoreError
Since v1.0.0
Layers
layer
Signature
declare const layer: Layer.Layer<RateLimiter, never, RateLimiterStore>
Since v1.0.0
Models
ConsumeResult (interface)
Signature
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
}
Since v1.0.0
RateLimiter (interface)
Signature
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<ConsumeResult, RateLimiterError>
}
Since v1.0.0
RateLimiterStore
RateLimiterStore (class)
Signature
declare class RateLimiterStore
Since v1.0.0
layerStoreMemory
Signature
declare const layerStoreMemory: Layer.Layer<RateLimiterStore, never, never>
Since v1.0.0
Tags
RateLimiter
Signature
declare const RateLimiter: Context.Tag<RateLimiter, RateLimiter>
Since v1.0.0
Type IDs
TypeId
Signature
declare const TypeId: "~@effect/experimental/RateLimiter"
Since v1.0.0
TypeId (type alias)
Signature
type TypeId = "~@effect/experimental/RateLimiter"
Since v1.0.0