Sign in

    moonbitstack/moonzero/zrpc does not have a README file

    BidiStreamFactory

    type BidiStreamFactory = () -> BidiStreamHandler

    A factory that mints one BidiStreamHandler per call, so each stream gets its own handler state (← the fresh ServerStream gRPC hands every bidi invocation).

    ClientStreamHandler

    type ClientStreamHandler = (Array[Bytes]) -> Bytes

    A client-streaming handler: every request message the client sends is collected, and after the client half-closes the handler returns one reply.

    RpcHandler

    type RpcHandler = (Bytes) -> Bytes

    A unary RPC handler: it maps a request message's wire bytes to a response message's wire bytes (the application/grpc+proto payload, sans the length-prefix framing that @moonrpc.encode_message adds). Streaming handlers arrive with the h2 transport; this is the unary shape zRPC registers today.

    ServerStreamHandler

    type ServerStreamHandler = (Bytes) -> Array[Bytes]

    A server-streaming handler: one request message in, an ordered sequence of response messages out (each framed as its own length-prefixed gRPC message). Mirrors go-zero's pb.XxxServer server-streaming method, which writes to a grpc.ServerStream instead of returning one reply.

    BidiCall

    pub struct BidiCall {
    channel : RpcChannel
    sid : Int
    pending : Bytes
    status_code : Int
    ended : Bool
    }

    A live client-side bidirectional call over the h2c channel (← gRPC's ClientStream): the request stream stays open while messages flow both ways. send writes one request message and returns whatever replies the server produced right then (bidi interleaving — an echo handler answers each message as it arrives); close_send half-closes the request stream, runs the server's on_end, and reports the final grpc-status. The channel's HPACK decoder is advanced across every reply block, so its dynamic table stays in lockstep with the engine's encoder for the life of the call. pending holds DATA octets not yet split into a whole length-prefixed message (a message may straddle two DATA frames under flow control).

    BidiCall::close_send

    fn BidiCall::close_send(self : BidiCall) -> Result[Array[Bytes],
    Status
    ] raise

    Half-close the request stream: run the server's on_end, return its final reply messages, and map the grpc-status trailer to Ok/Err. Calling it a second time is an error (Cancelled).

    BidiCall::send

    fn BidiCall::send(self : BidiCall, msg : Bytes) -> Array[Bytes] raise

    Send one request message on the open stream and return the replies the server emitted in response to it (possibly empty). A no-op once the stream is half-closed.

    BidiStreamHandler

    pub(all) struct BidiStreamHandler {
    on_message : (Bytes) -> Array[Bytes]
    on_end : () -> Array[Bytes]
    }

    A live bidirectional call (← go-zero's pb.XxxServer bidi method, which reads from and writes to the same grpc.ServerStream): on_message fires once per request message and returns the replies to send right then, so responses interleave with requests; on_end runs after the client half-closes and returns the final replies before the grpc-status trailer. The moonzero-local mirror of @moonrpc.BidiHandler, so callers register bidi methods without naming the transport package.

    RpcChannel

    pub struct RpcChannel {
    engine :
    H2Server

    encoder :
    Encoder

    decoder :
    Decoder

    authority : String
    next_stream_id : Int
    }

    An in-process gRPC channel bound to a server engine — the client half of the h2c transport. A call is carried as the real HTTP/2 frames a socket-backed client would send: an HPACK-coded HEADERS block with the gRPC pseudo-headers, a length-prefixed DATA frame closing the stream, and the grpc-status trailer read back off the engine's reply. The channel's HPACK encoder pairs with the engine's decoder and vice versa, so the dynamic-table state stays in lockstep across every call on the channel.

    RpcChannel::call

    fn RpcChannel::call(self : RpcChannel, path : String, request : Bytes) -> Result[Bytes,
    Status
    ] raise

    Invoke a unary method at path with request as its message payload, driving the call through the h2c engine and returning the reply payload on grpc-status: 0, or the mapped @moonrpc.Status otherwise. request and the returned reply are bare message bytes; the length-prefix framing is applied and stripped by the transport.

    RpcChannel::call_bidi_streaming

    fn RpcChannel::call_bidi_streaming(self : RpcChannel, path : String, requests : Array[Bytes]) -> Result[Array[Bytes],
    Status
    ] raise

    Drive a whole bidirectional call at path in one shot: send every message in requests (collecting the interleaved replies in order), then half-close and append the on_end replies. The result is every reply message the server produced, in emission order, or the non-zero grpc-status the stream closed with.

    RpcChannel::call_client_streaming

    fn RpcChannel::call_client_streaming(self : RpcChannel, path : String, requests : Array[Bytes]) -> Result[Bytes,
    Status
    ] raise

    Invoke a client-streaming method at path: send every message in requests as its own DATA frame, half-close the stream, and read back the single reply. An empty requests still opens and half-closes the stream, so the handler runs with no messages.

    RpcChannel::call_server_streaming

    fn RpcChannel::call_server_streaming(self : RpcChannel, path : String, request : Bytes) -> Result[Array[Bytes],
    Status
    ] raise

    Invoke a server-streaming method at path: send the single request message and read back the ordered sequence of reply messages the server produced, or the mapped error @moonrpc.Status if the stream closed with a non-zero grpc-status. On Ok the array holds every message in emission order (possibly empty).

    RpcChannel::connect

    fn RpcChannel::connect(server : RpcServer, authority? : String) -> RpcChannel

    Open a channel to server over an in-process h2c transport, exchanging the opening SETTINGS the way a real connection does. Client-initiated streams use odd identifiers (RFC 7540 §5.1.1), starting at 1.

    RpcChannel::open_bidi

    fn RpcChannel::open_bidi(self : RpcChannel, path : String) -> BidiCall raise

    Open a bidirectional stream to path, sending the request HEADERS without half-closing so the stream stays open for interleaved sends. An unregistered path answers trailers-only UNIMPLEMENTED during this HEADERS feed, which the returned call captures as its status.

    RpcGroup

    pub struct RpcGroup {
    server : RpcServer
    service : String
    }

    A per-service registration handle (← go-zero's service registrar closure): binds a set of methods to one package.Service on a shared RpcServer.

    RpcGroup::register

    fn RpcGroup::register(self : RpcGroup, name : String, handler : (Bytes) -> Bytes) -> Unit

    Register a method name on this group's service, building the @moonrpc.Method descriptor and installing handler under its gRPC path. (register, not method — the latter is a reserved word.)

    RpcGroup::register_bidi_streaming

    fn RpcGroup::register_bidi_streaming(self : RpcGroup, name : String, factory : () -> BidiStreamHandler) -> Unit

    Register a bidirectional-streaming method name on this group's service.

    RpcGroup::register_client_streaming

    fn RpcGroup::register_client_streaming(self : RpcGroup, name : String, handler : (Array[Bytes]) -> Bytes) -> Unit

    Register a client-streaming method name on this group's service.

    RpcGroup::register_server_streaming

    fn RpcGroup::register_server_streaming(self : RpcGroup, name : String, handler : (Bytes) -> Array[Bytes]) -> Unit

    Register a server-streaming method name on this group's service.

    RpcGroup::service

    fn RpcGroup::service(self : RpcGroup) -> String

    The fully-qualified package.Service this group registers under.

    RpcServer

    pub struct RpcServer {
    conf :
    RpcServerConf

    handlers : Map[String, (Bytes) -> Bytes]
    server_streaming : Map[String, (Bytes) -> Array[Bytes]]
    client_streaming : Map[String, (Array[Bytes]) -> Bytes]
    bidi_streaming : Map[String, () -> BidiStreamHandler]
    }

    A zRPC server (← go-zero's zrpc.Server): config plus a registry mapping each method's gRPC :path (/package.Service/Method) to its handler. Handlers are registered via @moonrpc.Method descriptors — directly or through a RpcGroup — and dispatched by path, mirroring how go-zero registers service implementations on the underlying gRPC server. Unary, server-streaming, and client-streaming methods live in separate registries so one path resolves to exactly one cardinality.

    RpcServer::conf

    The server's configuration.

    RpcServer::dispatch

    fn RpcServer::dispatch(self : RpcServer, path : String, request : Bytes) -> Result[Bytes,
    Status
    ]

    Dispatch a unary call to the handler registered for path, returning the response bytes. An unregistered path yields Err(Unimplemented) — exactly the grpc-status a real gRPC server returns for an unknown method — so a transport can translate the result straight onto the wire.

    RpcServer::dispatch_graceful

    fn RpcServer::dispatch_graceful(self : RpcServer, coord :
    ShutdownCoordinator
    , path : String, request : Bytes) -> Result[Bytes,
    Status
    ]

    Dispatch a unary call through the shutdown gate: refuse with Unavailable when the server is shutting down, otherwise run the handler and count it as in-flight for the duration so a concurrent shutdown drains behind it. The gate wraps RpcServer::dispatch, so an unregistered path still yields Unimplemented.

    RpcServer::group

    fn RpcServer::group(self : RpcServer, service : String) -> RpcGroup

    Open a RpcGroup that registers methods under the fully-qualified package.Service name — go-zero's per-service registration, without repeating the service name on each method.

    RpcServer::has_method

    fn RpcServer::has_method(self : RpcServer, path : String) -> Bool

    Whether a handler is registered for path.

    RpcServer::lookup

    fn RpcServer::lookup(self : RpcServer, path : String) -> (Bytes) -> Bytes?

    Look up the handler registered for a gRPC :path, or None if unregistered.

    RpcServer::methods

    fn RpcServer::methods(self : RpcServer) -> Array[String]

    The gRPC paths of every registered method.

    RpcServer::new

    Build an empty RPC server from its config.

    RpcServer::register

    fn RpcServer::register(self : RpcServer, desc :
    Method
    , handler : (Bytes) -> Bytes) -> Unit

    Register handler for method, keyed by its gRPC path. A later registration for the same path replaces the earlier one.

    RpcServer::register_bidi_streaming

    fn RpcServer::register_bidi_streaming(self : RpcServer, desc :
    Method
    , factory : () -> BidiStreamHandler) -> Unit

    Register a bidirectional-streaming handler for method, keyed by its gRPC path. factory runs once per call so each stream gets fresh handler state.

    RpcServer::register_client_streaming

    fn RpcServer::register_client_streaming(self : RpcServer, desc :
    Method
    , handler : (Array[Bytes]) -> Bytes) -> Unit

    Register a client-streaming handler for method, keyed by its gRPC path.

    RpcServer::register_server_streaming

    fn RpcServer::register_server_streaming(self : RpcServer, desc :
    Method
    , handler : (Bytes) -> Array[Bytes]) -> Unit

    Register a server-streaming handler for method, keyed by its gRPC path.

    RpcServer::to_h2

    Build a @moonrpc.H2Server protocol engine from this zRPC server's registered handlers — the transport-facing view of the same registry dispatch reads. Each handler is bound to its gRPC path, so a request arriving over the h2c transport is dispatched to exactly the handler the group registered.

    status_of_code

    fn status_of_code(code : Int) ->
    Status

    Map a numeric grpc-status code back to a @moonrpc.Status. Anything outside the canonical 0–16 range is reported as Unknown, matching how a gRPC client treats an unrecognised code.