/// Distribution's internals (ADR 0072): what the compiler tells the
/// runtime about the types that cross hosts, in the format of
/// docs/wire-format.md, the program's `@remote` entries, and the
/// `[cluster]` policy of `louise.toml`. For std/dist, not for programs.
///
/// The compiler gives the bodies of the `@intrinsic` functions. A
/// program that never calls `definitions` or `schema_hash` carries no
/// table, one that never calls `entries` or `dispatch` no entry, and one
/// that never calls `cluster_policy` no `[cluster]` policy.
import std/file.{type fs}
import std/net.{type net}
import std/process.{type Dynamic, type proc}
import std/random.{type random}
import std/string
import std/time.{type clock}
/// The descriptor of `a`: its `Wire` dictionary. `x` is not read.
@intrinsic
pub fn describe :: x: a -> String where Wire
/// The descriptor of `a`, with no value of it at hand: pass `None`. For a
/// handle whose type parameter is all it says of `a` (`std/dist.Name`).
@intrinsic
pub fn describe_none :: none: Option -> String where Wire
/// The program's type definitions table: every data type a descriptor of
/// the program can name, in table text, ordered by name.
@intrinsic
pub fn definitions :: -> String
/// The program's schema hash: the fixed hash of its definitions table, as
/// 16 lowercase hex digits.
@intrinsic
pub fn schema_hash :: -> String
/// A `@remote` function of the program: its key, its qualified name, and
/// its argument's descriptor and type hash (16 lowercase hex digits).
pub type Entry
{ Entry(key: Int, name: String, descriptor: String, hash: String) }
/// The program's `@remote` functions, ordered by key. A node registers
/// their descriptors when it starts, to decode the arguments of the
/// spawns it accepts.
@intrinsic
pub fn entries :: -> List
/// A value with its type's descriptor: what `dispatch` takes. Only
/// `std/wire` makes one, with `arg` from a value whose `Wire` dictionary
/// the type checker gives, and the runtime, from a value it decoded with
/// that descriptor: the descriptor is always the value's.
pub opaque type Arg { Arg(descriptor: String, value: Dynamic) }
/// `x`, with its type's descriptor.
pub fn arg :: x: a -> Arg where Wire
{
Arg(describe x, to_dynamic x)
}
/// Why `dispatch` refused.
pub type Refused
{ /// No `@remote` function of the program has this key.
UnknownEntry
/// The argument's type is not the entry's.
, WrongType
}
/// The actor body that runs the `@remote` function `key` on `arg`, if
/// `arg`'s descriptor is its argument's. The caller spawns it, plainly,
/// linked or monitored. Code runs only through the program's own table of
/// keys: no code address ever comes off the wire.
pub fn dispatch ::
key: Int
arg: Arg
-> Result () / , Refused>
{
match arg { Arg(descriptor, value) -> entry_body key descriptor value }
}
/// `dispatch`'s body: a switch over the keys, then a comparison with the
/// entry's descriptor. Sound only because `value` is of the type
/// `descriptor` describes, which `Arg` guarantees.
@intrinsic
fn entry_body ::
key: Int
descriptor: String
value: Dynamic
-> Result () / , Refused>
@extern c "tl_coerce"
fn to_dynamic :: x: a -> Dynamic
/// The `[cluster]` section of the root package's `louise.toml` (ADR
/// 0072), checked by the compiler, its defaults filled in: the policy
/// every node of the program shares (`discovery` is `"seeds"` or
/// `"mesh"`). A node's own name, certificates and seeds come from its
/// environment, not from here.
@derive Eq Debug
pub type Cluster
{ Cluster(
name: String
, listen: Option
, heartbeat_ms: Int
, node_down_after_ms: Int
, max_message_bytes: Int
, outbound_queue_bytes: Int
, max_peers: Int
, discovery: String
)
}
/// The program's `[cluster]` policy, or `None` without the section.
pub fn cluster :: -> Option
{
match cluster_policy ()
{ "" -> None, text -> Some(read_policy (string.split text "\n")) }
}
/// `[cluster]` as the compiler writes it: a `key=value` line, ended by
/// `\n`, for each of `Cluster`'s fields in order, with `listen=` empty
/// when the section has none; the empty string without the section.
@intrinsic
pub fn cluster_policy :: -> String
fn read_policy :: lines: List -> Cluster
{
let listen = policy_value lines "listen"
Cluster( name: policy_value lines "name"
, listen: if listen == "" { None } else { Some(listen) }
, heartbeat_ms: policy_int lines "heartbeat_ms"
, node_down_after_ms: policy_int lines "node_down_after_ms"
, max_message_bytes: policy_int lines "max_message_bytes"
, outbound_queue_bytes: policy_int lines "outbound_queue_bytes"
, max_peers: policy_int lines "max_peers"
, discovery: policy_value lines "discovery"
)
}
/// The value of `key`'s line. The text is the compiler's: a missing key
/// is a bug.
fn policy_value :: lines: List key: String -> String
{
match lines
{ [] -> panic ("wire.cluster: no `" <> key <> "` in the policy")
, [line, ..rest] -> if string.starts_with line (key <> "=")
{
string.slice line (string.length key + 1) (string.length line)
}
else
{
policy_value rest key
}
}
}
fn policy_int :: lines: List key: String -> Int
{
match string.to_int (policy_value lines key)
{ Ok(n) -> n, Error(e) -> panic ("wire.cluster: " <> key <> ": " <> e) }
}