Documentation
¶
Overview ¶
Package wsmux adapts a gorilla/websocket connection into a net.Conn so that yamux can multiplex independent logical streams on top of it, and gRPC can run on top of yamux. This is the foundation of the Hub ↔ Worker sync protocol: a single long-lived WebSocket carries many independent gRPC streams (unary RPCs + server-stream Watch subscriptions) without head-of-line blocking between them.
Read MUST use websocket.NextReader (not ReadMessage) so that yamux can pull bytes incrementally; if we buffered each WebSocket message whole, yamux's chunk-level fair-scheduling between streams would degenerate to "one message blocks the next".
Index ¶
Constants ¶
const MaxMsgSize = 64 * 1024 * 1024
MaxMsgSize bounds gRPC unary / streaming payload size. Set generously so a SandboxTemplate snapshot (which can contain hundreds of templates with embedded PodTemplateSpec) cannot trip the default 4 MiB cap.
Variables ¶
This section is empty.
Functions ¶
func DialGRPC ¶
DialGRPC wraps wsConn into a yamux client session and returns a *grpc.ClientConn that opens fresh yamux streams on demand. The Worker side (which accepted the WebSocket Upgrade) is the yamux client.
The returned ClientConn is lazily connecting (grpc.NewClient semantics): no stream is opened until the first RPC. On any RPC, gRPC calls our ContextDialer which Opens one yamux stream; gRPC then runs HTTP/2 on that stream and multiplexes all subsequent RPCs as HTTP/2 streams within it. The outer yamux session is reserved for future non-gRPC streams (e.g. terminal byte forwarding) without needing protocol surgery.
func ServeGRPC ¶
func ServeGRPC(wsConn *websocket.Conn, register func(*grpc.Server)) (*grpc.Server, *yamux.Session, error)
ServeGRPC wraps wsConn into a yamux server session and starts a gRPC server that runs on top of it. The supplied register callback registers all service implementations onto the server before Serve() is called.
The Hub side (which dialled the WebSocket) is the yamux server: it Accepts streams initiated by the Worker (gRPC client) and feeds them to its grpc.Server. ServeGRPC returns once the grpc.Server has begun serving; caller blocks on <-session.CloseChan() to detect WebSocket disconnect.
On return, calling grpcSrv.GracefulStop() or session.Close() tears the whole stack down; closing wsConn directly is also fine and propagates up.
func WrapConn ¶
WrapConn adapts a *websocket.Conn into a net.Conn.
Caller transfers ownership of conn to the adapter: closing the returned net.Conn closes the underlying WebSocket. Both endpoints (Hub and Worker) must use this adapter so the WebSocket data-message convention (binary, streamed via NextReader) is symmetric.
Types ¶
This section is empty.