
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/option |
| 8 | import std/process.{type Subject, type proc} |
| 9 | import std/random.{type random} |
| 10 | import std/time.{type clock} |
| 11 | |
| 12 | /// What a message handler decides. |
| 13 | pub type Next<s> { Continue(s), Stop } |
| 14 | |
| 15 | /// Start an actor with state `init`; `on_message` processes each message. |
| 16 | /// Returns the subject to send it messages. |
| 17 | pub fn start :: |
| 18 | init: s |
| 19 | on_message: send fn(s, msg) -> Next<s> / <proc, clock, random, fs, io, ffi> |
| 20 | -> Subject<msg> / <proc> |
| 21 | where Send<s> |
| 22 | { |
| 23 | let ready = process.subject () |
| 24 | { |
| 25 | use <- process.spawn |
| 26 | let inbox = process.subject () |
| 27 | process.send ready inbox |
| 28 | serve inbox init on_message |
| 29 | } |
| 30 | process.receive_forever ready |
| 31 | } |
| 32 | |
| 33 | fn serve :: |
| 34 | inbox: Subject<msg> |
| 35 | state: s |
| 36 | on_message: fn(s, msg) -> Next<s> / <proc, clock, random, fs, io, ffi> |
| 37 | -> () / <proc, clock, random, fs, io, ffi> |
| 38 | { |
| 39 | match on_message state (process.receive_forever inbox) |
| 40 | { Continue(next) -> serve inbox next on_message, Stop -> () } |
| 41 | } |
| 42 | |
| 43 | /// Why `try_call` got no answer. |
| 44 | @derive Eq Show |
| 45 | pub type CallError |
| 46 | { /// No reply within the timeout: the actor is busy, stuck, or stopped. |
| 47 | Timeout |
| 48 | } |
| 49 | |
| 50 | /// Send a request carrying a reply subject and wait up to `timeout` |
| 51 | /// milliseconds for the answer: `Error(Timeout)` without one. |
| 52 | /// |
| 53 | /// A reply that arrives after the timeout stays in the caller's mailbox |
| 54 | /// until the caller stops: the process API cannot close a subject yet. |
| 55 | pub fn try_call :: |
| 56 | to: Subject<msg> |
| 57 | request: fn(Subject<r>) -> msg |
| 58 | timeout: Int |
| 59 | -> Result<r, CallError> / <proc> |
| 60 | where Send<msg> |
| 61 | { |
| 62 | let reply = process.subject () |
| 63 | process.send to (request reply) |
| 64 | process.receive reply timeout |> option.to_result Timeout |
| 65 | } |
| 66 | |
| 67 | /// `try_call` for an actor that must answer: panics after `timeout` |
| 68 | /// milliseconds without a reply. |
| 69 | pub fn call :: |
| 70 | to: Subject<msg> |
| 71 | request: fn(Subject<r>) -> msg |
| 72 | timeout: Int |
| 73 | -> r / <proc> |
| 74 | where Send<msg> |
| 75 | { |
| 76 | match try_call to request timeout |
| 77 | { Ok(answer) -> answer |
| 78 | , Error(Timeout) -> panic "actor.call: no reply within the timeout" |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Send a message without waiting. |
| 83 | pub fn cast :: to: Subject<msg> message: msg -> () / <proc> where Send<msg> |
| 84 | { |
| 85 | process.send to message |
| 86 | } |