tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

143 lignes · 3809 octetsBrut · Historique
1import log.{type log}
2import postgres as pg
3import resilience/rate_limit.{type Admission, type RateLimit, Admitted, Wait}
4import shared.{type Store}
5import std/actor.{Continue}
6import std/process.{type Subject, type proc}
7import std/time.{type clock}
8@test import std/list
9@test import std/test.{assert_equal}
10
11/// Whether one more request under `key` is within its rate, or how long
12/// until it would be.
13pub effect throttle
14{
15 fn admission :: key: String -> Admission
16}
17
18/// Rates per key: each key may make `per_minute` requests a minute,
19/// refilled continuously (a token bucket, `resilience/rate_limit`). A
20/// local throttle keeps its buckets in one actor of this instance; a
21/// shared one keeps them in PostgreSQL (`throttle/shared`), so every
22/// instance draws on the same bucket, and falls back to its local buckets
23/// while the database fails (ADR 0081).
24pub opaque type Throttle
25{ Throttle(
26 local: Subject<Msg>
27 , shared: Option<(pg.Pool, Store)>
28 , per_minute: Int
29 )
30}
31
32type Msg
33{ Admit(key: String, now_ms: Int, reply: Subject<Admission>)
34 /// Whether a failure of the shared buckets should be logged now: once
35 /// a minute at most.
36, Warn(now_ms: Int, reply: Subject<Bool>)
37}
38
39type Local { Local(limit: RateLimit, warned_ms: Int) }
40
41/// A throttle of `per_minute` requests per key, in this instance; 0
42/// admits everything.
43pub fn start :: per_minute: Int -> Throttle / <proc>
44{
45 Throttle(local_actor per_minute, None, per_minute)
46}
47
48/// A throttle of `per_minute` requests per key, shared by every instance
49/// through `store`'s table in `pool`.
50pub fn shared :: per_minute: Int pool: pg.Pool store: Store -> Throttle / <proc>
51{
52 Throttle(local_actor per_minute, Some((pool, store)), per_minute)
53}
54
55fn local_actor :: per_minute: Int -> Subject<Msg> / <proc>
56{
57 actor.start
58 (Local(rate_limit.per_minute per_minute, 0 - 60000))
59 fn local msg { Continue(handled local msg) }
60}
61
62pub fn with_throttle ::
63 throttle: Throttle
64 action: fn() -> a / <throttle, proc, clock, log | e>
65 -> a / <proc, clock, log | e>
66{
67 let locally = fn key
68 {
69 let now_ms = time.unix_ms (time.now ())
70 actor.call throttle.local Admit(key, now_ms, _) 5000
71 }
72 handle
73 {
74 action ()
75 }
76 with
77 { admission key -> match throttle.shared
78 { None -> locally key
79 , Some
80 ( (pool, store)
81 ) -> match shared.admission pool store throttle.per_minute key
82 { Ok(answer) -> answer
83 , Error(why) ->
84 {
85 let now_ms = time.unix_ms (time.now ())
86 if actor.call throttle.local Warn(now_ms, _) 5000
87 {
88 log.warn
89 "throttle: the shared buckets failed ({why}); limiting per instance"
90 []
91 }
92 locally key
93 }
94 }
95 }
96 }
97}
98
99fn handled :: local: Local msg: Msg -> Local / <proc>
100{
101 match msg
102 { Admit(key, now_ms, reply) ->
103 {
104 let (kept, answer) = rate_limit.taken local.limit key now_ms
105 process.send reply answer
106 Local(..local, limit: kept)
107 }
108 , Warn
109 ( now_ms
110 , reply
111 ) -> if now_ms - local.warned_ms >= 60000
112 {
113 process.send reply True
114 Local(..local, warned_ms: now_ms)
115 }
116 else
117 {
118 process.send reply False
119 local
120 }
121 }
122}
123
124@test
125fn a_key_gets_its_rate_and_then_waits :: -> () / <proc>
126{
127 let throttle = start 3
128 let tries = fn at
129 {
130 time.with_fixed_clock
131 (time.from_unix_ms at)
132 fn { log.silently fn { with_throttle throttle fn { admission "a" } } }
133 }
134 assert_equal
135 (list.map [0, 0, 0, 0] tries)
136 [Admitted, Admitted, Admitted, Wait(20)]
137 assert_equal (tries 20000) Admitted
138 let other =
139 time.with_fixed_clock
140 (time.from_unix_ms 0)
141 fn { log.silently fn { with_throttle throttle fn { admission "b" } } }
142 assert_equal other Admitted
143}