import log.{type log} import postgres as pg import resilience/rate_limit.{type Admission, type RateLimit, Admitted, Wait} import shared.{type Store} import std/actor.{Continue} import std/process.{type Subject, type proc} import std/time.{type clock} @test import std/list @test import std/test.{assert_equal} /// Whether one more request under `key` is within its rate, or how long /// until it would be. pub effect throttle { fn admission :: key: String -> Admission } /// Rates per key: each key may make `per_minute` requests a minute, /// refilled continuously (a token bucket, `resilience/rate_limit`). A /// local throttle keeps its buckets in one actor of this instance; a /// shared one keeps them in PostgreSQL (`throttle/shared`), so every /// instance draws on the same bucket, and falls back to its local buckets /// while the database fails (ADR 0081). pub opaque type Throttle { Throttle( local: Subject , shared: Option<(pg.Pool, Store)> , per_minute: Int ) } type Msg { Admit(key: String, now_ms: Int, reply: Subject) /// Whether a failure of the shared buckets should be logged now: once /// a minute at most. , Warn(now_ms: Int, reply: Subject) } type Local { Local(limit: RateLimit, warned_ms: Int) } /// A throttle of `per_minute` requests per key, in this instance; 0 /// admits everything. pub fn start :: per_minute: Int -> Throttle / { Throttle(local_actor per_minute, None, per_minute) } /// A throttle of `per_minute` requests per key, shared by every instance /// through `store`'s table in `pool`. pub fn shared :: per_minute: Int pool: pg.Pool store: Store -> Throttle / { Throttle(local_actor per_minute, Some((pool, store)), per_minute) } fn local_actor :: per_minute: Int -> Subject / { actor.start (Local(rate_limit.per_minute per_minute, 0 - 60000)) fn local msg { Continue(handled local msg) } } pub fn with_throttle :: throttle: Throttle action: fn() -> a / -> a / { let locally = fn key { let now_ms = time.unix_ms (time.now ()) actor.call throttle.local Admit(key, now_ms, _) 5000 } handle { action () } with { admission key -> match throttle.shared { None -> locally key , Some ( (pool, store) ) -> match shared.admission pool store throttle.per_minute key { Ok(answer) -> answer , Error(why) -> { let now_ms = time.unix_ms (time.now ()) if actor.call throttle.local Warn(now_ms, _) 5000 { log.warn "throttle: the shared buckets failed ({why}); limiting per instance" [] } locally key } } } } } fn handled :: local: Local msg: Msg -> Local / { match msg { Admit(key, now_ms, reply) -> { let (kept, answer) = rate_limit.taken local.limit key now_ms process.send reply answer Local(..local, limit: kept) } , Warn ( now_ms , reply ) -> if now_ms - local.warned_ms >= 60000 { process.send reply True Local(..local, warned_ms: now_ms) } else { process.send reply False local } } } @test fn a_key_gets_its_rate_and_then_waits :: -> () / { let throttle = start 3 let tries = fn at { time.with_fixed_clock (time.from_unix_ms at) fn { log.silently fn { with_throttle throttle fn { admission "a" } } } } assert_equal (list.map [0, 0, 0, 0] tries) [Admitted, Admitted, Admitted, Wait(20)] assert_equal (tries 20000) Admitted let other = time.with_fixed_clock (time.from_unix_ms 0) fn { log.silently fn { with_throttle throttle fn { admission "b" } } } assert_equal other Admitted }