Skip to content
Myr

rate-limits.ts

This file

Path

apps/platform/src/adapters/rate-limits.ts

85 lines · 2.4 KiB · TypeScript

Last changed by

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–10why, one step back
1import { sameHandle } from "@repo/protocol/ids";
2import type { Handle } from "@repo/protocol/ids";3import { CapReached } from "@repo/protocol/refusal";4import { Effect, Layer, Ref } from "effect";56import { RateLimits } from "../rate-limits.ts";7import type { Limited } from "../rate-limits.ts";89export interface Limits {10 readonly claim: number;

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

line 11why, one step back
11 readonly draft: number;

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 12–14why, one step back
12 readonly proposal: number;
13}14

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 15why, one step back
15// Mirrors each binding's `simple.limit` in wrangler.jsonc: a binding can't say its own limit.

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 16–20why, one step back
16export const WorkerCaps = {
17 claim: 10,18 draft: 2,19 proposal: 5,20} as const satisfies Limits;

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 21why, one step back
21

Say which limit a CapReached reached and when to try again, and that a fork takes branches onlyoscabrielbefore adoption42ea9b7

lines 22–27why, one step back
22const LIMIT_OF = {
23 claim: "claims",24 draft: "drafts",25 proposal: "proposals",26} as const satisfies Record<Limited, CapReached["limit"]>;27

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 28why, one step back
28// A binding can't say when its window resets, so the whole 60 s period is the honest upper bound.

Say which limit a CapReached reached and when to try again, and that a fork takes branches onlyoscabrielbefore adoption42ea9b7

lines 29–30why, one step back
29const WINDOW_SECONDS = 60;
30

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 31why, one step back
31const capped = (limits: Limits, kind: Limited) =>

Say which limit a CapReached reached and when to try again, and that a fork takes branches onlyoscabrielbefore adoption42ea9b7

lines 32–36why, one step back
32 new CapReached({
33 cap: limits[kind],34 limit: LIMIT_OF[kind],35 retryAfter: WINDOW_SECONDS,36 });

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 37why, one step back
37

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

line 38why, one step back
38// A binding that fails is a defect: a call it could not count is not let through.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 39–40why, one step back
39export const WorkerRateLimits = (bindings: {
40 readonly claim: RateLimit;

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

line 41why, one step back
41 readonly draft: RateLimit;

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 42–85why, one step back
42 readonly proposal: RateLimit;
43}): Layer.Layer<RateLimits> =>44 Layer.succeed(45 RateLimits,46 RateLimits.of({47 check: (kind, handle) =>48 Effect.tryPromise(() =>49 bindings[kind].limit({ key: handle.toLowerCase() })50 ).pipe(51 Effect.orDie,52 Effect.flatMap(({ success }) =>53 success ? Effect.void : Effect.fail(capped(WorkerCaps, kind))54 )55 ),56 })57 );5859interface Spent {60 readonly handle: Handle;61 readonly kind: Limited;62}6364export const MemoryRateLimits = (limits: Limits): Layer.Layer<RateLimits> =>65 Layer.effect(66 RateLimits,67 Effect.gen(function* () {68 const spent = yield* Ref.make<readonly Spent[]>([]);6970 return RateLimits.of({71 check: (kind, handle) =>72 Effect.flatMap(73 Ref.modify(spent, (all) => {74 const count = all.filter(75 (each) => each.kind === kind && sameHandle(each.handle, handle)76 ).length;7778 return [count < limits[kind], [...all, { handle, kind }]];79 }),80 (allowed) =>81 allowed ? Effect.void : Effect.fail(capped(limits, kind))82 ),83 });84 })85 );