
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | /// Stateful actors in the style of OTP's gen_server. |
| 2 | /// |
| 3 | /// let counter = actor.start(0, fn(n, msg) { ... }) |
| 4 | /// let value = actor.call(counter, Get, 1000) |
| 5 | |
| 6 | import std/file.{type fs} |
| 7 | import std/net.{type net} |
| 8 | import std/option |
| 9 | import std/process.{type Subject, type proc} |
| 10 | import std/random.{type random} |
| 11 | import std/time.{type clock} |
| 12 | |
| 13 | /// What a message handler decides. |
| 14 | pub type Next<s> { Continue(s), Stop } |
| 15 | |
| 16 | /// Start an actor with state `init`; `on_message` processes each message. |
| 17 | /// Returns the subject to send it messages. |
| 18 | pub fn start :: |
| 19 | init: s |
| 20 | on_message: send fn(s, msg) |
| 21 | -> Next<s> / <proc, clock, random, fs, net, io, ffi> |
| 22 | -> Subject<msg> / <proc> |
| 23 | where Send<s> |
| 24 | { |
| 25 | let ready = process.subject () |
| 26 | { |
| 27 | use <- process.spawn |
| 28 | let inbox = process.subject () |
| 29 | process.send ready inbox |
| 30 | serve inbox init on_message |
| 31 | } |
| 32 | process.receive_forever ready |
| 33 | } |
| 34 | |
| 35 | fn serve :: |
| 36 | inbox: Subject<msg> |
| 37 | state: s |
| 38 | on_message: fn(s, msg) -> Next<s> / <proc, clock, random, fs, net, io, ffi> |
| 39 | -> () / <proc, clock, random, fs, net, io, ffi> |
| 40 | { |
| 41 | match on_message state (process.receive_forever inbox) |
| 42 | { Continue(next) -> serve inbox next on_message, Stop -> () } |
| 43 | } |
| 44 | |
| 45 | /// Why `try_call` got no answer. |
| 46 | @derive Eq Show |
| 47 | pub type CallError |
| 48 | { /// No reply within the timeout: the actor is busy, stuck, or stopped. |
| 49 | Timeout |
| 50 | } |
| 51 | |
| 52 | /// Send a request carrying a reply subject and wait up to `timeout` |
| 53 | /// milliseconds for the answer: `Error(Timeout)` without one. |
| 54 | /// |
| 55 | /// A reply that arrives after the timeout stays in the caller's mailbox |
| 56 | /// until the caller stops: the process API cannot close a subject yet. |
| 57 | pub fn try_call :: |
| 58 | to: Subject<msg> |
| 59 | request: fn(Subject<r>) -> msg |
| 60 | timeout: Int |
| 61 | -> Result<r, CallError> / <proc> |
| 62 | where Send<msg> |
| 63 | { |
| 64 | let reply = process.subject () |
| 65 | process.send to (request reply) |
| 66 | process.receive reply timeout |> option.to_result Timeout |
| 67 | } |
| 68 | |
| 69 | /// `try_call` for an actor that must answer: panics after `timeout` |
| 70 | /// milliseconds without a reply. |
| 71 | pub fn call :: |
| 72 | to: Subject<msg> |
| 73 | request: fn(Subject<r>) -> msg |
| 74 | timeout: Int |
| 75 | -> r / <proc> |
| 76 | where Send<msg> |
| 77 | { |
| 78 | match try_call to request timeout |
| 79 | { Ok(answer) -> answer |
| 80 | , Error(Timeout) -> panic "actor.call: no reply within the timeout" |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// Send a message without waiting. |
| 85 | pub fn cast :: to: Subject<msg> message: msg -> () / <proc> where Send<msg> |
| 86 | { |
| 87 | process.send to message |
| 88 | } |