louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | import std/io |
| 2 | import std/process. |
| 3 | { type Pid |
| 4 | , type Subject |
| 5 | , type proc |
| 6 | , Crashed |
| 7 | , Down |
| 8 | , ExitSignal |
| 9 | , Killed |
| 10 | , Normal |
| 11 | , Shutdown |
| 12 | } |
| 13 | import std/supervisor. |
| 14 | {ChildWith, OneForOne, Permanent, Spec, Temporary, Transient} |
| 15 | |
| 16 | // Supervisors with windowed restart intensity, child kinds and an ordered |
| 17 | // shutdown, and `process.stop` (ADR 0060). Children report to the main |
| 18 | // actor, which prints; every step waits for the one before, so the order |
| 19 | // is the same on any number of threads. |
| 20 | |
| 21 | fn reason :: r: process.ExitReason -> String |
| 22 | { |
| 23 | match r |
| 24 | { Normal -> "normal" |
| 25 | , Killed -> "killed" |
| 26 | , Crashed(m) -> "crashed: " <> m |
| 27 | , Shutdown -> "shutdown" |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn wait_down :: down: Subject<process.Down> -> String / <proc> |
| 32 | { |
| 33 | let Down(_, why) = process.receive_forever down |
| 34 | reason why |
| 35 | } |
| 36 | |
| 37 | /// An actor counting attempts: each `Subject<Int>` sent gets the next. |
| 38 | fn counter :: -> Subject<Subject<Int>> / <proc> |
| 39 | { |
| 40 | let ready = process.subject () |
| 41 | let _ = |
| 42 | process.spawn |
| 43 | fn |
| 44 | { |
| 45 | let asks = process.subject () |
| 46 | process.send ready asks |
| 47 | count asks 1 |
| 48 | } |
| 49 | process.receive_forever ready |
| 50 | } |
| 51 | |
| 52 | fn count :: asks: Subject<Subject<Int>> n: Int -> () / <proc> |
| 53 | { |
| 54 | let reply = process.receive_forever asks |
| 55 | process.send reply n |
| 56 | count asks (n + 1) |
| 57 | } |
| 58 | |
| 59 | fn attempt :: attempts: Subject<Subject<Int>> -> Int / <proc> |
| 60 | { |
| 61 | let reply = process.subject () |
| 62 | process.send attempts reply |
| 63 | process.receive_forever reply |
| 64 | } |
| 65 | |
| 66 | /// A child that tells `log` it started, then, trapping exits, waits to be |
| 67 | /// stopped and tells `log` it stops. |
| 68 | fn polite :: name: String log: Subject<String> -> () / <proc> |
| 69 | { |
| 70 | process.trap_exits True |
| 71 | process.send log (name <> " started") |
| 72 | let ExitSignal(_, why) = process.receive_forever (process.exits ()) |
| 73 | process.send log (name <> " stopping (" <> reason why <> ")") |
| 74 | } |
| 75 | |
| 76 | fn print_next :: log: Subject<String> n: Int -> () / <io, proc> |
| 77 | { |
| 78 | if n > 0 |
| 79 | { |
| 80 | io.println (process.receive_forever log) |
| 81 | print_next log (n - 1) |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | () |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn wait_for :: log: Subject<String> n: Int -> () / <proc> |
| 90 | { |
| 91 | if n > 0 |
| 92 | { |
| 93 | let _ = process.receive_forever log |
| 94 | wait_for log (n - 1) |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | () |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn main :: -> () / <io, proc> |
| 103 | { |
| 104 | // `stop` stops an actor that does not trap exits... |
| 105 | let sleeper = |
| 106 | { |
| 107 | use <- process.spawn |
| 108 | let never: Subject<Int> = process.subject () |
| 109 | let _ = process.receive_forever never |
| 110 | } |
| 111 | let down = process.monitor sleeper |
| 112 | process.stop sleeper |
| 113 | io.println ("sleeper: " <> wait_down down) |
| 114 | //> sleeper: shutdown |
| 115 | // ...and tells one that does, which cleans up and returns. |
| 116 | let log = process.subject () |
| 117 | let careful = |
| 118 | { |
| 119 | use <- process.spawn |
| 120 | polite "careful" log |
| 121 | } |
| 122 | let down = process.monitor careful |
| 123 | print_next log 1 |
| 124 | process.stop careful |
| 125 | print_next log 1 |
| 126 | io.println ("careful: " <> wait_down down) |
| 127 | //> careful started |
| 128 | //> careful stopping (shutdown) |
| 129 | //> careful: normal |
| 130 | |
| 131 | // More than 3 restarts within a second: the supervisor gives up. |
| 132 | let (_, fast_down) = |
| 133 | { |
| 134 | use <- process.spawn_monitor |
| 135 | supervisor.run |
| 136 | Spec( OneForOne |
| 137 | , [ ChildWith( "fast" |
| 138 | , fn |
| 139 | { |
| 140 | process.send log "fast started" |
| 141 | panic "fast failed" |
| 142 | } |
| 143 | , Permanent |
| 144 | , 1000 |
| 145 | ) |
| 146 | ] |
| 147 | , 3 |
| 148 | , 1000 |
| 149 | ) |
| 150 | } |
| 151 | print_next log 4 |
| 152 | io.println ("fast supervisor: " <> wait_down fast_down) |
| 153 | //> fast started |
| 154 | //> fast started |
| 155 | //> fast started |
| 156 | //> fast started |
| 157 | //> fast supervisor: crashed: supervisor: too many restarts |
| 158 | // The same crashes spread over more than the window: it keeps going. |
| 159 | // (The clock is virtual: no socket is open.) |
| 160 | let attempts = counter () |
| 161 | let (_, slow_down) = |
| 162 | { |
| 163 | use <- process.spawn_monitor |
| 164 | supervisor.run |
| 165 | Spec( OneForOne |
| 166 | , [ ChildWith( "slow" |
| 167 | , fn |
| 168 | { |
| 169 | let n = attempt attempts |
| 170 | process.send log ("slow attempt " <> show n) |
| 171 | process.sleep 600 |
| 172 | if n < 4 |
| 173 | { |
| 174 | panic "slow failed" |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | () |
| 179 | } |
| 180 | } |
| 181 | , Transient |
| 182 | , 1000 |
| 183 | ) |
| 184 | ] |
| 185 | , 1 |
| 186 | , 500 |
| 187 | ) |
| 188 | } |
| 189 | print_next log 4 |
| 190 | io.println ("slow supervisor: " <> wait_down slow_down) |
| 191 | //> slow attempt 1 |
| 192 | //> slow attempt 2 |
| 193 | //> slow attempt 3 |
| 194 | //> slow attempt 4 |
| 195 | //> slow supervisor: normal |
| 196 | |
| 197 | // Kinds: a temporary child is not restarted after a crash (and its |
| 198 | // supervisor, left without children, returns)... |
| 199 | let (_, once_down) = |
| 200 | { |
| 201 | use <- process.spawn_monitor |
| 202 | supervisor.run |
| 203 | Spec( OneForOne |
| 204 | , [ ChildWith( "once" |
| 205 | , fn |
| 206 | { |
| 207 | process.send log "temporary ran" |
| 208 | panic "temporary failed" |
| 209 | } |
| 210 | , Temporary |
| 211 | , 1000 |
| 212 | ) |
| 213 | ] |
| 214 | , 5 |
| 215 | , 60000 |
| 216 | ) |
| 217 | } |
| 218 | print_next log 1 |
| 219 | io.println ("temporary supervisor: " <> wait_down once_down) |
| 220 | //> temporary ran |
| 221 | //> temporary supervisor: normal |
| 222 | // ...a permanent one is restarted after returning. |
| 223 | let returns = counter () |
| 224 | let (kinds, kinds_down) = |
| 225 | { |
| 226 | use <- process.spawn_monitor |
| 227 | supervisor.run |
| 228 | Spec( OneForOne |
| 229 | , [ ChildWith( "again" |
| 230 | , fn |
| 231 | { |
| 232 | let n = attempt returns |
| 233 | if n < 3 |
| 234 | { |
| 235 | process.send log ("permanent returned " <> show n) |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | polite "permanent" log |
| 240 | } |
| 241 | } |
| 242 | , Permanent |
| 243 | , 1000 |
| 244 | ) |
| 245 | ] |
| 246 | , 5 |
| 247 | , 60000 |
| 248 | ) |
| 249 | } |
| 250 | print_next log 3 |
| 251 | process.stop kinds |
| 252 | print_next log 1 |
| 253 | io.println ("permanent supervisor: " <> wait_down kinds_down) |
| 254 | //> permanent returned 1 |
| 255 | //> permanent returned 2 |
| 256 | //> permanent started |
| 257 | //> permanent stopping (shutdown) |
| 258 | //> permanent supervisor: normal |
| 259 | |
| 260 | // An ordered shutdown: last started, first stopped; a nested supervisor |
| 261 | // stops its own children first; a child that ignores the shutdown is |
| 262 | // killed after its timeout. |
| 263 | let stubborn_pid = process.subject () |
| 264 | let (tree, tree_down) = |
| 265 | { |
| 266 | use <- process.spawn_monitor |
| 267 | supervisor.run |
| 268 | Spec( OneForOne |
| 269 | , [ ChildWith("a", fn { polite "a" log }, Permanent, 1000) |
| 270 | , ChildWith( "inner" |
| 271 | , fn |
| 272 | { |
| 273 | supervisor.run |
| 274 | Spec( OneForOne |
| 275 | , [ChildWith("leaf", fn { polite "leaf" log }, Permanent, 1000)] |
| 276 | , 1 |
| 277 | , 1000 |
| 278 | ) |
| 279 | } |
| 280 | , Permanent |
| 281 | , 5000 |
| 282 | ) |
| 283 | , ChildWith( "stubborn" |
| 284 | , fn |
| 285 | { |
| 286 | process.trap_exits True |
| 287 | process.send stubborn_pid (process.self ()) |
| 288 | let never: Subject<Int> = process.subject () |
| 289 | let _ = process.receive_forever never |
| 290 | } |
| 291 | , Permanent |
| 292 | , 200 |
| 293 | ) |
| 294 | ] |
| 295 | , 1 |
| 296 | , 1000 |
| 297 | ) |
| 298 | } |
| 299 | // `a` and `leaf` start in either order. |
| 300 | wait_for log 2 |
| 301 | let stubborn = process.receive_forever stubborn_pid |
| 302 | let stubborn_down = process.monitor stubborn |
| 303 | process.stop tree |
| 304 | let Down(_, why) = process.receive_forever stubborn_down |
| 305 | io.println ("stubborn: " <> reason why) |
| 306 | print_next log 2 |
| 307 | io.println ("tree supervisor: " <> wait_down tree_down) |
| 308 | //> stubborn: killed |
| 309 | //> leaf stopping (shutdown) |
| 310 | //> a stopping (shutdown) |
| 311 | //> tree supervisor: normal |
| 312 | } |