From 59daf958595ccd064f4a67ea75120558182f379e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 5 Nov 2018 15:39:51 +0100 Subject: [PATCH] Option to disable keep alive for JSON-RPC http transport (#9848) * Keep alive jsonrpc CLI option. * Update to latest json-rpc. * Keep alive flag. --- Cargo.lock | 16 ++++++++-------- parity/cli/mod.rs | 7 +++++++ parity/configuration.rs | 1 + parity/rpc.rs | 3 +++ rpc/src/lib.rs | 4 ++++ rpc/src/tests/rpc.rs | 1 + rpc/src/v1/extractors.rs | 3 ++- rpc/src/v1/informant.rs | 1 + 8 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb79c020b..0ef2913ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1552,7 +1552,7 @@ dependencies = [ [[package]] name = "jsonrpc-core" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1564,7 +1564,7 @@ dependencies = [ [[package]] name = "jsonrpc-http-server" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "hyper 0.12.11 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", @@ -1577,7 +1577,7 @@ dependencies = [ [[package]] name = "jsonrpc-ipc-server" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "jsonrpc-server-utils 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", @@ -1590,7 +1590,7 @@ dependencies = [ [[package]] name = "jsonrpc-macros" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "jsonrpc-pubsub 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", @@ -1600,7 +1600,7 @@ dependencies = [ [[package]] name = "jsonrpc-pubsub" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1610,7 +1610,7 @@ dependencies = [ [[package]] name = "jsonrpc-server-utils" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "bytes 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1626,7 +1626,7 @@ dependencies = [ [[package]] name = "jsonrpc-tcp-server" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "jsonrpc-server-utils 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", @@ -1638,7 +1638,7 @@ dependencies = [ [[package]] name = "jsonrpc-ws-server" version = "9.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#207a277b098943864ecaf22dbab7a5e309866d6b" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2#f8a54f46f7f1d68b4e7899ca1e929803bf966a5b" dependencies = [ "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", diff --git a/parity/cli/mod.rs b/parity/cli/mod.rs index fce9b571c..d83b205ce 100644 --- a/parity/cli/mod.rs +++ b/parity/cli/mod.rs @@ -467,6 +467,10 @@ usage! { "--no-jsonrpc", "Disable the HTTP JSON-RPC API server.", + FLAG flag_jsonrpc_no_keep_alive: (bool) = false, or |c: &Config| c.rpc.as_ref()?.keep_alive, + "--jsonrpc-no-keep-alive", + "Disable HTTP/1.1 keep alive header. Disabling keep alive will prevent re-using the same TCP connection to fire multiple requests, recommended when using one request per connection.", + ARG arg_jsonrpc_port: (u16) = 8545u16, or |c: &Config| c.rpc.as_ref()?.port.clone(), "--jsonrpc-port=[PORT]", "Specify the port portion of the HTTP JSON-RPC API server.", @@ -1219,6 +1223,7 @@ struct Rpc { server_threads: Option, processing_threads: Option, max_payload: Option, + keep_alive: Option, } #[derive(Default, Debug, PartialEq, Deserialize)] @@ -1676,6 +1681,7 @@ mod tests { // -- API and Console Options // RPC flag_no_jsonrpc: false, + flag_jsonrpc_no_keep_alive: false, arg_jsonrpc_port: 8545u16, arg_jsonrpc_interface: "local".into(), arg_jsonrpc_cors: "null".into(), @@ -1958,6 +1964,7 @@ mod tests { server_threads: None, processing_threads: None, max_payload: None, + keep_alive: None, }), ipc: Some(Ipc { disable: None, diff --git a/parity/configuration.rs b/parity/configuration.rs index b84db3922..d81e36858 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -855,6 +855,7 @@ impl Configuration { Some(max) if max > 0 => max as usize, _ => 5usize, }, + keep_alive: !self.args.flag_jsonrpc_no_keep_alive, }; Ok(conf) diff --git a/parity/rpc.rs b/parity/rpc.rs index 4b08aa789..d77bc6d6d 100644 --- a/parity/rpc.rs +++ b/parity/rpc.rs @@ -44,6 +44,7 @@ pub struct HttpConfiguration { pub server_threads: usize, pub processing_threads: usize, pub max_payload: usize, + pub keep_alive: bool, } impl Default for HttpConfiguration { @@ -58,6 +59,7 @@ impl Default for HttpConfiguration { server_threads: 1, processing_threads: 4, max_payload: 5, + keep_alive: true, } } } @@ -218,6 +220,7 @@ pub fn new_http( rpc::RpcExtractor, conf.server_threads, conf.max_payload, + conf.keep_alive, ); match start_result { diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index e1f5423ca..6f635849b 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -137,6 +137,7 @@ pub fn start_http( extractor: T, threads: usize, max_payload: usize, + keep_alive: bool, ) -> ::std::io::Result where M: jsonrpc_core::Metadata, S: jsonrpc_core::Middleware, @@ -145,6 +146,7 @@ pub fn start_http( { let extractor = http_common::MetaExtractor::new(extractor); Ok(http::ServerBuilder::with_meta_extractor(handler, extractor) + .keep_alive(keep_alive) .threads(threads) .cors(cors_domains.into()) .allowed_hosts(allowed_hosts.into()) @@ -163,6 +165,7 @@ pub fn start_http_with_middleware( middleware: R, threads: usize, max_payload: usize, + keep_alive: bool, ) -> ::std::io::Result where M: jsonrpc_core::Metadata, S: jsonrpc_core::Middleware, @@ -172,6 +175,7 @@ pub fn start_http_with_middleware( { let extractor = http_common::MetaExtractor::new(extractor); Ok(http::ServerBuilder::with_meta_extractor(handler, extractor) + .keep_alive(keep_alive) .threads(threads) .cors(cors_domains.into()) .allowed_hosts(allowed_hosts.into()) diff --git a/rpc/src/tests/rpc.rs b/rpc/src/tests/rpc.rs index e4aead29d..656811d67 100644 --- a/rpc/src/tests/rpc.rs +++ b/rpc/src/tests/rpc.rs @@ -40,6 +40,7 @@ fn serve(handler: Option>) -> Server { }, 1, 5, + false, ).unwrap()) } diff --git a/rpc/src/v1/extractors.rs b/rpc/src/v1/extractors.rs index f0433db02..a7c1c40b1 100644 --- a/rpc/src/v1/extractors.rs +++ b/rpc/src/v1/extractors.rs @@ -218,9 +218,10 @@ impl> WsDispatcher { impl> core::Middleware for WsDispatcher { type Future = Either< - core::FutureRpcResult, + core::FutureRpcResult, core::FutureResponse, >; + type CallFuture = core::middleware::NoopCallFuture; fn on_request(&self, request: core::Request, meta: Metadata, process: F) -> Either diff --git a/rpc/src/v1/informant.rs b/rpc/src/v1/informant.rs index 3dab54843..dfa20ee46 100644 --- a/rpc/src/v1/informant.rs +++ b/rpc/src/v1/informant.rs @@ -205,6 +205,7 @@ impl Middleware { impl core::Middleware for Middleware { type Future = core::FutureResponse; + type CallFuture = core::middleware::NoopCallFuture; fn on_request(&self, request: core::Request, meta: M, process: F) -> Either where F: FnOnce(core::Request, M) -> X,