Rust JSON-RPC library on top of async/await
Another breaking release where the major changes are:
host filtering
has been moved to tower middleware instead of the server API.wss://my.server.com
Regarding host filtering prior to this release one had to do:
let acl = AllowHosts::Only(vec!["http://localhost:*".into(), "http://127.0.0.1:*".into()]);
let server = ServerBuilder::default().set_host_filtering(acl).build("127.0.0.1:0").await.unwrap();
After this release then one have to do:
let middleware = tower::ServiceBuilder::new().layer(HostFilterLayer::new(["example.com"]).unwrap());
let server = Server::builder().set_middleware(middleware).build("127.0.0.1:0".parse::<SocketAddr>()?).await?;
Thanks to the external contributors @polachok, @bobs4462 and @aj3n that contributed to this release.
SubscriptionMessage::new
(#1176)SubscriptionSink::connection_id
(#1175)Params::get
(#1173)PendingSubscriptionSink::connection_id
(#1163)&str
(#1160)RootCertStore::add_trust_anchors
(#1165)This release fixes a couple bugs and improves the ergonomics for the HTTP client when no tower middleware is enabled.
This is a breaking release that removes the CallError
which was used to represent a JSON-RPC error object that
could happen during JSON-RPC method call and one could assign application specific error code, message and data in a
specific implementation.
Previously jsonrpsee provided CallError
that could be converted to/from jsonrpsee::core::Error
and in some scenarios the error code was automatically assigned by jsonrpsee. After jsonrpsee
added support for custom error types the CallError
doesn't provide any benefit because one has to implement Into<ErrorObjectOwned>
on the error type anyway.
Thus, jsonrpsee::core::Error
can't be used in the proc macro API anymore and the type alias
RpcResult
has been modified to Result<(), ErrorObjectOwned>
instead.
Before it was possible to do:
#[derive(thiserror::Error)]
enum Error {
A,
B,
}
#[rpc(server, client)]
pub trait Rpc
{
#[method(name = "getKeys")]
async fn keys(&self) -> Result<String, jsonrpsee::core::Error> {
Err(jsonrpsee::core::Error::to_call_error(Error::A))
// or jsonrpsee::core::Error::Call(CallError::Custom(ErrorObject::owned(1, "a", None::<()>)))
}
}
After this change one has to do:
pub enum Error {
A,
B,
}
impl From<Error> for ErrorObjectOwned {
fn from(e: Error) -> Self {
match e {
Error::A => ErrorObject::owned(1, "a", None::<()>),
Error::B => ErrorObject::owned(2, "b", None::<()>),
}
}
}
#[rpc(server, client)]
pub trait Rpc {
// Use a custom error type that implements `Into<ErrorObject>`
#[method(name = "custom_err_ty")]
async fn custom_err_type(&self) -> Result<String, Error> {
Err(Error::A)
}
// Use `ErrorObject` as error type directly.
#[method(name = "err_obj")]
async fn error_obj(&self) -> RpcResult<String> {
Err(ErrorObjectOwned::owned(1, "c", None::<()>))
}
}
CallError
(#1087)This release fixes HTTP graceful shutdown for the server.
This is a significant release and the major breaking changes to be aware of are:
This release changes the server to be "backpressured" and it mostly concerns subscriptions.
New APIs has been introduced because of that and the API pipe_from_stream
has been removed.
Before it was possible to do:
module
.register_subscription("sub", "s", "unsub", |_, sink, _| async move {
let stream = stream_of_integers();
tokio::spawn(async move {
sink.pipe_from_stream(stream)
});
})
.unwrap();
After this release one must do something like:
// This is just a example helper.
//
// Other examples:
// - <https://github.com/paritytech/jsonrpsee/blob/master/examples/examples/ws_pubsub_broadcast.rs>
// - <https://github.com/paritytech/jsonrpsee/blob/master/examples/examples/ws_pubsub_with_params.rs>
async fn pipe_from_stream<T: Serialize>(
pending: PendingSubscriptionSink,
mut stream: impl Stream<Item = T> + Unpin,
) -> Result<(), anyhow::Error> {
let mut sink = pending.accept().await?;
loop {
tokio::select! {
_ = sink.closed() => break Ok(()),
maybe_item = stream.next() => {
let Some(item) = match maybe_item else {
break Ok(()),
};
let msg = SubscriptionMessage::from_json(&item)?;
if let Err(e) = sink.send_timeout(msg, Duration::from_secs(60)).await {
match e {
// The subscription or connection was closed.
SendTimeoutError::Closed(_) => break Ok(()),
/// The subscription send timeout expired
/// the message is returned and you could save that message
/// and retry again later.
SendTimeoutError::Timeout(_) => break Err(anyhow::anyhow!("Subscription timeout expired")),
}
}
}
}
}
}
module
.register_subscription("sub", "s", "unsub", |_, pending, _| async move {
let stream = stream();
pipe_from_stream(sink, stream).await
})
.unwrap();
This release also introduces a trait called IntoResponse
which is makes it possible to return custom types and/or error
types instead of enforcing everything to return Result<T, jsonrpsee::core::Error>
This affects the APIs RpcModule::register_method
, RpcModule::register_async_method
and RpcModule::register_blocking_method
and when these are used in the proc macro API are affected by this change.
Be aware that the client APIs don't support this yet
The IntoResponse
trait is already implemented for Result<T, jsonrpsee::core::Error>
and for the primitive types
Before it was possible to do:
// This would return Result<&str, jsonrpsee::core::Error>
module.register_method("say_hello", |_, _| Ok("lo"))?;
After this release it's possible to do:
// Note, this method call is infallible and you might not want to return Result.
module.register_method("say_hello", |_, _| "lo")?;
jsonrpsee now spawns the subscriptions via tokio::spawn
and it's sufficient to provide an async block in register_subscription
Further, the subscription API had an explicit close API for closing subscriptions which was hard to understand and to get right. This has been removed and everything is handled by the return value/type of the async block instead.
Example:
module
.register_subscription::<RpcResult<(), _, _>::("sub", "s", "unsub", |_, pending, _| async move {
// This just answers the RPC call and if this fails => no close notification is sent out.
pending.accept().await?;
// This is sent out as a `close notification/message`.
Err(anyhow::anyhow!("The subscription failed"))?;
})
.unwrap();
The return value in the example above needs to implement IntoSubscriptionCloseResponse
and
any value that is returned after that the subscription has been accepted will be treated as a IntoSubscriptionCloseResponse
.
Because Result<(), E>
is used here the close notification will be sent out as error notification but it's possible to
disable the subscription close response by using ()
instead of Result<(), E>
or implement IntoSubscriptionCloseResponse
for other behaviour.
Semaphore::(u32::MAX)
(#1051)max_log_length
APIs and use missing configs (#956)impl IntoSubscriptionResponse
(#1034)IntoResponse
trait for method calls (#1057)jsonrpc
protocol version field in Response
as Option
(#1046)SubscriptionAnswer
(#1025)max_notifs_per_subscription
to max_buffer_capacity_per_subscription
(#1012)FutureDriver
with tokio::spawn
(#1080)v0.16.1 is release that adds two new APIs to the server http_only
and ws_only
to make it possible to allow only HTTP respectively WebSocket.
Both HTTP and WebSocket are still enabled by default.
async-channel
(#940)