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: Shared Service Definitions

Loading…

muxio-rpc-service: Shared Service Definitions

Relevant source files

The muxio-rpc-service crate provides the foundational traits, constants, and macros required to define RPC service contracts within the Muxio framework extensions/muxio-rpc-service/Cargo.toml:2-3 It serves as the shared dependency between service providers (endpoints) and service consumers (callers), ensuring that both sides of a connection agree on method identifiers and serialization formats.

Core Service Abstractions

The crate centers around the “prebuffered” RPC model, where requests and responses are fully realized in memory before being processed or transmitted. This model simplifies service implementation by abstracting away the underlying stream-based transport.

RpcMethodPrebuffered Trait

The RpcMethodPrebuffered trait is the primary mechanism for defining a typed RPC method. It associates a unique u64 identifier with specific input and output types, along with their respective codec logic extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs:3-35

PropertyDescription
METHOD_IDA unique u64 used by the RpcDispatcher to route requests to the correct handler extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs10
InputThe high-level Rust type for request data (e.g., a struct or Vec<f64>) extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs13
OutputThe high-level Rust type for response data extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs16
encode_requestSerializes the Input type into a byte array for transport extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs19
decode_requestDeserializes raw bytes into the Input type on the server side extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs25
encode_responseSerializes the Output type into bytes after execution extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs28
decode_responseDeserializes raw bytes back into the Output type on the client side extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs34

Sources: extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs:3-35

Compile-Time Method Identification

To avoid manual management of magic numbers for method IDs, muxio-rpc-service provides a deterministic hashing mechanism.

rpc_method_id! Macro

The rpc_method_id! macro uses the xxh3 (64-bit) algorithm to generate method identifiers from string literals at compile time extensions/muxio-rpc-service/src/macros.rs:35-40

Sources: extensions/muxio-rpc-service/src/macros.rs:1-40

Response Status Mapping

The crate defines the status of an RPC execution through the RpcResultStatus enum, which is used to communicate the outcome of a request across the wire.

RpcResultStatus Enum

This enum is represented as a u8 for efficient transport and categorizes the result of an RPC call extensions/muxio-rpc-service/src/result_status.rs:3-10:

Sources: extensions/muxio-rpc-service/src/result_status.rs:1-11

Constants and Configuration

Default values for transport and buffering are defined here to ensure consistency across implementations.

Sources: extensions/muxio-rpc-service/src/constants.rs:1-33

Architecture & Data Flow Diagrams

RPC Service Definition Logic

This diagram illustrates how the RpcMethodPrebuffered trait bridges high-level types to the binary transport using the rpc_method_id! macro.

Sources: extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs:3-35 extensions/muxio-rpc-service/src/macros.rs:35-40

graph TD
    subgraph "Service Definition Space"
        A["RpcMethodPrebuffered Trait"]
B["METHOD_ID"]
M["rpc_method_id! Macro"]
end

    subgraph "Data Entities"
        I["Input Type"]
O["Output Type"]
end

    subgraph "Codec Logic"
        E1["encode_request(Input) -> Vec<u8>"]
D1["decode_request(&[u8]) -> Input"]
E2["encode_response(Output) -> Vec<u8>"]
D2["decode_response(&[u8]) -> Output"]
end

    M -- "Compile-time Hash" --> B
 
   A --> B
 
   A --> I
 
   A --> O
 
   I --> E1
 
   E1 -.-> D1
 
   O --> E2
 
   E2 -.-> D2
graph LR
    subgraph "Endpoint Execution"
        EXEC["Handler Execution"]
SUCCESS["Success (0)"]
APP_ERR["Fail (1)"]
SYS_ERR["SystemError (2)"]
NOT_FOUND["MethodNotFound (3)"]
end

    subgraph "Wire Representation"
        STATUS["RpcResultStatus (u8)"]
end

    subgraph "Caller Resolution"
        RES["Result<Output, Error>"]
end

 
   EXEC --> SUCCESS
 
   EXEC --> APP_ERR
 
   EXEC --> SYS_ERR
 
   EXEC --> NOT_FOUND

 
   SUCCESS --> STATUS
 
   APP_ERR --> STATUS
 
   SYS_ERR --> STATUS
 
   NOT_FOUND --> STATUS

 
   STATUS --> RES

Result Status Propagation

This diagram maps how RpcResultStatus categorizes the outcome of a service execution for the caller.

Sources: extensions/muxio-rpc-service/src/result_status.rs:3-10

Module Structure

The crate is organized into several modules to separate concerns:

Sources: extensions/muxio-rpc-service/src/constants.rs:1-33 extensions/muxio-rpc-service/src/macros.rs:1-40 extensions/muxio-rpc-service/src/result_status.rs:1-11 extensions/muxio-rpc-service/src/prebuffered/prebuffered_traits.rs:3-35