RateLimitStore.ts overview

Drizzle RateLimitStore, one row per key (a dialect’s Tables, or a table mapped with defineRateLimitTable). increment is two atomic statements, never a read-modify-write; on MySQL the count is read back, which can only refuse more. Rows of keys never seen again stay until purgeExpired.


Exports Grouped by Category


utils

PurgeExpiredOptions (interface)

Options for purgeExpired.

Signature

export interface PurgeExpiredOptions {
  /**
   * The most rows one call deletes, a positive integer; omitted, every
   * spent row goes in one `DELETE`.
   */
  readonly limit?: number
}

Source

layer

RateLimitStore backed by drizzle, on top of DrizzleDatabase.

Signature

declare const layer: (
  tables?: RateLimitTableInput
) => Layer.Layer<Auth.RateLimit.RateLimitStore, never, DrizzleDatabase>

Source

make

Build a RateLimitStore over the ambient DrizzleDatabase.

Signature

declare const make: (
  tables?: RateLimitTableInput
) => Effect.Effect<typeof Auth.RateLimit.RateLimitStore.Service, never, DrizzleDatabase>

Source

purgeExpired

Delete the rate-limit rows whose window and block have both passed, in one DELETE over the ambient DrizzleDatabase.

The table holds one row per key, IP and identifier ever counted, and no store call deletes the row of a key that is never seen again, so without a purge it grows by one row per distinct caller. A row goes once its reset_at is not after now and its blocked_until is unset or not after now: the limiter then treats the key as one it has never seen, so the next request opens a fresh window either way. A row still inside its window or still blocked stays. Schedule it from the host (a cron trigger, a timer on a long-lived server); now is read from the Clock.

With options.limit the DELETE takes at most that many of those rows, chosen by key through a derived table, which PostgreSQL, SQLite and MySQL all accept (MySQL refuses a LIMIT directly inside IN): `DELETE … WHERE AND key IN (SELECT key FROM (SELECT key … WHERE

LIMIT n) AS spent)`; the outer `` keeps a row a concurrent `increment` refreshed after the subquery picked it (PostgreSQL READ COMMITTED rechecks only the outer `WHERE`). It returns nothing on any dialect (MySQL has no `RETURNING`, and the drivers report affected rows each their own way), so a Workers cron that must stay inside its budget runs a fixed number of bounded calls per tick; whatever is left goes on the next tick. Which spent rows a capped call takes is unspecified. A `limit` that is not a positive integer fails with `AuthError` before any statement runs. **Signature** ```ts declare const purgeExpired: ( tables?: RateLimitTableInput, options?: PurgeExpiredOptions ) => Effect.Effect<void, Auth.Errors.AuthError, DrizzleDatabase> ``` [Source](https://github.com/thomasfosterau/effect-auth/blob/trunk/packages/drizzle/src/RateLimitStore.ts#L187)

This site uses Just the Docs, a documentation theme for Jekyll.