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.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 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 | |
| 10 | import postgres as pg |
| 11 | import resilience/rate_limit.{type Admission, Admitted, Wait} |
| 12 | import std/list |
| 13 | import std/process.{type proc} |
| 14 | import std/result |
| 15 | import 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). |
| 21 | pub opaque type Store { Store(table: String, key_column: String) } |
| 22 | |
| 23 | /// The table `rates`, keyed by `key`. |
| 24 | pub 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. |
| 31 | pub 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 | |
| 44 | fn 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 | |
| 55 | fn lower :: c: String -> Bool |
| 56 | { |
| 57 | c >= "a" && c <= "z" |
| 58 | } |
| 59 | |
| 60 | fn 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. |
| 66 | pub 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. |
| 72 | pub 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. |
| 107 | fn 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) |
| 118 | values ($1, $2::bigint - $3::bigint, (extract(epoch from clock_timestamp()) * 1000)::bigint) |
| 119 | on conflict ({k}) do update |
| 120 | set units = least($2::bigint, r.units + (excluded.at_ms - r.at_ms) * $4::bigint) - $3::bigint, |
| 121 | at_ms = excluded.at_ms |
| 122 | where least($2::bigint, r.units + (excluded.at_ms - r.at_ms) * $4::bigint) >= $3::bigint |
| 123 | returning 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. |
| 129 | fn 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 |
| 132 | from {store.table} where {store.key_column} = $1" |
| 133 | , [pg.text key, pg.int full, pg.int per_minute] |
| 134 | ) |
| 135 | } |
| 136 | |
| 137 | fn 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 |
| 149 | fn 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 | } |