tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

158 lignes · 5439 octetsBrut · Historique
1/// Distribution's internals (ADR 0072): what the compiler tells the
2/// runtime about the types that cross hosts, in the format of
3/// docs/wire-format.md, the program's `@remote` entries, and the
4/// `[cluster]` policy of `louise.toml`. For std/dist, not for programs.
5///
6/// The compiler gives the bodies of the `@intrinsic` functions. A
7/// program that never calls `definitions` or `schema_hash` carries no
8/// table, one that never calls `entries` or `dispatch` no entry, and one
9/// that never calls `cluster_policy` no `[cluster]` policy.
10
11import std/process.{type Dynamic, type proc}
12import std/random.{type random}
13import std/string
14import std/time.{type clock}
15
16/// The descriptor of `a`: its `Wire` dictionary. `x` is not read.
17@intrinsic
18pub fn describe :: x: a -> String where Wire<a>
19
20/// The descriptor of `a`, with no value of it at hand: pass `None`. For a
21/// handle whose type parameter is all it says of `a` (`std/dist.Name`).
22@intrinsic
23pub fn describe_none :: none: Option<a> -> String where Wire<a>
24
25/// The program's type definitions table: every data type a descriptor of
26/// the program can name, in table text, ordered by name.
27@intrinsic
28pub fn definitions :: -> String
29
30/// The program's schema hash: the fixed hash of its definitions table, as
31/// 16 lowercase hex digits.
32@intrinsic
33pub fn schema_hash :: -> String
34
35/// A `@remote` function of the program: its key, its qualified name, and
36/// its argument's descriptor and type hash (16 lowercase hex digits).
37pub type Entry
38{ Entry(key: Int, name: String, descriptor: String, hash: String) }
39
40/// The program's `@remote` functions, ordered by key. A node registers
41/// their descriptors when it starts, to decode the arguments of the
42/// spawns it accepts.
43@intrinsic
44pub fn entries :: -> List<Entry>
45
46/// A value with its type's descriptor: what `dispatch` takes. Only
47/// `std/wire` makes one, with `arg` from a value whose `Wire` dictionary
48/// the type checker gives, and the runtime, from a value it decoded with
49/// that descriptor: the descriptor is always the value's.
50pub opaque type Arg { Arg(descriptor: String, value: Dynamic) }
51
52/// `x`, with its type's descriptor.
53pub fn arg :: x: a -> Arg where Wire<a>
54{
55 Arg(describe x, to_dynamic x)
56}
57
58/// Why `dispatch` refused.
59pub type Refused
60{ /// No `@remote` function of the program has this key.
61 UnknownEntry
62 /// The argument's type is not the entry's.
63, WrongType
64}
65
66/// The actor body that runs the `@remote` function `key` on `arg`, if
67/// `arg`'s descriptor is its argument's. The caller spawns it, plainly,
68/// linked or monitored. Code runs only through the program's own table of
69/// keys: no code address ever comes off the wire.
70pub fn dispatch ::
71 key: Int
72 arg: Arg
73 -> Result<send fn() -> () / <proc, clock, random, io, ffi>, Refused>
74{
75 match arg { Arg(descriptor, value) -> entry_body key descriptor value }
76}
77
78/// `dispatch`'s body: a switch over the keys, then a comparison with the
79/// entry's descriptor. Sound only because `value` is of the type
80/// `descriptor` describes, which `Arg` guarantees.
81@intrinsic
82fn entry_body ::
83 key: Int
84 descriptor: String
85 value: Dynamic
86 -> Result<send fn() -> () / <proc, clock, random, io, ffi>, Refused>
87
88@extern c "tl_coerce"
89fn to_dynamic :: x: a -> Dynamic
90
91/// The `[cluster]` section of the root package's `louise.toml` (ADR
92/// 0072), checked by the compiler, its defaults filled in: the policy
93/// every node of the program shares (`discovery` is `"seeds"` or
94/// `"mesh"`). A node's own name, certificates and seeds come from its
95/// environment, not from here.
96@derive Eq Debug
97pub type Cluster
98{ Cluster(
99 name: String
100 , listen: Option<String>
101 , heartbeat_ms: Int
102 , node_down_after_ms: Int
103 , max_message_bytes: Int
104 , outbound_queue_bytes: Int
105 , max_peers: Int
106 , discovery: String
107 )
108}
109
110/// The program's `[cluster]` policy, or `None` without the section.
111pub fn cluster :: -> Option<Cluster>
112{
113 match cluster_policy ()
114 { "" -> None, text -> Some(read_policy (string.split text "\n")) }
115}
116
117/// `[cluster]` as the compiler writes it: a `key=value` line, ended by
118/// `\n`, for each of `Cluster`'s fields in order, with `listen=` empty
119/// when the section has none; the empty string without the section.
120@intrinsic
121pub fn cluster_policy :: -> String
122
123fn read_policy :: lines: List<String> -> Cluster
124{
125 let listen = policy_value lines "listen"
126 Cluster( name: policy_value lines "name"
127 , listen: if listen == "" { None } else { Some(listen) }
128 , heartbeat_ms: policy_int lines "heartbeat_ms"
129 , node_down_after_ms: policy_int lines "node_down_after_ms"
130 , max_message_bytes: policy_int lines "max_message_bytes"
131 , outbound_queue_bytes: policy_int lines "outbound_queue_bytes"
132 , max_peers: policy_int lines "max_peers"
133 , discovery: policy_value lines "discovery"
134 )
135}
136
137/// The value of `key`'s line. The text is the compiler's: a missing key
138/// is a bug.
139fn policy_value :: lines: List<String> key: String -> String
140{
141 match lines
142 { [] -> panic ("wire.cluster: no `" <> key <> "` in the policy")
143 , [line, ..rest] -> if string.starts_with line (key <> "=")
144 {
145 string.slice line (string.length key + 1) (string.length line)
146 }
147 else
148 {
149 policy_value rest key
150 }
151 }
152}
153
154fn policy_int :: lines: List<String> key: String -> Int
155{
156 match string.to_int (policy_value lines key)
157 { Ok(n) -> n, Error(e) -> panic ("wire.cluster: " <> key <> ": " <> e) }
158}