Rust JSON-RPC library on top of async/await
Yet another rather small release that fixes a cancel-safety issue that could cause an unexpected panic when reading disconnect reason from the background task.
Also this makes the API Client::disconnect_reason
cancel-safe.
Another small release that adds a new API for RpcModule if one already has the state in an Arc
and a couple of bug fixes.
RpcModule::from_arc
(#1324)AlreadyStoppedError
(#1325)Thanks to the external contributors @mattsse and @aatifsyed who contributed to this release.
This is a small patch release that exposes the connection details to server methods without breaking changes. Currently, users can only retrieve the connection ID from these details. We plan to extend this functionality in jsonrpsee v1.0, although this will necessitate a breaking change.
Another breaking release where a new ResponsePayload
type is introduced in order
to make it possible to determine whether a response has been processed.
Unfortunately, the IntoResponse trait
was modified to enable that
and some minor changes were made to make more fields private to avoid further
breakage.
ResponsePayload API
#[rpc(server)]
pub trait Api {
#[method(name = "x")]
fn x(&self) -> ResponsePayload<'static, String>;
}
impl RpcServer for () {
fn x(&self) -> ResponsePayload<'static, String> {
let (rp, rp_done) = ResponsePayload::success("ehheeheh".to_string()).notify_on_completion();
tokio::spawn(async move {
if rp_done.await.is_ok() {
do_task_that_depend_x();
}
});
rp
}
}
We are getting closer to releasing jsonrpsee v1.0 and the following work is planned:
Thanks to the external contributor @dan-starkware who contributed to this release.
TowerService::on_session_close
(#1284)Response
has been processed. (#1281)This release contains big changes and let's go over the main ones:
After getting plenty of feedback regarding a JSON-RPC specific middleware,
this release introduces a composable "tower-like" middleware that applies per JSON-RPC method call.
The new middleware also replaces the old RpcLogger
which may break some use-cases, such as if
JSON-RPC was made on a WebSocket or HTTP transport, but it's possible to implement that by
using jsonrpsee as a tower service
or the low-level server API
.
An example how write such middleware:
#[derive(Clone)]
pub struct ModifyRequestIf<S>(S);
impl<'a, S> RpcServiceT<'a> for ModifyRequestIf<S>
where
S: Send + Sync + RpcServiceT<'a>,
{
type Future = S::Future;
fn call(&self, mut req: Request<'a>) -> Self::Future {
// Example how to modify the params in the call.
if req.method == "say_hello" {
// It's a bit awkward to create new params in the request
// but this shows how to do it.
let raw_value = serde_json::value::to_raw_value("myparams").unwrap();
req.params = Some(StdCow::Owned(raw_value));
}
// Re-direct all calls that isn't `say_hello` to `say_goodbye`
else if req.method != "say_hello" {
req.method = "say_goodbye".into();
}
self.0.call(req)
}
}
async fn run_server() {
// Construct our middleware and build the server.
let rpc_middleware = RpcServiceBuilder::new().layer_fn(|service| ModifyRequestIf(service));
let server = Server::builder().set_rpc_middleware(rpc_middleware).build("127.0.0.1:0").await.unwrap();
// Start the server.
let mut module = RpcModule::new(());
module.register_method("say_hello", |_, _| "lo").unwrap();
module.register_method("say_goodbye", |_, _| "goodbye").unwrap();
let handle = server.start(module);
handle.stopped().await;
}
For users who want to get full control of the HTTP request, it's now possible to utilize jsonrpsee as a tower service example here
For users who want to get low-level access and for example to disconnect misbehaving peers that is now possible as well example here
Logging of RPC calls has been disabled by default, but it's possible to enable that with the RPC logger middleware or provide your own middleware for that.
let rpc_middleware = RpcServiceBuilder::new().rpc_logger(1024);
let server = Server::builder().set_rpc_middleware(rpc_middleware).build("127.0.0.1:0").await?;
The WebSocket ping/pong APIs have been refactored to be able to disconnect inactive connections both by from the server and client-side.
Thanks to the external contributors @oleonardolima and @venugopv who contributed to this release.
tcp_nodelay == true
by default ([#1263])(https://github.com/paritytech/jsonrpsee/pull/1263)disconnect_reason
API (#1246)service
and low-level API for more fine-grained API to disconnect peers etc
(#1224)HostFilterLayer::disable
(#1213)This release removes the bounded buffer check which was intended to provide backpressure all the way down to the TCP layer but it didn't work well.
For subscriptions the backpressure will be handled by implementation itself and just rely on that.
This release adds support for synchronous subscriptions
and fixes a leak in WebSocket server
where FuturesUnordered was not getting polled until shutdown, so it was accumulating tasks forever.
register_subscription_raw
(#1182)