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.

122 lignes · 3025 octetsBrut · Historique
1/// At most `limit` jobs at once (ADR 0081): a job that finds every slot
2/// taken is refused at once (`Full`) instead of waiting, so a burst of
3/// costly work (password hashing) cannot pile up. A slot is held while
4/// its job runs, and freed after `lifetime_ms` (30 s with `start`) even
5/// when its holder died without leaving.
6///
7/// let hashing = gate.start 4
8/// match gate.admitted hashing fn { argon2.hash password }
9/// { Ok(hash) -> ..., Error(gate.Full) -> ... } // 503, try again
10
11import std/actor.{Continue}
12import std/list
13import std/process.{type Subject, type proc}
14import std/time
15@test import std/test.{assert_equal}
16
17pub opaque type Gate { Gate(Subject<Msg>) }
18
19@derive Eq Debug Show
20pub type Full { Full }
21
22type Msg { Enter(reply: Subject<Option<Int>>), Leave(slot: Int) }
23
24type Slots
25{ Slots(held: List<(Int, Int)>, next: Int, limit: Int, lifetime: Int) }
26
27pub fn start :: limit: Int -> Gate / <proc>
28{
29 start_with limit 30000
30}
31
32pub fn start_with :: limit: Int lifetime_ms: Int -> Gate / <proc>
33{
34 Gate( actor.start
35 Slots([], 0, limit, lifetime_ms)
36 fn slots msg { Continue(handled slots msg) }
37 )
38}
39
40pub fn admitted ::
41 gate: Gate
42 work: fn() -> a / <proc | e>
43 -> Result<a, Full> / <proc | e>
44{
45 let Gate(subject) = gate
46 match actor.call subject Enter 5000
47 { Some(slot) ->
48 {
49 let outcome = work ()
50 actor.cast subject Leave(slot)
51 Ok(outcome)
52 }
53 , None -> Error(Full)
54 }
55}
56
57fn handled :: slots: Slots msg: Msg -> Slots / <proc, time.clock>
58{
59 let now = time.in_milliseconds (time.monotonic ())
60 let live = list.filter slots.held fn held { held.1 > now }
61 match msg
62 { Enter
63 ( reply
64 ) -> if list.length live < slots.limit
65 {
66 process.send reply Some(slots.next)
67 Slots( ..slots
68 , held: [(slots.next, now + slots.lifetime), ..live]
69 , next: slots.next + 1
70 )
71 }
72 else
73 {
74 process.send reply None
75 Slots(..slots, held: live)
76 }
77 , Leave(slot) ->
78 Slots(..slots, held: list.filter live fn held { held.0 != slot })
79 }
80}
81
82@test
83fn work_is_admitted_while_slots_are_free :: -> () / <proc>
84{
85 let open = start 2
86 assert_equal (admitted open fn { 1 }) Ok(1)
87 assert_equal (admitted open fn { 2 }) Ok(2)
88}
89
90@test
91fn work_beyond_the_limit_is_refused_at_once :: -> () / <proc>
92{
93 let narrow = start 1
94 let _ =
95 process.spawn
96 fn
97 {
98 let _ = admitted narrow fn { process.sleep 1000 }
99 ()
100 }
101 process.sleep 10
102 assert_equal (admitted narrow fn { "late" }) Error(Full)
103 process.sleep 2000
104 assert_equal (admitted narrow fn { "later" }) Ok("later")
105}
106
107@test
108fn a_slot_held_past_its_lifetime_is_freed :: -> () / <proc>
109{
110 let narrow = start_with 1 500
111 let _ =
112 process.spawn
113 fn
114 {
115 let _ = admitted narrow fn { process.sleep 100000 }
116 ()
117 }
118 process.sleep 10
119 assert_equal (admitted narrow fn { "blocked" }) Error(Full)
120 process.sleep 1000
121 assert_equal (admitted narrow fn { "freed" }) Ok("freed")
122}