This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
RPC Service Extension Layer
Loading…
RPC Service Extension Layer
Relevant source files
- extensions/muxio-rpc-service-caller/Cargo.toml
- extensions/muxio-rpc-service-endpoint/Cargo.toml
- extensions/muxio-rpc-service/Cargo.toml
The RPC Service Extension Layer provides high-level abstractions for defining, calling, and handling RPC services. While the core muxio crate handles the mechanics of binary framing and stream multiplexing, this layer introduces structured service definitions, client-side calling interfaces, and server-side handler registries.
These extensions are split into three primary crates to ensure a clear separation of concerns between shared definitions, client logic, and server logic.
Layered Architecture Overview
The following diagram illustrates how the RPC Service Extension crates sit between the low-level muxio core and the final transport implementations (like WebSocket or IPC).
Service Extension Topology
graph TD
subgraph "Application Space"
[ServiceDefinition] --> |implements| [RpcMethodPrebuffered]
end
subgraph "RPC Service Extension Layer"
[muxio-rpc-service-caller] -.-> [muxio-rpc-service]
[muxio-rpc-service-endpoint] -.-> [muxio-rpc-service]
end
subgraph "Muxio Core"
[muxio-rpc-service-caller] --> [RpcDispatcher]
[muxio-rpc-service-endpoint] --> [RpcRespondableSession]
end
[muxio-rpc-service-caller] --- [muxio-tokio-rpc-client]
[muxio-rpc-service-endpoint] --- [muxio-tokio-rpc-server]
Sources:
- extensions/muxio-rpc-service/Cargo.toml:1-3
- extensions/muxio-rpc-service-caller/Cargo.toml:1-3
- extensions/muxio-rpc-service-endpoint/Cargo.toml:1-3
muxio-rpc-service: Shared Service Definitions
The muxio-rpc-service crate defines the “language” that both clients and servers speak. It focuses on compile-time safety and efficient serialization using bitcode extensions/muxio-rpc-service/Cargo.toml12
- Trait-Based Definitions : Uses the
RpcMethodPrebufferedtrait to define RPC methods, associating them with specific request and response types. - Compile-Time Hashing : Provides the
rpc_method_id!macro, which usesxxhash-rustextensions/muxio-rpc-service/Cargo.toml14 at compile-time to generate uniqueu64identifiers for methods based on their names. - Standardized Results : Defines
RpcResultStatusandRpcServiceErrorto ensure consistent error propagation across the network. - Configuration : Sets the
DEFAULT_SERVICE_MAX_CHUNK_SIZEfor prebuffered message fragmentation.
For details, see muxio-rpc-service: Shared Service Definitions.
Sources:
muxio-rpc-service-caller: Client-Side Calling Interface
The muxio-rpc-service-caller crate provides the interface used by clients to initiate RPC requests. It abstracts the underlying RpcDispatcher into a more ergonomic API using async-trait extensions/muxio-rpc-service-caller/Cargo.toml12
- Caller Interface : The
RpcServiceCallerInterfacetrait defines how a client interacts with the transport, includingget_dispatcher,get_emit_fn, and checkingis_connectedstatus. - Typed Calling : Provides
call_rpc_bufferedandcall_rpc_streamingmethods that automatically handle serialization and deserialization based on theRpcMethodPrebuffereddefinition. - State Management : Tracks the
RpcTransportState(e.g., Connected, Disconnected) and allows applications to react to lifecycle events viaset_state_change_handler. - Dynamic Channels : Utilizes
DynamicChannel(Bounded or Unbounded) to manage asynchronous data flow for streaming calls, backed bytokio::syncextensions/muxio-rpc-service-caller/Cargo.toml16
For details, see muxio-rpc-service-caller: Client-Side Calling Interface.
Sources:
muxio-rpc-service-endpoint: Server-Side Handler Registry
The muxio-rpc-service-endpoint crate manages the server-side logic for receiving requests and routing them to the appropriate handlers.
- Handler Registration : The
RpcServiceEndpointstruct allows developers to register handlers for specific method IDs usingregister_prebuffered. - Execution Pipeline : Implements a three-stage
read_bytespipeline for incoming data:- Decode : Incoming bytes are decoded into the request type using
bitcodeextensions/muxio-rpc-service-endpoint/Cargo.toml17 - Execute : The registered
RpcPrebufferedHandleris executed (optionally concurrently). - Emit : The result is serialized and sent back to the client via the response emission logic.
- Decode : Incoming bytes are decoded into the request type using
- Concurrency : Includes support for
tokioto handle concurrent request processing via thetokio_supportfeature flag extensions/muxio-rpc-service-endpoint/Cargo.toml13 managing handlers through aHandlersLock.
For details, see muxio-rpc-service-endpoint: Server-Side Handler Registry.
Sources:
Entity Mapping: Extension to Core
The following table maps the high-level Extension Layer concepts to the low-level Core entities they wrap or utilize.
| Extension Entity | Core Entity | Role |
|---|---|---|
RpcServiceCallerInterface | RpcDispatcher | Wraps the dispatcher to provide type-safe call methods. |
RpcServiceEndpoint | RpcRespondableSession | Uses the session to read bytes and emit responses. |
RpcMethodPrebuffered | RpcHeader | Provides the u64 method ID stored in the header. |
DynamicChannel | Frame | Manages the flow of frames for streaming RPC calls. |
RpcResultStatus | RpcResponse | Encodes the success or failure status into the response header. |
Sources:
- extensions/muxio-rpc-service/Cargo.toml:12-14
- extensions/muxio-rpc-service-caller/Cargo.toml:14-16
- extensions/muxio-rpc-service-endpoint/Cargo.toml:20-23