Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Server::start infallible and add fn builder() #1137

Merged
merged 4 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benches/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub async fn http_server(handle: tokio::runtime::Handle) -> (String, jsonrpsee::
let module = gen_rpc_module();

let addr = server.local_addr().unwrap();
let handle = server.start(module).unwrap();
let handle = server.start(module);
(format!("http://{}", addr), handle)
}

Expand Down Expand Up @@ -161,7 +161,7 @@ pub async fn ws_server(handle: tokio::runtime::Handle) -> (String, jsonrpsee::se
.unwrap();

let addr = format!("ws://{}", server.local_addr().unwrap());
let handle = server.start(module).unwrap();
let handle = server.start(module);
(addr, handle)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/core_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
module.register_method("say_hello", |_, _| "lo")?;
let addr = server.local_addr()?;

let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/cors_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
})?;

let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
module.register_method("say_hello", |_, _| "lo")?;

let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/http_proxy_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
module.register_method("say_hello", |_, _| "lo").unwrap();
module.register_method("system_health", |_, _| serde_json::json!({ "health": true })).unwrap();

let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
module.register_method("say_hello", |_, _| "lo")?;
let addr = server.local_addr()?;

let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
let mut module = RpcModule::new(());
module.register_method("say_hello", |_, _| "lo").unwrap();

let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/multi_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
""
})?;
let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
let server = ServerBuilder::default().build("127.0.0.1:0").await?;

let addr = server.local_addr()?;
let handle = server.start(RpcServerImpl.into_rpc())?;
let handle = server.start(RpcServerImpl.into_rpc());

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/proc_macro_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
let server = ServerBuilder::default().build("127.0.0.1:0").await?;

let addr = server.local_addr()?;
let handle = server.start(RpcServerImpl.into_rpc())?;
let handle = server.start(RpcServerImpl.into_rpc());

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/tokio_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
})?;

