RPC cpu pool (#6023)

* RPC cpu pool.

* introduce optional thread pool when processing RPC requests.

* Bump jsonrpc.

* Removing boxes.

* Fix CLI tests.
This commit is contained in:
Tomasz Drwięga 2017-07-11 12:22:19 +02:00 committed by Gav Wood
parent dc51dde112
commit 7fb46bff06
10 changed files with 103 additions and 43 deletions

22
Cargo.lock generated
View File

@ -1061,7 +1061,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "jsonrpc-core" name = "jsonrpc-core"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1073,7 +1073,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-http-server" name = "jsonrpc-http-server"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"hyper 0.10.0-a.0 (git+https://github.com/paritytech/hyper)", "hyper 0.10.0-a.0 (git+https://github.com/paritytech/hyper)",
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
@ -1086,7 +1086,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-ipc-server" name = "jsonrpc-ipc-server"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
@ -1099,7 +1099,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-macros" name = "jsonrpc-macros"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"jsonrpc-pubsub 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-pubsub 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
@ -1109,7 +1109,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-minihttp-server" name = "jsonrpc-minihttp-server"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
@ -1124,7 +1124,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-pubsub" name = "jsonrpc-pubsub"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1134,8 +1134,9 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-server-utils" name = "jsonrpc-server-utils"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"globset 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1146,7 +1147,7 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-tcp-server" name = "jsonrpc-tcp-server"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
@ -1160,11 +1161,13 @@ dependencies = [
[[package]] [[package]]
name = "jsonrpc-ws-server" name = "jsonrpc-ws-server"
version = "7.0.0" version = "7.0.0"
source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#d12476f42ee672fa9d023f66fcfa5981d9aaba3a" source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7#5e79be8a098cdda221713992f4a46b41a1d4d8f0"
dependencies = [ dependencies = [
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"jsonrpc-server-utils 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-server-utils 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ws 0.7.1 (git+https://github.com/tomusdrw/ws-rs)", "ws 0.7.1 (git+https://github.com/tomusdrw/ws-rs)",
] ]
@ -1775,6 +1778,7 @@ dependencies = [
"ethsync 1.7.0", "ethsync 1.7.0",
"fetch 0.1.0", "fetch 0.1.0",
"futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"jsonrpc-http-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-http-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",
"jsonrpc-ipc-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-ipc-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)",

View File

@ -180,8 +180,10 @@ usage! {
or |c: &Config| otry!(c.rpc).apis.as_ref().map(|vec| vec.join(",")), or |c: &Config| otry!(c.rpc).apis.as_ref().map(|vec| vec.join(",")),
flag_jsonrpc_hosts: String = "none", flag_jsonrpc_hosts: String = "none",
or |c: &Config| otry!(c.rpc).hosts.as_ref().map(|vec| vec.join(",")), or |c: &Config| otry!(c.rpc).hosts.as_ref().map(|vec| vec.join(",")),
flag_jsonrpc_threads: Option<usize> = None, flag_jsonrpc_server_threads: Option<usize> = None,
or |c: &Config| otry!(c.rpc).threads.map(Some), or |c: &Config| otry!(c.rpc).server_threads.map(Some),
flag_jsonrpc_threads: usize = 0usize,
or |c: &Config| otry!(c.rpc).processing_threads,
// WS // WS
flag_no_ws: bool = false, flag_no_ws: bool = false,
@ -468,7 +470,8 @@ struct Rpc {
cors: Option<String>, cors: Option<String>,
apis: Option<Vec<String>>, apis: Option<Vec<String>>,
hosts: Option<Vec<String>>, hosts: Option<Vec<String>>,
threads: Option<usize>, server_threads: Option<usize>,
processing_threads: Option<usize>,
} }
#[derive(Default, Debug, PartialEq, Deserialize)] #[derive(Default, Debug, PartialEq, Deserialize)]
@ -749,7 +752,8 @@ mod tests {
flag_jsonrpc_cors: Some("null".into()), flag_jsonrpc_cors: Some("null".into()),
flag_jsonrpc_apis: "web3,eth,net,parity,traces,rpc,secretstore".into(), flag_jsonrpc_apis: "web3,eth,net,parity,traces,rpc,secretstore".into(),
flag_jsonrpc_hosts: "none".into(), flag_jsonrpc_hosts: "none".into(),
flag_jsonrpc_threads: None, flag_jsonrpc_server_threads: None,
flag_jsonrpc_threads: 0,
// WS // WS
flag_no_ws: false, flag_no_ws: false,
@ -977,7 +981,8 @@ mod tests {
cors: None, cors: None,
apis: None, apis: None,
hosts: None, hosts: None,
threads: None, server_threads: None,
processing_threads: None,
}), }),
ipc: Some(Ipc { ipc: Some(Ipc {
disable: None, disable: None,

View File

@ -176,9 +176,12 @@ API and Console Options:
is additional security against some attack is additional security against some attack
vectors. Special options: "all", "none", vectors. Special options: "all", "none",
(default: {flag_jsonrpc_hosts}). (default: {flag_jsonrpc_hosts}).
--jsonrpc-threads THREADS Enables experimental faster implementation of JSON-RPC server. --jsonrpc-server-threads NUM Enables experimental faster implementation of JSON-RPC server.
Requires Dapps server to be disabled Requires Dapps server to be disabled
using --no-dapps. (default: {flag_jsonrpc_threads:?}) using --no-dapps. (default: {flag_jsonrpc_server_threads:?})
--jsonrpc-threads THREADS Turn on additional processing threads in all RPC servers.
Setting this to non-zero value allows parallel cpu-heavy queries
execution. (default: {flag_jsonrpc_threads})
--no-ws Disable the WebSockets server. (default: {flag_no_ws}) --no-ws Disable the WebSockets server. (default: {flag_no_ws})
--ws-port PORT Specify the port portion of the WebSockets server --ws-port PORT Specify the port portion of the WebSockets server

View File

@ -137,7 +137,7 @@ impl Configuration {
let secretstore_conf = self.secretstore_config()?; let secretstore_conf = self.secretstore_config()?;
let format = self.format()?; let format = self.format()?;
if self.args.flag_jsonrpc_threads.is_some() && dapps_conf.enabled { if self.args.flag_jsonrpc_server_threads.is_some() && dapps_conf.enabled {
dapps_conf.enabled = false; dapps_conf.enabled = false;
writeln!(&mut stderr(), "Warning: Disabling Dapps server because fast RPC server was enabled.").expect("Error writing to stderr.") writeln!(&mut stderr(), "Warning: Disabling Dapps server because fast RPC server was enabled.").expect("Error writing to stderr.")
} }
@ -825,11 +825,12 @@ impl Configuration {
}, },
hosts: self.rpc_hosts(), hosts: self.rpc_hosts(),
cors: self.rpc_cors(), cors: self.rpc_cors(),
threads: match self.args.flag_jsonrpc_threads { server_threads: match self.args.flag_jsonrpc_server_threads {
Some(threads) if threads > 0 => Some(threads), Some(threads) if threads > 0 => Some(threads),
None => None, None => None,
_ => return Err("--jsonrpc-threads number needs to be positive.".into()), _ => return Err("--jsonrpc-server-threads number needs to be positive.".into()),
} },
processing_threads: self.args.flag_jsonrpc_threads,
}; };
Ok(conf) Ok(conf)

View File

@ -30,6 +30,7 @@ use rpc_apis::{self, ApiSet};
pub use parity_rpc::{IpcServer, HttpServer, RequestMiddleware}; pub use parity_rpc::{IpcServer, HttpServer, RequestMiddleware};
pub use parity_rpc::ws::Server as WsServer; pub use parity_rpc::ws::Server as WsServer;
pub use parity_rpc::informant::CpuPool;
pub const DAPPS_DOMAIN: &'static str = "web3.site"; pub const DAPPS_DOMAIN: &'static str = "web3.site";
@ -42,7 +43,8 @@ pub struct HttpConfiguration {
pub apis: ApiSet, pub apis: ApiSet,
pub cors: Option<Vec<String>>, pub cors: Option<Vec<String>>,
pub hosts: Option<Vec<String>>, pub hosts: Option<Vec<String>>,
pub threads: Option<usize>, pub server_threads: Option<usize>,
pub processing_threads: usize,
} }
impl HttpConfiguration { impl HttpConfiguration {
@ -63,7 +65,8 @@ impl Default for HttpConfiguration {
apis: ApiSet::UnsafeContext, apis: ApiSet::UnsafeContext,
cors: None, cors: None,
hosts: Some(Vec::new()), hosts: Some(Vec::new()),
threads: None, server_threads: None,
processing_threads: 0,
} }
} }
} }
@ -94,7 +97,8 @@ impl From<UiConfiguration> for HttpConfiguration {
apis: rpc_apis::ApiSet::SafeContext, apis: rpc_apis::ApiSet::SafeContext,
cors: None, cors: None,
hosts: conf.hosts, hosts: conf.hosts,
threads: None, server_threads: None,
processing_threads: 0,
} }
} }
} }
@ -176,6 +180,7 @@ pub struct Dependencies<D: rpc_apis::Dependencies> {
pub apis: Arc<D>, pub apis: Arc<D>,
pub remote: TokioRemote, pub remote: TokioRemote,
pub stats: Arc<RpcStats>, pub stats: Arc<RpcStats>,
pub pool: Option<CpuPool>,
} }
pub fn new_ws<D: rpc_apis::Dependencies>( pub fn new_ws<D: rpc_apis::Dependencies>(
@ -192,11 +197,12 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?; let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?;
let full_handler = setup_apis(rpc_apis::ApiSet::SafeContext, deps); let pool = deps.pool.clone();
let full_handler = setup_apis(rpc_apis::ApiSet::SafeContext, deps, pool.clone());
let handler = { let handler = {
let mut handler = MetaIoHandler::with_middleware(( let mut handler = MetaIoHandler::with_middleware((
rpc::WsDispatcher::new(full_handler), rpc::WsDispatcher::new(full_handler),
Middleware::new(deps.stats.clone(), deps.apis.activity_notifier()) Middleware::new(deps.stats.clone(), deps.apis.activity_notifier(), pool)
)); ));
let apis = conf.apis.list_apis(); let apis = conf.apis.list_apis();
deps.apis.extend_with_set(&mut handler, &apis); deps.apis.extend_with_set(&mut handler, &apis);
@ -252,7 +258,8 @@ pub fn new_http<D: rpc_apis::Dependencies>(
let http_address = (conf.interface, conf.port); let http_address = (conf.interface, conf.port);
let url = format!("{}:{}", http_address.0, http_address.1); let url = format!("{}:{}", http_address.0, http_address.1);
let addr = url.parse().map_err(|_| format!("Invalid {} listen host/port given: {}", id, url))?; let addr = url.parse().map_err(|_| format!("Invalid {} listen host/port given: {}", id, url))?;
let handler = setup_apis(conf.apis, deps); let pool = deps.pool.clone();
let handler = setup_apis(conf.apis, deps, pool);
let remote = deps.remote.clone(); let remote = deps.remote.clone();
let cors_domains = into_domains(conf.cors); let cors_domains = into_domains(conf.cors);
@ -265,7 +272,7 @@ pub fn new_http<D: rpc_apis::Dependencies>(
handler, handler,
remote, remote,
rpc::RpcExtractor, rpc::RpcExtractor,
match (conf.threads, middleware) { match (conf.server_threads, middleware) {
(Some(threads), None) => rpc::HttpSettings::Threads(threads), (Some(threads), None) => rpc::HttpSettings::Threads(threads),
(None, middleware) => rpc::HttpSettings::Dapps(middleware), (None, middleware) => rpc::HttpSettings::Dapps(middleware),
(Some(_), Some(_)) => { (Some(_), Some(_)) => {
@ -291,7 +298,8 @@ pub fn new_ipc<D: rpc_apis::Dependencies>(
return Ok(None); return Ok(None);
} }
let handler = setup_apis(conf.apis, dependencies); let pool = dependencies.pool.clone();
let handler = setup_apis(conf.apis, dependencies, pool);
let remote = dependencies.remote.clone(); let remote = dependencies.remote.clone();
match rpc::start_ipc(&conf.socket_addr, handler, remote, rpc::RpcExtractor) { match rpc::start_ipc(&conf.socket_addr, handler, remote, rpc::RpcExtractor) {
Ok(server) => Ok(Some(server)), Ok(server) => Ok(Some(server)),
@ -318,11 +326,11 @@ fn with_domain(items: Option<Vec<String>>, domain: &str, addresses: &[Option<(St
}) })
} }
fn setup_apis<D>(apis: ApiSet, deps: &Dependencies<D>) -> MetaIoHandler<Metadata, Middleware<D::Notifier>> fn setup_apis<D>(apis: ApiSet, deps: &Dependencies<D>, pool: Option<CpuPool>) -> MetaIoHandler<Metadata, Middleware<D::Notifier>>
where D: rpc_apis::Dependencies where D: rpc_apis::Dependencies
{ {
let mut handler = MetaIoHandler::with_middleware( let mut handler = MetaIoHandler::with_middleware(
Middleware::new(deps.stats.clone(), deps.apis.activity_notifier()) Middleware::new(deps.stats.clone(), deps.apis.activity_notifier(), pool)
); );
let apis = apis.list_apis(); let apis = apis.list_apis();
deps.apis.extend_with_set(&mut handler, &apis); deps.apis.extend_with_set(&mut handler, &apis);

View File

@ -316,6 +316,11 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) ->
apis: deps_for_rpc_apis.clone(), apis: deps_for_rpc_apis.clone(),
remote: event_loop.raw_remote(), remote: event_loop.raw_remote(),
stats: rpc_stats.clone(), stats: rpc_stats.clone(),
pool: if cmd.http_conf.processing_threads > 0 {
Some(rpc::CpuPool::new(cmd.http_conf.processing_threads))
} else {
None
},
}; };
// start rpc servers // start rpc servers
@ -663,6 +668,12 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R
apis: deps_for_rpc_apis.clone(), apis: deps_for_rpc_apis.clone(),
remote: event_loop.raw_remote(), remote: event_loop.raw_remote(),
stats: rpc_stats.clone(), stats: rpc_stats.clone(),
pool: if cmd.http_conf.processing_threads > 0 {
Some(rpc::CpuPool::new(cmd.http_conf.processing_threads))
} else {
None
},
}; };
// start rpc servers // start rpc servers

View File

@ -10,6 +10,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
cid = "0.2" cid = "0.2"
futures = "0.1" futures = "0.1"
futures-cpupool = "0.1"
log = "0.3" log = "0.3"
multihash ="0.6" multihash ="0.6"
order-stat = "0.1" order-stat = "0.1"

View File

@ -23,6 +23,7 @@
extern crate cid; extern crate cid;
extern crate crypto as rust_crypto; extern crate crypto as rust_crypto;
extern crate futures; extern crate futures;
extern crate futures_cpupool;
extern crate multihash; extern crate multihash;
extern crate order_stat; extern crate order_stat;
extern crate rand; extern crate rand;

View File

@ -217,18 +217,26 @@ 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> {
fn on_request<F>(&self, request: core::Request, meta: Metadata, process: F) -> core::FutureResponse where type Future = core::futures::future::Either<
F: FnOnce(core::Request, Metadata) -> core::FutureResponse, M::Future,
core::FutureResponse,
>;
fn on_request<F, X>(&self, request: core::Request, meta: Metadata, process: F) -> Self::Future where
F: FnOnce(core::Request, Metadata) -> X,
X: core::futures::Future<Item=Option<core::Response>, Error=()> + Send + 'static,
{ {
use self::core::futures::future::Either::{A, B};
let use_full = match &meta.origin { let use_full = match &meta.origin {
&Origin::Signer { .. } => true, &Origin::Signer { .. } => true,
_ => false, _ => false,
}; };
if use_full { if use_full {
self.full_handler.handle_rpc_request(request, meta) A(self.full_handler.handle_rpc_request(request, meta))
} else { } else {
process(request, meta) B(process(request, meta).boxed())
} }
} }
} }

View File

@ -21,10 +21,13 @@ use std::sync::Arc;
use std::sync::atomic::{self, AtomicUsize}; use std::sync::atomic::{self, AtomicUsize};
use std::time; use std::time;
use futures::Future; use futures::Future;
use futures_cpupool as pool;
use jsonrpc_core as rpc; use jsonrpc_core as rpc;
use order_stat; use order_stat;
use util::RwLock; use util::RwLock;
pub use self::pool::CpuPool;
const RATE_SECONDS: usize = 10; const RATE_SECONDS: usize = 10;
const STATS_SAMPLES: usize = 60; const STATS_SAMPLES: usize = 60;
@ -184,14 +187,16 @@ pub trait ActivityNotifier: Send + Sync + 'static {
pub struct Middleware<T: ActivityNotifier = ClientNotifier> { pub struct Middleware<T: ActivityNotifier = ClientNotifier> {
stats: Arc<RpcStats>, stats: Arc<RpcStats>,
notifier: T, notifier: T,
pool: Option<CpuPool>,
} }
impl<T: ActivityNotifier> Middleware<T> { impl<T: ActivityNotifier> Middleware<T> {
/// Create new Middleware with stats counter and activity notifier. /// Create new Middleware with stats counter and activity notifier.
pub fn new(stats: Arc<RpcStats>, notifier: T) -> Self { pub fn new(stats: Arc<RpcStats>, notifier: T, pool: Option<CpuPool>) -> Self {
Middleware { Middleware {
stats: stats, stats,
notifier: notifier, notifier,
pool,
} }
} }
@ -201,19 +206,32 @@ impl<T: ActivityNotifier> Middleware<T> {
} }
impl<M: rpc::Metadata, T: ActivityNotifier> rpc::Middleware<M> for Middleware<T> { impl<M: rpc::Metadata, T: ActivityNotifier> rpc::Middleware<M> for Middleware<T> {
fn on_request<F>(&self, request: rpc::Request, meta: M, process: F) -> rpc::FutureResponse where type Future = rpc::futures::future::Either<
F: FnOnce(rpc::Request, M) -> rpc::FutureResponse, pool::CpuFuture<Option<rpc::Response>, ()>,
rpc::FutureResponse,
>;
fn on_request<F, X>(&self, request: rpc::Request, meta: M, process: F) -> Self::Future where
F: FnOnce(rpc::Request, M) -> X,
X: rpc::futures::Future<Item=Option<rpc::Response>, Error=()> + Send + 'static,
{ {
use self::rpc::futures::future::Either::{A, B};
let start = time::Instant::now(); let start = time::Instant::now();
let response = process(request, meta);
self.notifier.active(); self.notifier.active();
self.stats.count_request();
let stats = self.stats.clone(); let stats = self.stats.clone();
stats.count_request(); let future = process(request, meta).map(move |res| {
response.map(move |res| {
stats.add_roundtrip(Self::as_micro(start.elapsed())); stats.add_roundtrip(Self::as_micro(start.elapsed()));
res res
}).boxed() });
match self.pool {
Some(ref pool) => A(pool.spawn(future)),
None => B(future.boxed()),
}
} }
} }