Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

This documentation is part of the "Projects with Books" initiative at zenOSmosis.

The source code for this project is available on GitHub.

muxio-rpc-service-caller: Client-Side Calling Interface

Loading…

muxio-rpc-service-caller: Client-Side Calling Interface

Relevant source files

The muxio-rpc-service-caller crate provides a high-level, runtime-agnostic interface for building Muxio RPC clients extensions/muxio-rpc-service-caller/Cargo.toml:2-3 It abstracts the complexities of stream lifecycle management, request correlation, and payload chunking into a simplified calling API.

RpcServiceCallerInterface

The core of the client-side logic is the RpcServiceCallerInterface trait. It defines the minimum set of capabilities a transport (like Tokio or WASM) must provide to support RPC calls extensions/muxio-rpc-service-caller/src/caller_interface.rs:25-31

Key Methods

MethodDescription
get_dispatcherReturns the Arc<TokioMutex<RpcDispatcher>> used for request correlation extensions/muxio-rpc-service-caller/src/caller_interface.rs28
get_emit_fnReturns a function used to send raw bytes to the underlying transport extensions/muxio-rpc-service-caller/src/caller_interface.rs29
is_connectedChecks the current connectivity status extensions/muxio-rpc-service-caller/src/caller_interface.rs30
set_state_change_handlerRegisters a callback for transport state transitions extensions/muxio-rpc-service-caller/src/caller_interface.rs:226-229
call_rpc_streamingInitiates an RPC call and returns an encoder and a response stream extensions/muxio-rpc-service-caller/src/caller_interface.rs:33-43
call_rpc_bufferedA convenience method that buffers the entire response before returning extensions/muxio-rpc-service-caller/src/caller_interface.rs:189-195

RPC Call Data Flow

The following diagram illustrates how a call moves from the high-level interface through the dispatcher to the transport.

Client-Side Call Flow

sequenceDiagram
    participant User as "User Code"
    participant Caller as "RpcServiceCallerInterface"
    participant Disp as "RpcDispatcher"
    participant Trans as "Transport (Emit)"

    User->>Caller: call_rpc_streaming(RpcRequest)
    Caller->>Disp: register_session()
    Disp-->>Caller: RpcSession (StreamID)
    Caller->>Trans: emit(HeaderFrame)
    Caller-->>User: (RpcStreamEncoder, DynamicReceiver)
    
    Note over User, Trans: Data Streaming Phase
    
    User->>Caller: encoder.write_payload(chunk)
    Caller->>Trans: emit(PayloadFrame)
    
    Trans-->>Caller: recv_fn(RpcStreamEvent)
    Caller->>User: DynamicReceiver.next()

Sources: extensions/muxio-rpc-service-caller/src/caller_interface.rs:33-187 extensions/muxio-rpc-service-caller/src/caller_interface.rs:189-224

Dynamic Channels

To handle asynchronous responses, the caller uses DynamicChannel, which provides a unified interface over both bounded and unbounded futures::channel::mpsc channels extensions/muxio-rpc-service-caller/src/dynamic_channel.rs:11-16

Sources: extensions/muxio-rpc-service-caller/src/dynamic_channel.rs:1-76

RpcCallPrebuffered Blanket Implementation

The RpcCallPrebuffered trait provides a “smart” transport strategy for RPC methods defined using the RpcMethodPrebuffered pattern extensions/muxio-rpc-service-caller/src/prebuffered/traits.rs:11-21

Smart Transport Strategy

Because a single Muxio frame header is limited (typically ~64KB), the implementation automatically decides how to send arguments:

  1. Small Arguments : If the encoded input is smaller than DEFAULT_SERVICE_MAX_CHUNK_SIZE, it is sent inside the rpc_param_bytes field of the initial header frame extensions/muxio-rpc-service-caller/src/prebuffered/traits.rs:63-65
  2. Large Arguments : If the input is large, it is placed in rpc_prebuffered_payload_bytes. The RpcDispatcher then automatically chunks and streams this data as subsequent payload frames extensions/muxio-rpc-service-caller/src/prebuffered/traits.rs:59-61

Entity Mapping: Prebuffered Call

Sources: extensions/muxio-rpc-service-caller/src/prebuffered/traits.rs:30-74 extensions/muxio-rpc-service-caller/src/caller_interface.rs:189-200

Write Loop and Backpressure

The write_channel module provides spawn_write_loop, which manages the outbound message queue for a connection extensions/muxio-rpc-service-caller/src/write_channel.rs:34-41

Sources: extensions/muxio-rpc-service-caller/src/write_channel.rs:1-54

Transport State and Error Handling

RpcTransportState

The client tracks the connection status using the RpcTransportState enum:

Error Handling

Errors are encapsulated in the RpcServiceError type.

Entity Mapping: Error Propagation

Sources: extensions/muxio-rpc-service-caller/src/transport_state.rs:1-7 extensions/muxio-rpc-service-caller/src/caller_interface.rs:44-53 extensions/muxio-rpc-service-caller/src/caller_interface.rs:118-170 extensions/muxio-rpc-service-caller/src/prebuffered/traits.rs90