json-rpc-rs
A thread pool-based Rust implementation of JSON-RPC 2.0. This library provides a simple, user-friendly API for creating local JSON-RPC servers with concurrent request handling, graceful shutdown, and support for multiple transports.
Features
- Builder Pattern: Configure servers with a fluent, intuitive API
- Type-Safe Methods: Register methods using closures with automatic parameter deserialization
- Thread Pool: Handle concurrent requests with a configurable worker pool
- Multiple Transports: Use Stdio or InMemory transports, or implement custom ones
- Graceful Shutdown: Clean server shutdown with signal support
- Request Cancellation: Cancel in-flight requests when needed
- Batch Requests: Full JSON-RPC 2.0 batch request support
- Error Handling: Simple error creation with JSON-RPC compliant error codes
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
Create a simple JSON-RPC server with one method and run it using stdin/stdout:
use Server;
Send a request:
|
Response:
Usage
Creating a Server
Create a server with default configuration:
use Server;
let mut server = new;
Configure thread pool size and shutdown signal:
use ;
let shutdown = new;
let mut server = new
.with_thread_pool_size
.with_shutdown_signal;
Registering Methods
Register methods with tuple parameters:
server.register?;
Register methods with a single parameter:
server.register?;
Register methods with struct parameters:
use Deserialize;
server.register?;
Running the Server
Run with the default Stdio transport:
server.run?;
Run with a custom transport:
use ;
let = unconnected;
let mut server = new.with_transport;
server.register?;
server.run?;
Error Handling
Return protocol errors:
use Error;
server.register?;
Return JSON-RPC standard errors:
server.register?;
Graceful Shutdown
Use a shutdown signal to stop the server:
use ;
use thread;
use Duration;
let shutdown = new;
let mut server = new.with_shutdown_signal;
// In a separate thread, signal shutdown after 5 seconds
spawn;
server.run?;
Batch Requests
The library handles batch requests automatically. Since the server uses NDJSON (newline-delimited JSON), batch request arrays must be on a single line:
|
Response:
Request Cancellation
Cancel in-flight requests using a cancellation token:
use ;
use Arc;
let token = new;
let mut server = new;
server.register?;
Transports
The library provides two built-in transports:
- Stdio: NDJSON (newline-delimited JSON) over stdin/stdout. This is the default and ideal for local servers.
- InMemory: In-memory transport for testing and in-process communication.
Implement custom transports by implementing the
Transport trait. See the
transport module for examples.
Thread Pool
The server uses a fixed-size thread pool for concurrent request handling:
- Default size: Number of CPU cores
- Configure with
.with_thread_pool_size(n) - Each request processes in a worker thread
- Responses return to the main thread for transmission
Limitations
- This library uses blocking I/O. It is not designed for async runtimes like tokio.
- The default Stdio transport works with NDJSON (newline-delimited JSON) only.
- This library is designed for local JSON-RPC servers, not distributed systems.
- Custom transports require implementing the
Transporttrait.
JSON-RPC 2.0 Compliance
This library implements the JSON-RPC 2.0 specification, including:
- Request objects with
jsonrpc,method,params, andidfields - Notification objects (requests without
id) - Response objects with
resultorerrorfields - Error objects with
code,message, and optionaldatafields - Batch requests (arrays of requests)
- Standard error codes (-32700 to -32099)
See JSON-RPC 2.0 Specification for details.
Examples
The examples directory contains working examples:
basic_server.rs: Demonstrates error handlingecho_server.rs: Simple echo server
Run an example:
Documentation
Generate and view API documentation:
License
MIT