src/sys.bend source
src/sys.bend on the hub · documented module
import Base# Sys# ===## Effects Base lacks (effs/sys.c): the wall clock, termination signals, a# listener on IPv6 as well as IPv4, Unix sockets, and a small table of# expiring strings.# The wall clock: (days since 1970-01-01, (second of the day,# microseconds)). time.bend's Wall.of makes it a Wall.def Sys.wall() -> IO(U32 & (U32 & U32)): import "./effs/sys.c" import "./effs/sys.js"# Waits for SIGTERM or SIGINT; answers the signal's number.def Sys.signal() -> IO(U32): import "./effs/sys.c" import "./effs/sys.js"def Sys.listen.dual(port: U32) -> IO(Result<&1, &1, U32 & String, Listener>): import "./effs/sys.c" import "./effs/sys.js"# What the JS lane's Sys.listen.dual answers (effs/sys.js).def Sys.listen.js() -> String: "dual-stack listen needs the native lane"def Sys.listen.v4(+port: U32, c: U32, +m: String, lane: Bool) -> IO(Result<&1, &1, U32 & String, Listener>): match lane: case True{}: TCP.listen("0.0.0.0", port) case False{}: IO.pure(Result<&1, &1, U32 & String, Listener>, Fail{(c, m)})def Sys.listen.got(r: Result<&1, &1, U32 & String, Listener>, +port: U32) -> IO(Result<&1, &1, U32 & String, Listener>): match r: case Fail{(c, +m)}: # the JS lane's answer: listen through Base, on IPv4 Sys.listen.v4(port, c, m, String.starts_with(m, Sys.listen.js())) case Done{l}: IO.pure(Result<&1, &1, U32 & String, Listener>, Done{l})# A listener on every address of port: IPv6 and IPv4 (dual-stack), so# probes and webhooks answer on IPv6-only and dual-stack clusters; IPv4# only on a host without IPv6, and on the JS lane.def Sys.listen(+port: U32) -> IO(Result<&1, &1, U32 & String, Listener>): IO.bind(Result<&1, &1, U32 & String, Listener>, Result<&1, &1, U32 & String, Listener>, Sys.listen.dual(port), r => Sys.listen.got(r, port))# A process-wide table of short strings that expire (wall-clock seconds,# as Wall.secs counts them): what must outlive one request, such as an# exec credential plugin's token. A missing or expired key answers "".def Sys.memo_get(key: String, now: U32) -> IO(String): import "./effs/sys.c" import "./effs/sys.js"def Sys.memo_put(key: String, value: String, until: U32) -> IO(Unit): import "./effs/sys.c" import "./effs/sys.js"# A listener on the Unix socket at path (at most 103 bytes on macOS, 107# on Linux), which TCP.accept serves. A socket file left by a server that# is gone is replaced; a live server's fails with EADDRINUSE (48 on macOS,# 98 on Linux), and any other file there with EEXIST, never removed. mode,# unless 0, is the socket file's permission bits (0o600 = 384: the owner# only). The native lane only: the JS lane answers Fail.def Sys.unix_listen(path: String, mode: U32) -> IO(Result<&1, &1, U32 & String, Listener>): import "./effs/sys.c" import "./effs/sys.js"# A socket connected to the Unix socket at path, for TCP.send and# TCP.recv. The native lane only.def Sys.unix_connect(path: String) -> IO(Result<&1, &1, U32 & String, Socket>): import "./effs/sys.c" import "./effs/sys.js"# A second handle on sock's connection (dup(2)): one computation reads# while another writes, each owning a handle. The connection lasts until# both are closed; Sys.shutdown ends it for both.def Sys.dup(sock: Socket) -> IO(Socket & Result<&1, &1, U32 & String, Socket>): import "./effs/sys.c" import "./effs/sys.js"# shutdown(2) both ways: a recv on any handle of the connection ends. The# handle stays open until closed.def Sys.shutdown(sock: Socket) -> IO(Socket): import "./effs/sys.c" import "./effs/sys.js"