tobogganSe connecter

louise

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

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