cred402/lib/gateway/rate_limit.ts
caelum0x 086381d507 feat(gateway): make rate limiting org-scoped and plan-tiered
Rate limiting was per-API-key, so an org's `plan` field was decorative — a
free-plan org that minted 5 teammate keys got 5x the effective request
budget of a single-key free org. Org-scoped keys now share ONE rate-limit
bucket per org (`org:<id>`), sized by plan (free=120, pro=1000,
enterprise=10000 per window); orgless credentials (platform admin key,
anonymous dev identities) keep the prior per-identity behavior unchanged.

Adds GET /v1/orgs/usage (bucket status + total request count across an
org's keys) and a shared-bucket meter in the Orgs self-service console
panel.

Verified against a running dev server: 130 concurrent requests on a
free-plan org's owner key yielded exactly 120 successes + 10 real 429s,
and the org's teammate key immediately inherited the same depleted
remaining count (proving the bucket is shared, not per-key). A parallel
enterprise-plan org was unaffected. tsc + frontend build clean, 450/450
existing tests unchanged.
2026-10-24 00:40:11 +03:00

80 lines
3.0 KiB
TypeScript

/**
* Token-bucket rate limiter (p2 §7.1, security.md — rate limit all endpoints).
*
* Each identity (API key id, org id, or client IP) gets a bucket that refills
* at a steady rate up to a burst capacity. Smoother than fixed windows and
* cheap: O(1) per request, lazy refill, no background timers.
*
* `capacity` passed to the constructor is only the DEFAULT. Every call site
* may override it per-identity (`capacityOverride` on {@link check}/{@link
* peek}) so a single shared limiter instance can serve plan-tiered limits —
* e.g. an org's bucket sized by its `plan` — without allocating one
* `RateLimiter` per tier. The refill window (`windowMs`) is fixed at
* construction and shared by every identity; only the burst size varies.
*/
interface Bucket {
tokens: number;
lastRefill: number;
}
export interface RateLimitResult {
allowed: boolean;
remaining: number;
retryAfterMs: number;
}
export class RateLimiter {
private readonly buckets = new Map<string, Bucket>();
constructor(
private readonly capacity: number,
private readonly windowMs: number,
private readonly now: () => number = () => Date.now(),
) {}
private refill(identity: string, capacity: number): Bucket {
const t = this.now();
const bucket = this.buckets.get(identity) ?? { tokens: capacity, lastRefill: t };
const elapsed = t - bucket.lastRefill;
const refillPerMs = capacity / this.windowMs;
// Lazy refill since last seen, capped at the (possibly overridden) capacity —
// if a plan downgrade shrinks capacity below the bucket's current token
// count, clamp down rather than leaving a stale over-full bucket.
bucket.tokens = Math.min(capacity, bucket.tokens + elapsed * refillPerMs);
bucket.lastRefill = t;
return bucket;
}
check(identity: string, cost = 1, capacityOverride?: number): RateLimitResult {
const capacity = capacityOverride ?? this.capacity;
const bucket = this.refill(identity, capacity);
const refillPerMs = capacity / this.windowMs;
if (bucket.tokens >= cost) {
bucket.tokens -= cost;
this.buckets.set(identity, bucket);
return { allowed: true, remaining: Math.floor(bucket.tokens), retryAfterMs: 0 };
}
this.buckets.set(identity, bucket);
const deficit = cost - bucket.tokens;
return { allowed: false, remaining: 0, retryAfterMs: Math.ceil(deficit / refillPerMs) };
}
/** Read current bucket state without consuming a token — for usage/status displays. */
peek(identity: string, capacityOverride?: number): { remaining: number; limit: number } {
const capacity = capacityOverride ?? this.capacity;
const bucket = this.refill(identity, capacity);
this.buckets.set(identity, bucket);
return { remaining: Math.floor(bucket.tokens), limit: capacity };
}
/** Drop stale buckets to bound memory (call periodically). */
evictIdle(maxIdleMs: number): void {
const t = this.now();
for (const [id, b] of this.buckets) {
if (t - b.lastRefill > maxIdleMs) this.buckets.delete(id);
}
}
}