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.

160 lignes · 4922 octetsBrut · Historique
1/// Rate buckets every instance shares, in PostgreSQL (ADR 0081): one row
2/// per key in a table of the app's (`Store`), the same token bucket as
3/// `resilience/rate_limit` (`per_minute` units a millisecond, a minute's
4/// worth at most, `rate_limit.cost` a request), timed by the database's
5/// clock so that instances' clocks never disagree. One atomic statement
6/// admits or refuses; a refusal reads how long until the next request is
7/// paid for. An error is the caller's to handle (`throttle` falls back to
8/// its own buckets).
9
10import postgres as pg
11import resilience/rate_limit.{type Admission, Admitted, Wait}
12import std/list
13import std/process.{type proc}
14import std/result
15import std/string
16@test import std/test.{assert_equal}
17
18/// Where the buckets are: a table and its key column. Both are SQL
19/// identifiers checked when the store is made, since they are part of the
20/// statement's text (only values can be its parameters).
21pub opaque type Store { Store(table: String, key_column: String) }
22
23/// The table `rates`, keyed by `key`.
24pub fn rates :: -> Store
25{
26 Store("rates", "key")
27}
28
29/// The table `table`, keyed by `key_column`: lowercase letters, digits and
30/// `_`, starting with a letter or `_`, 63 bytes at most.
31pub fn store :: table: String key_column: String -> Result<Store, String>
32{
33 if identifier table && identifier key_column
34 {
35 Ok(Store(table, key_column))
36 }
37 else
38 {
39 Error( "a rate table and its key column are plain SQL names: {table}, {key_column}"
40 )
41 }
42}
43
44fn identifier :: name: String -> Bool
45{
46 let chars = string.chars name
47 match chars
48 { [] -> False
49 , [first, ..] ->
50 string.length name <= 63 && (lower first || first == "_")
51 && list.all chars fn c { lower c || digit c || c == "_" }
52 }
53}
54
55fn lower :: c: String -> Bool
56{
57 c >= "a" && c <= "z"
58}
59
60fn digit :: c: String -> Bool
61{
62 c >= "0" && c <= "9"
63}
64
65/// The statement that makes the store's table, for the app's schema.
66pub fn create_table :: store: Store -> String
67{
68 "create table if not exists {store.table} ({store.key_column} text primary key, units bigint not null, at_ms bigint not null)"
69}
70
71/// One request under `key`, against the shared bucket.
72pub fn admission ::
73 pool: pg.Pool
74 store: Store
75 per_minute: Int
76 key: String
77 -> Result<Admission, String> / <proc>
78{
79 if per_minute <= 0
80 {
81 Ok(Admitted)
82 }
83 else
84 {
85 let full = per_minute * rate_limit.cost ()
86 let cost = rate_limit.cost ()
87 use taken <- result.try (rows pool (taking store key full cost per_minute))
88 match taken
89 { [_, ..] -> Ok(Admitted)
90 , [] ->
91 {
92 use left <- result.try (rows pool (left store key full per_minute))
93 match left
94 { [row, ..] -> match pg.row_int row "units"
95 { Ok(Some(units)) -> Ok(Wait(rate_limit.wait_for per_minute units))
96 , _ -> Error("{store.table}: no units for {key}")
97 }
98 , [] -> Ok(Wait(1))
99 }
100 }
101 }
102 }
103}
104
105/// Take one request's units if the bucket has them, refilled since its
106/// last request; no row comes back when it has not.
107fn taking ::
108 store: Store
109 key: String
110 full: Int
111 cost: Int
112 per_minute: Int
113 -> pg.Statement
114{
115 let t = store.table
116 let k = store.key_column
117 pg.Statement( "insert into {t} as r ({k}, units, at_ms)
118values ($1, $2::bigint - $3::bigint, (extract(epoch from clock_timestamp()) * 1000)::bigint)
119on conflict ({k}) do update
120set units = least($2::bigint, r.units + (excluded.at_ms - r.at_ms) * $4::bigint) - $3::bigint,
121 at_ms = excluded.at_ms
122where least($2::bigint, r.units + (excluded.at_ms - r.at_ms) * $4::bigint) >= $3::bigint
123returning units"
124 , [pg.text key, pg.int full, pg.int cost, pg.int per_minute]
125 )
126}
127
128/// The units a refused bucket holds now.
129fn left :: store: Store key: String full: Int per_minute: Int -> pg.Statement
130{
131 pg.Statement( "select least($2::bigint, units + ((extract(epoch from clock_timestamp()) * 1000)::bigint - at_ms) * $3::bigint) as units
132from {store.table} where {store.key_column} = $1"
133 , [pg.text key, pg.int full, pg.int per_minute]
134 )
135}
136
137fn rows ::
138 pool: pg.Pool
139 statement: pg.Statement
140 -> Result<List<pg.Row>, String> / <proc>
141{
142 pg.with_connection pool fn { pg.query_statement statement }
143 |> result.flatten
144 |> result.map fn found { found.rows }
145 |> result.map_error pg.error_message
146}
147
148@test
149fn a_store_names_only_plain_sql_names ::
150{
151 assert_equal (store "api_rates" "agent" |> result.is_ok) True
152 assert_equal (store "_r2" "k_1" |> result.is_ok) True
153 assert_equal (store "rates; drop table x" "key" |> result.is_ok) False
154 assert_equal (store "Rates" "key" |> result.is_ok) False
155 assert_equal (store "rates" "" |> result.is_ok) False
156 assert_equal (store "9rates" "key" |> result.is_ok) False
157 assert_equal
158 (create_table (rates ()))
159 "create table if not exists rates (key text primary key, units bigint not null, at_ms bigint not null)"
160}