diff --git a/examples/examples/jsonrpsee_as_service.rs b/examples/examples/jsonrpsee_as_service.rs index 7b098d1a0a..cb8c303144 100644 --- a/examples/examples/jsonrpsee_as_service.rs +++ b/examples/examples/jsonrpsee_as_service.rs @@ -42,7 +42,9 @@ use jsonrpsee::core::async_trait; use jsonrpsee::http_client::HttpClient; use jsonrpsee::proc_macros::rpc; use jsonrpsee::server::middleware::rpc::{ResponseFuture, RpcServiceBuilder, RpcServiceT}; -use jsonrpsee::server::{serve_with_graceful_shutdown, stop_channel, ServerHandle, StopHandle, TowerServiceBuilder}; +use jsonrpsee::server::{ + serve_with_graceful_shutdown, stop_channel, ServerConfig, ServerHandle, StopHandle, TowerServiceBuilder, +}; use jsonrpsee::types::{ErrorObject, ErrorObjectOwned, Request}; use jsonrpsee::ws_client::{HeaderValue, WsClientBuilder}; use jsonrpsee::{MethodResponse, Methods}; @@ -174,8 +176,8 @@ async fn run_server(metrics: Metrics) -> anyhow::Result { stop_handle: stop_handle.clone(), metrics, svc_builder: jsonrpsee::server::Server::builder() + .set_config(ServerConfig::builder().max_connections(33).build()) .set_http_middleware(tower::ServiceBuilder::new().layer(CorsLayer::permissive())) - .max_connections(33) .to_service_builder(), }; diff --git a/examples/examples/ws_pubsub_broadcast.rs b/examples/examples/ws_pubsub_broadcast.rs index 8e7a97905d..39f306f6ba 100644 --- a/examples/examples/ws_pubsub_broadcast.rs +++ b/examples/examples/ws_pubsub_broadcast.rs @@ -33,7 +33,7 @@ use futures::StreamExt; use jsonrpsee::core::client::{Subscription, SubscriptionClientT}; use jsonrpsee::core::server::SubscriptionMessage; use jsonrpsee::rpc_params; -use jsonrpsee::server::{RpcModule, Server}; +use jsonrpsee::server::{RpcModule, Server, ServerConfig}; use jsonrpsee::ws_client::WsClientBuilder; use jsonrpsee::PendingSubscriptionSink; use tokio::sync::broadcast; @@ -66,7 +66,8 @@ async fn main() -> anyhow::Result<()> { async fn run_server() -> anyhow::Result { // let's configure the server only hold 5 messages in memory. - let server = Server::builder().set_message_buffer_capacity(5).build("127.0.0.1:0").await?; + let config = ServerConfig::builder().set_message_buffer_capacity(5).build(); + let server = Server::builder().set_config(config).build("127.0.0.1:0").await?; let (tx, _rx) = broadcast::channel::(16); let mut module = RpcModule::new(tx.clone()); diff --git a/examples/examples/ws_pubsub_with_params.rs b/examples/examples/ws_pubsub_with_params.rs index a5f3526493..677c9a5c2d 100644 --- a/examples/examples/ws_pubsub_with_params.rs +++ b/examples/examples/ws_pubsub_with_params.rs @@ -30,7 +30,7 @@ use std::time::Duration; use futures::{Stream, StreamExt}; use jsonrpsee::core::client::{Subscription, SubscriptionClientT}; use jsonrpsee::core::Serialize; -use jsonrpsee::server::{RpcModule, Server, SubscriptionMessage, TrySendError}; +use jsonrpsee::server::{RpcModule, Server, ServerConfig, SubscriptionMessage, TrySendError}; use jsonrpsee::ws_client::WsClientBuilder; use jsonrpsee::{rpc_params, PendingSubscriptionSink}; use tokio::time::interval; @@ -63,7 +63,8 @@ async fn main() -> anyhow::Result<()> { async fn run_server() -> anyhow::Result { const LETTERS: &str = "abcdefghijklmnopqrstuvxyz"; - let server = Server::builder().set_message_buffer_capacity(10).build("127.0.0.1:0").await?; + let config = ServerConfig::builder().set_message_buffer_capacity(10).build(); + let server = Server::builder().set_config(config).build("127.0.0.1:0").await?; let mut module = RpcModule::new(()); module .register_subscription( diff --git a/server/src/lib.rs b/server/src/lib.rs index 6b2fb53438..d949628b40 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -49,8 +49,8 @@ pub use jsonrpsee_core::{id_providers::*, traits::IdProvider}; pub use jsonrpsee_types as types; pub use middleware::rpc::RpcServiceBuilder; pub use server::{ - BatchRequestConfig, Builder as ServerBuilder, ConnectionState, PingConfig, Server, ServerConfig, TowerService, - TowerServiceBuilder, + BatchRequestConfig, Builder as ServerBuilder, ConnectionState, PingConfig, Server, ServerConfig, + ServerConfigBuilder, TowerService, TowerServiceBuilder, }; pub use tracing; diff --git a/server/src/server.rs b/server/src/server.rs index dd70b5ab10..77ffcedf50 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -203,6 +203,7 @@ pub struct ServerConfig { pub(crate) tcp_no_delay: bool, } +/// The builder to configure and create a JSON-RPC server configuration. #[derive(Debug, Clone)] pub struct ServerConfigBuilder { /// Maximum size in bytes of a request. @@ -377,74 +378,151 @@ impl ServerConfigBuilder { Self::default() } - /// See [`Builder::max_request_body_size`] for documentation. + /// Set the maximum size of a request body in bytes. Default is 10 MiB. pub fn max_request_body_size(mut self, size: u32) -> Self { self.max_request_body_size = size; self } - /// See [`Builder::max_response_body_size`] for documentation. + /// Set the maximum size of a response body in bytes. Default is 10 MiB. pub fn max_response_body_size(mut self, size: u32) -> Self { self.max_response_body_size = size; self } - /// See [`Builder::max_connections`] for documentation. + /// Set the maximum number of connections allowed. Default is 100. pub fn max_connections(mut self, max: u32) -> Self { self.max_connections = max; self } - /// See [`Builder::set_batch_request_config`] for documentation. + /// Set the maximum number of connections allowed. Default is 1024. + pub fn max_subscriptions_per_connection(mut self, max: u32) -> Self { + self.max_subscriptions_per_connection = max; + self + } + + /// Configure how [batch requests](https://www.jsonrpc.org/specification#batch) shall be handled + /// by the server. + /// + /// Default: batch requests are allowed and can be arbitrary big but the maximum payload size is limited. pub fn set_batch_request_config(mut self, cfg: BatchRequestConfig) -> Self { self.batch_requests_config = cfg; self } - /// See [`Builder::max_subscriptions_per_connection`] for documentation. - pub fn max_subscriptions_per_connection(mut self, max: u32) -> Self { - self.max_subscriptions_per_connection = max; + /// Configure a custom [`tokio::runtime::Handle`] to run the server on. + /// + /// Default: [`tokio::spawn`] + pub fn custom_tokio_runtime(mut self, rt: tokio::runtime::Handle) -> Self { + self.tokio_runtime = Some(rt); self } - /// See [`Builder::http_only`] for documentation. + /// Configure the server to only serve JSON-RPC HTTP requests. + /// + /// Default: both http and ws are enabled. pub fn http_only(mut self) -> Self { self.enable_http = true; self.enable_ws = false; self } - /// See [`Builder::ws_only`] for documentation. + /// Configure the server to only serve JSON-RPC WebSocket requests. + /// + /// That implies that server just denies HTTP requests which isn't a WebSocket upgrade request + /// + /// Default: both http and ws are enabled. pub fn ws_only(mut self) -> Self { self.enable_http = false; self.enable_ws = true; self } - /// See [`Builder::set_message_buffer_capacity`] for documentation. + /// The server enforces backpressure which means that + /// `n` messages can be buffered and if the client + /// can't keep with up the server. + /// + /// This `capacity` is applied per connection and + /// applies globally on the connection which implies + /// all JSON-RPC messages. + /// + /// For example if a subscription produces plenty of new items + /// and the client can't keep up then no new messages are handled. + /// + /// If this limit is exceeded then the server will "back-off" + /// and only accept new messages once the client reads pending messages. + /// + /// # Panics + /// + /// Panics if the buffer capacity is 0. + /// pub fn set_message_buffer_capacity(mut self, c: u32) -> Self { self.message_buffer_capacity = c; self } - /// See [`Builder::enable_ws_ping`] for documentation. + /// Enable WebSocket ping/pong on the server. + /// + /// Default: pings are disabled. + /// + /// # Examples + /// + /// ```rust + /// use std::{time::Duration, num::NonZeroUsize}; + /// use jsonrpsee_server::{ServerConfigBuilder, PingConfig}; + /// + /// // Set the ping interval to 10 seconds but terminates the connection if a client is inactive for more than 2 minutes + /// let ping_cfg = PingConfig::new().ping_interval(Duration::from_secs(10)).inactive_limit(Duration::from_secs(60 * 2)); + /// let builder = ServerConfigBuilder::default().enable_ws_ping(ping_cfg); + /// ``` pub fn enable_ws_ping(mut self, config: PingConfig) -> Self { self.ping_config = Some(config); self } - /// See [`Builder::disable_ws_ping`] for documentation. + /// Disable WebSocket ping/pong on the server. + /// + /// Default: pings are disabled. pub fn disable_ws_ping(mut self) -> Self { self.ping_config = None; self } - /// See [`Builder::set_id_provider`] for documentation. + /// Configure custom `subscription ID` provider for the server to use + /// to when getting new subscription calls. + /// + /// You may choose static dispatch or dynamic dispatch because + /// `IdProvider` is implemented for `Box`. + /// + /// Default: [`RandomIntegerIdProvider`]. + /// + /// # Examples + /// + /// ```rust + /// use jsonrpsee_server::{ServerConfigBuilder, RandomStringIdProvider, IdProvider}; + /// + /// // static dispatch + /// let builder1 = ServerConfigBuilder::default().set_id_provider(RandomStringIdProvider::new(16)); + /// + /// // or dynamic dispatch + /// let builder2 = ServerConfigBuilder::default().set_id_provider(Box::new(RandomStringIdProvider::new(16))); + /// ``` + /// pub fn set_id_provider(mut self, id_provider: I) -> Self { self.id_provider = Arc::new(id_provider); self } + /// Configure `TCP_NODELAY` on the socket to the supplied value `nodelay`. + /// + /// Default is `true`. + pub fn set_tcp_no_delay(mut self, no_delay: bool) -> Self { + self.tcp_no_delay = no_delay; + self + } + + /// Build the [`ServerConfig`]. pub fn build(self) -> ServerConfig { ServerConfig { max_request_body_size: self.max_request_body_size, @@ -463,7 +541,7 @@ impl ServerConfigBuilder { } } -/// Builder to configure and create a JSON-RPC server +/// Builder to configure and create a JSON-RPC server. #[derive(Debug)] pub struct Builder { server_cfg: ServerConfig, @@ -486,6 +564,11 @@ impl Builder { pub fn new() -> Self { Self::default() } + + /// Create a server builder with the given [`ServerConfig`]. + pub fn with_config(config: ServerConfig) -> Self { + Self { server_cfg: config, ..Default::default() } + } } impl TowerServiceBuilder { @@ -553,36 +636,9 @@ impl TowerServiceBuilder Builder { - /// Set the maximum size of a request body in bytes. Default is 10 MiB. - pub fn max_request_body_size(mut self, size: u32) -> Self { - self.server_cfg.max_request_body_size = size; - self - } - - /// Set the maximum size of a response body in bytes. Default is 10 MiB. - pub fn max_response_body_size(mut self, size: u32) -> Self { - self.server_cfg.max_response_body_size = size; - self - } - - /// Set the maximum number of connections allowed. Default is 100. - pub fn max_connections(mut self, max: u32) -> Self { - self.server_cfg.max_connections = max; - self - } - - /// Configure how [batch requests](https://www.jsonrpc.org/specification#batch) shall be handled - /// by the server. - /// - /// Default: batch requests are allowed and can be arbitrary big but the maximum payload size is limited. - pub fn set_batch_request_config(mut self, cfg: BatchRequestConfig) -> Self { - self.server_cfg.batch_requests_config = cfg; - self - } - - /// Set the maximum number of connections allowed. Default is 1024. - pub fn max_subscriptions_per_connection(mut self, max: u32) -> Self { - self.server_cfg.max_subscriptions_per_connection = max; + /// Configure the [`ServerConfig`]. + pub fn set_config(mut self, cfg: ServerConfig) -> Self { + self.server_cfg = cfg; self } @@ -648,66 +704,6 @@ impl Builder { Builder { server_cfg: self.server_cfg, rpc_middleware, http_middleware: self.http_middleware } } - /// Configure a custom [`tokio::runtime::Handle`] to run the server on. - /// - /// Default: [`tokio::spawn`] - pub fn custom_tokio_runtime(mut self, rt: tokio::runtime::Handle) -> Self { - self.server_cfg.tokio_runtime = Some(rt); - self - } - - /// Enable WebSocket ping/pong on the server. - /// - /// Default: pings are disabled. - /// - /// # Examples - /// - /// ```rust - /// use std::{time::Duration, num::NonZeroUsize}; - /// use jsonrpsee_server::{ServerBuilder, PingConfig}; - /// - /// // Set the ping interval to 10 seconds but terminates the connection if a client is inactive for more than 2 minutes - /// let ping_cfg = PingConfig::new().ping_interval(Duration::from_secs(10)).inactive_limit(Duration::from_secs(60 * 2)); - /// let builder = ServerBuilder::default().enable_ws_ping(ping_cfg); - /// ``` - pub fn enable_ws_ping(mut self, config: PingConfig) -> Self { - self.server_cfg.ping_config = Some(config); - self - } - - /// Disable WebSocket ping/pong on the server. - /// - /// Default: pings are disabled. - pub fn disable_ws_ping(mut self) -> Self { - self.server_cfg.ping_config = None; - self - } - - /// Configure custom `subscription ID` provider for the server to use - /// to when getting new subscription calls. - /// - /// You may choose static dispatch or dynamic dispatch because - /// `IdProvider` is implemented for `Box`. - /// - /// Default: [`RandomIntegerIdProvider`]. - /// - /// # Examples - /// - /// ```rust - /// use jsonrpsee_server::{ServerBuilder, RandomStringIdProvider, IdProvider}; - /// - /// // static dispatch - /// let builder1 = ServerBuilder::default().set_id_provider(RandomStringIdProvider::new(16)); - /// - /// // or dynamic dispatch - /// let builder2 = ServerBuilder::default().set_id_provider(Box::new(RandomStringIdProvider::new(16))); - /// ``` - /// - pub fn set_id_provider(mut self, id_provider: I) -> Self { - self.server_cfg.id_provider = Arc::new(id_provider); - self - } - /// Configure a custom [`tower::ServiceBuilder`] middleware for composing layers to be applied to the RPC service. /// /// Default: No tower layers are applied to the RPC service. @@ -734,57 +730,6 @@ impl Builder { Builder { server_cfg: self.server_cfg, http_middleware, rpc_middleware: self.rpc_middleware } } - /// Configure `TCP_NODELAY` on the socket to the supplied value `nodelay`. - /// - /// Default is `true`. - pub fn set_tcp_no_delay(mut self, no_delay: bool) -> Self { - self.server_cfg.tcp_no_delay = no_delay; - self - } - - /// Configure the server to only serve JSON-RPC HTTP requests. - /// - /// Default: both http and ws are enabled. - pub fn http_only(mut self) -> Self { - self.server_cfg.enable_http = true; - self.server_cfg.enable_ws = false; - self - } - - /// Configure the server to only serve JSON-RPC WebSocket requests. - /// - /// That implies that server just denies HTTP requests which isn't a WebSocket upgrade request - /// - /// Default: both http and ws are enabled. - pub fn ws_only(mut self) -> Self { - self.server_cfg.enable_http = false; - self.server_cfg.enable_ws = true; - self - } - - /// The server enforces backpressure which means that - /// `n` messages can be buffered and if the client - /// can't keep with up the server. - /// - /// This `capacity` is applied per connection and - /// applies globally on the connection which implies - /// all JSON-RPC messages. - /// - /// For example if a subscription produces plenty of new items - /// and the client can't keep up then no new messages are handled. - /// - /// If this limit is exceeded then the server will "back-off" - /// and only accept new messages once the client reads pending messages. - /// - /// # Panics - /// - /// Panics if the buffer capacity is 0. - /// - pub fn set_message_buffer_capacity(mut self, c: u32) -> Self { - self.server_cfg.message_buffer_capacity = c; - self - } - /// Convert the server builder to a [`TowerServiceBuilder`]. /// /// This can be used to utilize the [`TowerService`] from jsonrpsee. @@ -792,7 +737,7 @@ impl Builder { /// # Examples /// /// ```no_run - /// use jsonrpsee_server::{Methods, ServerHandle, ws, stop_channel, serve_with_graceful_shutdown}; + /// use jsonrpsee_server::{Methods, ServerConfig, ServerHandle, ws, stop_channel, serve_with_graceful_shutdown}; /// use tower::Service; /// use std::{error::Error as StdError, net::SocketAddr}; /// use futures_util::future::{self, Either}; @@ -800,7 +745,9 @@ impl Builder { /// /// fn run_server() -> ServerHandle { /// let (stop_handle, server_handle) = stop_channel(); - /// let svc_builder = jsonrpsee_server::Server::builder().max_connections(33).to_service_builder(); + /// let svc_builder = jsonrpsee_server::Server::builder() + /// .set_config(ServerConfig::builder().max_connections(33).build()) + /// .to_service_builder(); /// let methods = Methods::new(); /// let stop_handle = stop_handle.clone(); /// diff --git a/server/src/tests/helpers.rs b/server/src/tests/helpers.rs index 518587711a..5bd4f580f9 100644 --- a/server/src/tests/helpers.rs +++ b/server/src/tests/helpers.rs @@ -3,7 +3,9 @@ use std::sync::atomic::Ordering; use std::sync::Arc; use std::{fmt, sync::atomic::AtomicUsize}; -use crate::{serve_with_graceful_shutdown, stop_channel, RpcModule, Server, ServerBuilder, ServerHandle}; +use crate::{ + serve_with_graceful_shutdown, stop_channel, RpcModule, Server, ServerBuilder, ServerConfigBuilder, ServerHandle, +}; use futures_util::FutureExt; use jsonrpsee_core::server::Methods; @@ -213,7 +215,10 @@ pub(crate) async fn ws_server_with_stats(metrics: Metrics) -> SocketAddr { let (stop_handle, server_handle) = stop_channel(); let metrics = metrics.clone(); - let rpc_svc = Server::builder().max_connections(33).to_service_builder().build(Methods::new(), stop_handle.clone()); + let rpc_svc = Server::builder() + .set_config(ServerConfigBuilder::new().max_connections(33).build()) + .to_service_builder() + .build(Methods::new(), stop_handle.clone()); tokio::spawn(async move { loop { diff --git a/server/src/tests/http.rs b/server/src/tests/http.rs index 1897def96b..13ad53af61 100644 --- a/server/src/tests/http.rs +++ b/server/src/tests/http.rs @@ -26,7 +26,7 @@ use std::net::SocketAddr; -use crate::{BatchRequestConfig, RegisterMethodError, RpcModule, ServerBuilder, ServerHandle}; +use crate::{BatchRequestConfig, RegisterMethodError, RpcModule, ServerBuilder, ServerConfig, ServerHandle}; use jsonrpsee_core::RpcResult; use jsonrpsee_test_utils::helpers::*; use jsonrpsee_test_utils::mocks::{Id, StatusCode}; @@ -455,7 +455,8 @@ async fn can_register_modules() { async fn can_set_the_max_request_body_size() { let addr = "127.0.0.1:0"; // Rejects all requests larger than 100 bytes - let server = ServerBuilder::default().max_request_body_size(100).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_request_body_size(100).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_p, _cx, _| "a".repeat(100)).unwrap(); let addr = server.local_addr().unwrap(); @@ -480,7 +481,8 @@ async fn can_set_the_max_request_body_size() { async fn can_set_the_max_response_size() { let addr = "127.0.0.1:0"; // Set the max response size to 100 bytes - let server = ServerBuilder::default().max_response_body_size(100).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_response_body_size(100).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_p, _cx, _| "a".repeat(101)).unwrap(); let addr = server.local_addr().unwrap(); @@ -500,7 +502,8 @@ async fn can_set_the_max_response_size() { async fn can_set_the_max_response_size_to_batch() { let addr = "127.0.0.1:0"; // Set the max response size to 100 bytes - let server = ServerBuilder::default().max_response_body_size(100).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_response_body_size(100).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_p, _cx, _| "a".repeat(51)).unwrap(); let addr = server.local_addr().unwrap(); @@ -520,8 +523,8 @@ async fn can_set_the_max_response_size_to_batch() { async fn disabled_batches() { let addr = "127.0.0.1:0"; // Disable batches support. - let server = - ServerBuilder::default().set_batch_request_config(BatchRequestConfig::Disabled).build(addr).await.unwrap(); + let config = ServerConfig::builder().set_batch_request_config(BatchRequestConfig::Disabled).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("should_ok", |_, _ctx, _| "ok").unwrap(); let addr = server.local_addr().unwrap(); @@ -544,8 +547,8 @@ async fn disabled_batches() { async fn batch_limit_works() { let addr = "127.0.0.1:0"; // Disable batches support. - let server = - ServerBuilder::default().set_batch_request_config(BatchRequestConfig::Limit(1)).build(addr).await.unwrap(); + let config = ServerConfig::builder().set_batch_request_config(BatchRequestConfig::Limit(1)).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("should_ok", |_, _ctx, _| "ok").unwrap(); let addr = server.local_addr().unwrap(); diff --git a/server/src/tests/shared.rs b/server/src/tests/shared.rs index d20e5ec10e..5468aca09c 100644 --- a/server/src/tests/shared.rs +++ b/server/src/tests/shared.rs @@ -1,4 +1,5 @@ use crate::tests::helpers::{init_logger, server_with_handles}; +use crate::ServerConfig; use hyper::StatusCode; use jsonrpsee_test_utils::helpers::{http_request, ok_response, to_http_uri}; use jsonrpsee_test_utils::mocks::{Id, WebSocketTestClient, WebSocketTestError}; @@ -42,8 +43,8 @@ async fn run_forever() { async fn http_only_works() { use crate::{RpcModule, ServerBuilder}; - let server = - ServerBuilder::default().http_only().build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); + let config = ServerConfig::builder().http_only().build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let mut module = RpcModule::new(()); module .register_method("say_hello", |_, _, _| { @@ -68,7 +69,8 @@ async fn http_only_works() { async fn ws_only_works() { use crate::{RpcModule, ServerBuilder}; - let server = ServerBuilder::default().ws_only().build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); + let config = ServerConfig::builder().ws_only().build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let mut module = RpcModule::new(()); module .register_method("say_hello", |_, _, _| { diff --git a/server/src/tests/ws.rs b/server/src/tests/ws.rs index dbedf03d72..4d8b54cfb7 100644 --- a/server/src/tests/ws.rs +++ b/server/src/tests/ws.rs @@ -29,7 +29,7 @@ use std::time::Duration; use crate::tests::helpers::{deser_call, init_logger, server_with_context, ws_server_with_stats, Metrics}; use crate::types::SubscriptionId; -use crate::{BatchRequestConfig, RegisterMethodError}; +use crate::{BatchRequestConfig, RegisterMethodError, ServerConfig}; use crate::{RpcModule, ServerBuilder}; use jsonrpsee_core::server::{SendTimeoutError, SubscriptionMessage}; use jsonrpsee_core::traits::IdProvider; @@ -47,7 +47,8 @@ async fn can_set_the_max_request_body_size() { let addr = "127.0.0.1:0"; // Rejects all requests larger than 100 bytes - let server = ServerBuilder::default().max_request_body_size(100).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_request_body_size(100).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_p, _cx, _| "a".repeat(100)).unwrap(); let addr = server.local_addr().unwrap(); @@ -75,7 +76,8 @@ async fn can_set_the_max_response_body_size() { let addr = "127.0.0.1:0"; // Set the max response body size to 100 bytes - let server = ServerBuilder::default().max_response_body_size(100).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_response_body_size(100).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_, _, _| "a".repeat(101)).unwrap(); let addr = server.local_addr().unwrap(); @@ -98,7 +100,8 @@ async fn can_set_the_max_response_size_to_batch() { let addr = "127.0.0.1:0"; // Set the max response body size to 100 bytes - let server = ServerBuilder::default().max_response_body_size(100).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_response_body_size(100).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_p, _cx, _| "a".repeat(51)).unwrap(); let addr = server.local_addr().unwrap(); @@ -121,7 +124,8 @@ async fn can_set_max_connections() { let addr = "127.0.0.1:0"; // Server that accepts max 2 connections - let server = ServerBuilder::default().max_connections(2).build(addr).await.unwrap(); + let config = ServerConfig::builder().max_connections(2).build(); + let server = ServerBuilder::with_config(config).build(addr).await.unwrap(); let mut module = RpcModule::new(()); module.register_method("anything", |_, _, _| ()).unwrap(); let addr = server.local_addr().unwrap(); @@ -559,13 +563,8 @@ async fn custom_subscription_id_works() { } init_logger(); - let server = ServerBuilder::default() - .set_id_provider(HardcodedSubscriptionId) - .build("127.0.0.1:0") - .with_default_timeout() - .await - .unwrap() - .unwrap(); + let config = ServerConfig::builder().set_id_provider(HardcodedSubscriptionId).build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let addr = server.local_addr().unwrap(); let mut module = RpcModule::new(()); module @@ -594,13 +593,8 @@ async fn custom_subscription_id_works() { #[tokio::test] async fn disabled_batches() { // Disable batches support. - let server = ServerBuilder::default() - .set_batch_request_config(BatchRequestConfig::Disabled) - .build("127.0.0.1:0") - .with_default_timeout() - .await - .unwrap() - .unwrap(); + let config = ServerConfig::builder().set_batch_request_config(BatchRequestConfig::Disabled).build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let mut module = RpcModule::new(()); module.register_method("should_ok", |_, _ctx, _| "ok").unwrap(); @@ -624,13 +618,8 @@ async fn disabled_batches() { #[tokio::test] async fn batch_limit_works() { // Disable batches support. - let server = ServerBuilder::default() - .set_batch_request_config(BatchRequestConfig::Limit(1)) - .build("127.0.0.1:0") - .with_default_timeout() - .await - .unwrap() - .unwrap(); + let config = ServerConfig::builder().set_batch_request_config(BatchRequestConfig::Limit(1)).build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let mut module = RpcModule::new(()); module.register_method("should_ok", |_, _ctx, _| "ok").unwrap(); @@ -722,13 +711,8 @@ async fn ws_server_backpressure_works() { let (backpressure_tx, mut backpressure_rx) = tokio::sync::mpsc::channel::<()>(1); - let server = ServerBuilder::default() - .set_message_buffer_capacity(5) - .build("127.0.0.1:0") - .with_default_timeout() - .await - .unwrap() - .unwrap(); + let config = ServerConfig::builder().set_message_buffer_capacity(5).build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let mut module = RpcModule::new(backpressure_tx); @@ -908,14 +892,11 @@ async fn server_with_infinite_call( timeout: Duration, tx: tokio::sync::mpsc::UnboundedSender<()>, ) -> (crate::ServerHandle, std::net::SocketAddr) { - let server = ServerBuilder::default() - // Make sure that the ping_interval doesn't force the connection to be closed + // Make sure that the ping_interval doesn't force the connection to be closed + let config = ServerConfig::builder() .enable_ws_ping(crate::PingConfig::new().max_failures(usize::MAX).ping_interval(timeout)) - .build("127.0.0.1:0") - .with_default_timeout() - .await - .unwrap() - .unwrap(); + .build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap(); let mut module = RpcModule::new(tx); diff --git a/tests/tests/integration_tests.rs b/tests/tests/integration_tests.rs index 1571cfc4a7..81140a1d25 100644 --- a/tests/tests/integration_tests.rs +++ b/tests/tests/integration_tests.rs @@ -50,7 +50,7 @@ use jsonrpsee::core::server::SubscriptionMessage; use jsonrpsee::core::{JsonValue, StringError}; use jsonrpsee::http_client::HttpClientBuilder; use jsonrpsee::server::middleware::http::HostFilterLayer; -use jsonrpsee::server::{ConnectionGuard, ServerBuilder, ServerHandle}; +use jsonrpsee::server::{ConnectionGuard, ServerBuilder, ServerConfig, ServerHandle}; use jsonrpsee::types::error::{ErrorObject, UNKNOWN_ERROR_CODE}; use jsonrpsee::ws_client::WsClientBuilder; use jsonrpsee::{rpc_params, ResponsePayload, RpcModule}; @@ -826,7 +826,8 @@ async fn ws_server_limit_subs_per_conn_works() { init_logger(); - let server = ServerBuilder::default().max_subscriptions_per_connection(10).build("127.0.0.1:0").await.unwrap(); + let config = ServerConfig::builder().max_subscriptions_per_connection(10).build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").await.unwrap(); let server_url = format!("ws://{}", server.local_addr().unwrap()); let mut module = RpcModule::new(()); @@ -881,7 +882,8 @@ async fn ws_server_unsub_methods_should_ignore_sub_limit() { init_logger(); - let server = ServerBuilder::default().max_subscriptions_per_connection(10).build("127.0.0.1:0").await.unwrap(); + let config = ServerConfig::builder().max_subscriptions_per_connection(10).build(); + let server = ServerBuilder::with_config(config).build("127.0.0.1:0").await.unwrap(); let server_url = format!("ws://{}", server.local_addr().unwrap()); let mut module = RpcModule::new(());