Network channels
Goal: see that communication over the network is the same
Channel[T]as local processes (the language’s native typed RPC), and understand theSerializableconstraint.
The same idea, crossing the network
Section titled “The same idea, crossing the network”Two processes in the same runtime are isolated worlds that talk over a channel. Two runtimes on
different machines are just two more isolated worlds, and the communication is the same concept.
There is no network channel type: it is the same old Channel[T], obtained by a net operation
that imposes what physics requires.
Physics requires two things, and both already exist in the language:
- The payload has to become bytes, so the operation asks for
T + Serializable(chapter 11). Sending something non-serializable over the network does not compile. - Establishing can fail, so the operation returns
Result, and the channel inherits the mandatorytimeoutof the sync.
use net
// server: listens, each connection becomes a Channel[T]listener := net.listen("0.0.0.0:8080") catch |e| { return e }loop { chan := net.accept[Message](listener) catch |e| { continue } // Message: Serializable spawn handle(chan) // each client = a process}
// client: connects, gets the Channel[T]chan := net.connect[Message]("api.example:8080") catch |e| { return e }chan <- request // SAME channel APIreply := -> chan timeout(5s) catch |e| { return e } // timeout already mandatoryWhat you get is an ordinary Channel[Message]: send, -> recv, <->, match/select,
catch, all identical to chapter 06. The network did not add a communication model; it reused the whole
channel and let physics (serialization, failure) express itself through the constraint and the Result.
This is native typed RPC
Section titled “This is native typed RPC”Since both sides share T at compile time, the network channel is a typed RPC, with no IDL and
no generated stub: you send a language value and the other side receives the same type. (To talk
to the world that is not yours, whose contract is the wire format and not a shared T, the path is
HTTP, through the official http lib, chapter 12.)
Raw sockets, when you need them
Section titled “Raw sockets, when you need them”For protocols that don’t speak your serialization, net also has layer 2, with TcpStream/
UdpSocket (bytes) that satisfy Readable/Writeable, and thin TLS on top. It is on that
layer that http is built.
stream := net.dial_tcp("example:80") catch |e| { return e }io.write_all(stream, pedido_http_cru)