Option to disable keep alive for JSON-RPC http transport (#9848)

* Keep alive jsonrpc CLI option.

* Update to latest json-rpc.

* Keep alive flag.
This commit is contained in:
Tomasz Drwięga 2018-11-05 15:39:51 +01:00 committed by Wei Tang
parent 3a6e04ba15
commit 59daf95859
8 changed files with 27 additions and 9 deletions

16
Cargo.lock generated
View File

@ -1552,7 +1552,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-core" name = "jsonrpc-core"
version = "9.0.0" 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 = [ dependencies = [
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1564,7 +1564,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-http-server" name = "jsonrpc-http-server"
version = "9.0.0" 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 = [ dependencies = [
"hyper 0.12.11 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)",
@ -1577,7 +1577,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-ipc-server" name = "jsonrpc-ipc-server"
version = "9.0.0" 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 = [ dependencies = [
"jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "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)", "jsonrpc-server-utils 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)",
@ -1590,7 +1590,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-macros" name = "jsonrpc-macros"
version = "9.0.0" 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 = [ dependencies = [
"jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "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)", "jsonrpc-pubsub 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)",
@ -1600,7 +1600,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-pubsub" name = "jsonrpc-pubsub"
version = "9.0.0" 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 = [ dependencies = [
"jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "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)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1610,7 +1610,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-server-utils" name = "jsonrpc-server-utils"
version = "9.0.0" 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 = [ dependencies = [
"bytes 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "globset 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1626,7 +1626,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-tcp-server" name = "jsonrpc-tcp-server"
version = "9.0.0" 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 = [ dependencies = [
"jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)", "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)", "jsonrpc-server-utils 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)",
@ -1638,7 +1638,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-ws-server" name = "jsonrpc-ws-server"
version = "9.0.0" 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 = [ dependencies = [
"error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "jsonrpc-core 9.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-2.2)",

View File

@ -467,6 +467,10 @@ usage! {
"--no-jsonrpc", "--no-jsonrpc",
"Disable the HTTP JSON-RPC API server.", "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(), ARG arg_jsonrpc_port: (u16) = 8545u16, or |c: &Config| c.rpc.as_ref()?.port.clone(),
"--jsonrpc-port=[PORT]", "--jsonrpc-port=[PORT]",
"Specify the port portion of the HTTP JSON-RPC API server.", "Specify the port portion of the HTTP JSON-RPC API server.",
@ -1219,6 +1223,7 @@ struct Rpc {
server_threads: Option<usize>, server_threads: Option<usize>,
processing_threads: Option<usize>, processing_threads: Option<usize>,
max_payload: Option<usize>, max_payload: Option<usize>,
keep_alive: Option<bool>,
} }
#[derive(Default, Debug, PartialEq, Deserialize)] #[derive(Default, Debug, PartialEq, Deserialize)]
@ -1676,6 +1681,7 @@ mod tests {
// -- API and Console Options // -- API and Console Options
// RPC // RPC
flag_no_jsonrpc: false, flag_no_jsonrpc: false,
flag_jsonrpc_no_keep_alive: false,
arg_jsonrpc_port: 8545u16, arg_jsonrpc_port: 8545u16,
arg_jsonrpc_interface: "local".into(), arg_jsonrpc_interface: "local".into(),
arg_jsonrpc_cors: "null".into(), arg_jsonrpc_cors: "null".into(),
@ -1958,6 +1964,7 @@ mod tests {
server_threads: None, server_threads: None,
processing_threads: None, processing_threads: None,
max_payload: None, max_payload: None,
keep_alive: None,
}), }),
ipc: Some(Ipc { ipc: Some(Ipc {
disable: None, disable: None,

View File

@ -855,6 +855,7 @@ impl Configuration {
Some(max) if max > 0 => max as usize, Some(max) if max > 0 => max as usize,
_ => 5usize, _ => 5usize,
}, },
keep_alive: !self.args.flag_jsonrpc_no_keep_alive,
}; };
Ok(conf) Ok(conf)

View File

@ -44,6 +44,7 @@ pub struct HttpConfiguration {
pub server_threads: usize, pub server_threads: usize,
pub processing_threads: usize, pub processing_threads: usize,
pub max_payload: usize, pub max_payload: usize,
pub keep_alive: bool,
} }
impl Default for HttpConfiguration { impl Default for HttpConfiguration {
@ -58,6 +59,7 @@ impl Default for HttpConfiguration {
server_threads: 1, server_threads: 1,
processing_threads: 4, processing_threads: 4,
max_payload: 5, max_payload: 5,
keep_alive: true,
} }
} }
} }
@ -218,6 +220,7 @@ pub fn new_http<D: rpc_apis::Dependencies>(
rpc::RpcExtractor, rpc::RpcExtractor,
conf.server_threads, conf.server_threads,
conf.max_payload, conf.max_payload,
conf.keep_alive,
); );
match start_result { match start_result {

View File

@ -137,6 +137,7 @@ pub fn start_http<M, S, H, T>(
extractor: T, extractor: T,
threads: usize, threads: usize,
max_payload: usize, max_payload: usize,
keep_alive: bool,
) -> ::std::io::Result<HttpServer> where ) -> ::std::io::Result<HttpServer> where
M: jsonrpc_core::Metadata, M: jsonrpc_core::Metadata,
S: jsonrpc_core::Middleware<M>, S: jsonrpc_core::Middleware<M>,
@ -145,6 +146,7 @@ pub fn start_http<M, S, H, T>(
{ {
let extractor = http_common::MetaExtractor::new(extractor); let extractor = http_common::MetaExtractor::new(extractor);
Ok(http::ServerBuilder::with_meta_extractor(handler, extractor) Ok(http::ServerBuilder::with_meta_extractor(handler, extractor)
.keep_alive(keep_alive)
.threads(threads) .threads(threads)
.cors(cors_domains.into()) .cors(cors_domains.into())
.allowed_hosts(allowed_hosts.into()) .allowed_hosts(allowed_hosts.into())
@ -163,6 +165,7 @@ pub fn start_http_with_middleware<M, S, H, T, R>(
middleware: R, middleware: R,
threads: usize, threads: usize,
max_payload: usize, max_payload: usize,
keep_alive: bool,
) -> ::std::io::Result<HttpServer> where ) -> ::std::io::Result<HttpServer> where
M: jsonrpc_core::Metadata, M: jsonrpc_core::Metadata,
S: jsonrpc_core::Middleware<M>, S: jsonrpc_core::Middleware<M>,
@ -172,6 +175,7 @@ pub fn start_http_with_middleware<M, S, H, T, R>(
{ {
let extractor = http_common::MetaExtractor::new(extractor); let extractor = http_common::MetaExtractor::new(extractor);
Ok(http::ServerBuilder::with_meta_extractor(handler, extractor) Ok(http::ServerBuilder::with_meta_extractor(handler, extractor)
.keep_alive(keep_alive)
.threads(threads) .threads(threads)
.cors(cors_domains.into()) .cors(cors_domains.into())
.allowed_hosts(allowed_hosts.into()) .allowed_hosts(allowed_hosts.into())

View File

@ -40,6 +40,7 @@ fn serve(handler: Option<MetaIoHandler<Metadata>>) -> Server<HttpServer> {
}, },
1, 1,
5, 5,
false,
).unwrap()) ).unwrap())
} }

View File

@ -218,9 +218,10 @@ impl<M: core::Middleware<Metadata>> WsDispatcher<M> {
impl<M: core::Middleware<Metadata>> core::Middleware<Metadata> for WsDispatcher<M> { impl<M: core::Middleware<Metadata>> core::Middleware<Metadata> for WsDispatcher<M> {
type Future = Either< type Future = Either<
core::FutureRpcResult<M::Future>, core::FutureRpcResult<M::Future, M::CallFuture>,
core::FutureResponse, core::FutureResponse,
>; >;
type CallFuture = core::middleware::NoopCallFuture;
fn on_request<F, X>(&self, request: core::Request, meta: Metadata, process: F) fn on_request<F, X>(&self, request: core::Request, meta: Metadata, process: F)
-> Either<Self::Future, X> -> Either<Self::Future, X>

View File

@ -205,6 +205,7 @@ impl<T: ActivityNotifier> Middleware<T> {
impl<M: core::Metadata, T: ActivityNotifier> core::Middleware<M> for Middleware<T> { impl<M: core::Metadata, T: ActivityNotifier> core::Middleware<M> for Middleware<T> {
type Future = core::FutureResponse; type Future = core::FutureResponse;
type CallFuture = core::middleware::NoopCallFuture;
fn on_request<F, X>(&self, request: core::Request, meta: M, process: F) -> Either<Self::Future, X> where fn on_request<F, X>(&self, request: core::Request, meta: M, process: F) -> Either<Self::Future, X> where
F: FnOnce(core::Request, M) -> X, F: FnOnce(core::Request, M) -> X,