tur/session
stdlib/session.tur
standard session-type protocol patterns for typed channels.
Since: Phase B1
defn
linear
echo-server-loop
(echo-server-loop [^linear ch :(Session (Rec self (Branch (Recv int (Send int self)) Close)))] :nil)
serve the echo protocol until the client closes.
Parameters
| ch | server-side channel with protocol | |
| Session[Rec self (Branch (Recv int (Send int self)) Close)] |
Returns
nil -- the function returns after the client sends Close
Example
(spawn (fn [] (echo-server-loop server-end)))
Since: SS4
defn
linear
echo-client-call
(echo-client-call [^linear ch :(Session (Rec self (Choose (Send int (Recv int self)) Close))) val :int] :[(int)
send one integer and receive the echo.
Parameters
| ch | client-side channel with protocol | |
| Session[Rec self (Choose (Send int (Recv int self)) Close)] | ||
| val | integer to send and have echoed back |
Returns
A pair [echoed-value, updated-channel] where updated-channel still has the recursive EchoClientProto so more calls can be made.
Example
(let [[r ch] (echo-client-call ch 42)]
(println r)) ; => 42
Since: SS4
defn
linear
rpc-call
(rpc-call [^linear ch :(Session (Send int (Recv int Close))) req :int] :int)
perform a single RPC: send request, receive response, close.
Parameters
| ch | client channel with protocol Session[Send int (Recv int Close)] | |
| req | integer request value to send to the server |
Returns
The integer response received from the server. The channel is fully consumed (closed) by this call.
Example
(let [resp (rpc-call ch 21)]
(println resp)) ; => 42 (if server doubles the value)
Since: SS4
defn
linear
pubsub-recv-loop
(pubsub-recv-loop [^linear ch :(Session (Recv int (Rec self (Branch (Recv int self) Close)))) handler :(-> int nil)] :nil)
receive messages from a topic until it closes.
Parameters
| ch | subscriber channel with protocol | |
| Session[Recv int (Rec self (Branch (Recv int self) Close))] | ||
| handler | function called with each received integer message |
Returns
nil -- returns after the topic sends the Close branch
Example
(pubsub-recv-loop sub-ch (fn [msg] (println msg)))
Since: SS4
defn
linear
pubsub-recv-loop-inner
(pubsub-recv-loop-inner [^linear ch :(Session (Rec self (Branch (Recv int self) Close))) handler :(-> int nil)] :nil)
inner recursive loop after initial recv.