let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing a stopping the server so let it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
let mut module = RpcModule::new(());
module.register_method("say_hello", |_, _| "lo")?;
let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/ws_pubsub_broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
})
.unwrap();
let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/ws_pubsub_with_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn run_server() -> anyhow::Result<SocketAddr> {
.unwrap();

let addr = server.local_addr()?;
let handle = server.start(module)?;
let handle = server.start(module);

// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
Expand Down
2 changes: 1 addition & 1 deletion proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub(crate) mod visitor;
/// pub async fn server() -> SocketAddr {
/// let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
/// let addr = server.local_addr().unwrap();
/// let server_handle = server.start(RpcServerImpl.into_rpc()).unwrap();
/// let server_handle = server.start(RpcServerImpl.into_rpc());
///
/// // `into_rpc()` method was generated inside of the `RpcServer` trait under the hood.
/// tokio::spawn(server_handle.stopped());
Expand Down
2 changes: 1 addition & 1 deletion proc-macros/tests/ui/correct/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl RpcServer for RpcServerImpl {
pub async fn server() -> SocketAddr {
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr = server.local_addr().unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc()).unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc());

tokio::spawn(server_handle.stopped());

Expand Down
2 changes: 1 addition & 1 deletion proc-macros/tests/ui/correct/custom_ret_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub trait RpcClient {
pub async fn server() -> SocketAddr {
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr = server.local_addr().unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc()).unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc());

tokio::spawn(server_handle.stopped());

Expand Down
2 changes: 1 addition & 1 deletion proc-macros/tests/ui/correct/only_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl RpcServer for RpcServerImpl {
pub async fn server() -> SocketAddr {
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr = server.local_addr().unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc()).unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc());

tokio::spawn(server_handle.stopped());
addr
Expand Down
2 changes: 1 addition & 1 deletion proc-macros/tests/ui/correct/param_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl RpcServer for RpcServerImpl {
pub async fn server() -> SocketAddr {
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr = server.local_addr().unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc()).unwrap();
let server_handle = server.start(RpcServerImpl.into_rpc());

tokio::spawn(server_handle.stopped());

Expand Down
4 changes: 2 additions & 2 deletions proc-macros/tests/ui/correct/rpc_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ pub async fn websocket_servers() -> (SocketAddr, SocketAddr) {
// Start server from `MyRpcS` trait.
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr_server_only = server.local_addr().unwrap();
let server_handle = server.start(ServerOnlyImpl.into_rpc()).unwrap();
let server_handle = server.start(ServerOnlyImpl.into_rpc());

tokio::spawn(server_handle.stopped());

// Start server from `MyRpcSC` trait.
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr_server_client = server.local_addr().unwrap();
let server_handle = server.start(ServerClientServerImpl.into_rpc()).unwrap();
let server_handle = server.start(ServerClientServerImpl.into_rpc());

tokio::spawn(server_handle.stopped());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub async fn server() -> SocketAddr {
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr = server.local_addr().unwrap();

server.start(DeprecatedServerImpl.into_rpc()).unwrap();
server.start(DeprecatedServerImpl.into_rpc());

addr
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
/// Start responding to connections requests.
///
/// This will run on the tokio runtime until the server is stopped or the `ServerHandle` is dropped.
pub fn start(mut self, methods: impl Into<Methods>) -> Result<ServerHandle, Error> {
pub fn start(mut self, methods: impl Into<Methods>) -> ServerHandle {
let methods = methods.into();
let (stop_tx, stop_rx) = watch::channel(());

Expand All @@ -113,7 +113,7 @@ where
None => tokio::spawn(self.start_inner(methods, stop_handle)),
};

Ok(ServerHandle::new(stop_tx))
ServerHandle::new(stop_tx)
}

async fn start_inner(self, methods: Methods, stop_handle: StopHandle) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub(crate) async fn server_with_handles() -> (SocketAddr, ServerHandle) {

let addr = server.local_addr().unwrap();

let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);
(addr, server_handle)
}

Expand Down Expand Up @@ -160,7 +160,7 @@ pub(crate) async fn server_with_context() -> SocketAddr {
.unwrap();

let addr = server.local_addr().unwrap();
let handle = server.start(rpc_module).unwrap();
let handle = server.start(rpc_module);

tokio::spawn(handle.stopped());
addr
Expand Down
12 changes: 6 additions & 6 deletions server/src/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async fn server() -> (SocketAddr, ServerHandle) {
})
.unwrap();

let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);
(addr, server_handle)
}

Expand Down Expand Up @@ -461,7 +461,7 @@ async fn can_set_the_max_request_body_size() {
module.register_method("anything", |_p, _cx| "a".repeat(100)).unwrap();
let addr = server.local_addr().unwrap();
let uri = to_http_uri(addr);
let handle = server.start(module).unwrap();
let handle = server.start(module);

// Invalid: too long
let req = format!(r#"{{"jsonrpc":"2.0", "method":{}, "id":1}}"#, "a".repeat(100));
Expand All @@ -486,7 +486,7 @@ async fn can_set_the_max_response_size() {
module.register_method("anything", |_p, _cx| "a".repeat(101)).unwrap();
let addr = server.local_addr().unwrap();
let uri = to_http_uri(addr);
let handle = server.start(module).unwrap();
let handle = server.start(module);

// Oversized response.
let req = r#"{"jsonrpc":"2.0", "method":"anything", "id":1}"#;
Expand All @@ -506,7 +506,7 @@ async fn can_set_the_max_response_size_to_batch() {
module.register_method("anything", |_p, _cx| "a".repeat(51)).unwrap();
let addr = server.local_addr().unwrap();
let uri = to_http_uri(addr);
let handle = server.start(module).unwrap();
let handle = server.start(module);

// Two response will end up in a response of 102 bytes which is too big.
let req = r#"[{"jsonrpc":"2.0", "method":"anything", "id":1},{"jsonrpc":"2.0", "method":"anything", "id":2}]"#;
Expand All @@ -527,7 +527,7 @@ async fn disabled_batches() {
module.register_method("should_ok", |_, _ctx| "ok").unwrap();
let addr = server.local_addr().unwrap();
let uri = to_http_uri(addr);
let handle = server.start(module).unwrap();
let handle = server.start(module);

// Send a valid batch.
let req = r#"[
Expand All @@ -551,7 +551,7 @@ async fn batch_limit_works() {
module.register_method("should_ok", |_, _ctx| "ok").unwrap();
let addr = server.local_addr().unwrap();
let uri = to_http_uri(addr);
let handle = server.start(module).unwrap();
let handle = server.start(module);

// Send a valid batch.
let req = r#"[
Expand Down
4 changes: 2 additions & 2 deletions server/src/tests/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn http_only_works() {
.unwrap();

let addr = server.local_addr().unwrap();
let _server_handle = server.start(module).unwrap();
let _server_handle = server.start(module);

let req = r#"{"jsonrpc":"2.0","method":"say_hello","id":1}"#;
let response = http_request(req.into(), to_http_uri(addr)).with_default_timeout().await.unwrap().unwrap();
Expand All @@ -79,7 +79,7 @@ async fn ws_only_works() {
.unwrap();

let addr = server.local_addr().unwrap();
let _server_handle = server.start(module).unwrap();
let _server_handle = server.start(module);

let req = r#"{"jsonrpc":"2.0","method":"say_hello","id":1}"#;
let response = http_request(req.into(), to_http_uri(addr)).with_default_timeout().await.unwrap().unwrap();
Expand Down
18 changes: 9 additions & 9 deletions server/src/tests/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn can_set_the_max_request_body_size() {
let mut module = RpcModule::new(());
module.register_method("anything", |_p, _cx| "a".repeat(100)).unwrap();
let addr = server.local_addr().unwrap();
let handle = server.start(module).unwrap();
let handle = server.start(module);

let mut client = WebSocketTestClient::new(addr).await.unwrap();

Expand Down Expand Up @@ -78,7 +78,7 @@ async fn can_set_the_max_response_body_size() {
let mut module = RpcModule::new(());
module.register_method("anything", |_p, _cx| "a".repeat(101)).unwrap();
let addr = server.local_addr().unwrap();
let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);

let mut client = WebSocketTestClient::new(addr).await.unwrap();

Expand All @@ -101,7 +101,7 @@ async fn can_set_the_max_response_size_to_batch() {
let mut module = RpcModule::new(());
module.register_method("anything", |_p, _cx| "a".repeat(51)).unwrap();
let addr = server.local_addr().unwrap();
let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);

let mut client = WebSocketTestClient::new(addr).await.unwrap();

Expand All @@ -125,7 +125,7 @@ async fn can_set_max_connections() {
module.register_method("anything", |_p, _cx| ()).unwrap();
let addr = server.local_addr().unwrap();

let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);

let conn1 = WebSocketTestClient::new(addr).await;
let conn2 = WebSocketTestClient::new(addr).await;
Expand Down Expand Up @@ -575,7 +575,7 @@ async fn custom_subscription_id_works() {
}
})
.unwrap();
let _handle = server.start(module).unwrap();
let _handle = server.start(module);

let mut client = WebSocketTestClient::new(addr).with_default_timeout().await.unwrap().unwrap();

Expand All @@ -600,7 +600,7 @@ async fn disabled_batches() {
module.register_method("should_ok", |_, _ctx| "ok").unwrap();
let addr = server.local_addr().unwrap();

let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);

// Send a valid batch.
let mut client = WebSocketTestClient::new(addr).with_default_timeout().await.unwrap().unwrap();
Expand Down Expand Up @@ -630,7 +630,7 @@ async fn batch_limit_works() {
module.register_method("should_ok", |_, _ctx| "ok").unwrap();
let addr = server.local_addr().unwrap();

let server_handle = server.start(module).unwrap();
let server_handle = server.start(module);

// Send a valid batch.
let mut client = WebSocketTestClient::new(addr).with_default_timeout().await.unwrap().unwrap();
Expand Down Expand Up @@ -767,7 +767,7 @@ async fn ws_server_backpressure_works() {
.unwrap();
let addr = server.local_addr().unwrap();

let _server_handle = server.start(module).unwrap();
let _server_handle = server.start(module);

// Send a valid batch.
let mut client = WebSocketTestClient::new(addr).with_default_timeout().await.unwrap().unwrap();
Expand Down Expand Up @@ -899,5 +899,5 @@ async fn server_with_infinite_call(
.unwrap();
let addr = server.local_addr().unwrap();

(server.start(module).unwrap(), addr)
(server.start(module), addr)
}
Loading