diff --git a/Cargo.lock b/Cargo.lock index 581020ab1..a3a89f370 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -888,6 +888,7 @@ dependencies = [ "grin_util 1.1.0", "http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.24 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper-rustls 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "lmdb-zero 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/servers/Cargo.toml b/servers/Cargo.toml index 446734913..109145fc0 100644 --- a/servers/Cargo.toml +++ b/servers/Cargo.toml @@ -11,6 +11,7 @@ edition = "2018" [dependencies] hyper = "0.12" +hyper-rustls = "0.14" fs2 = "0.4" futures = "0.1" http = "0.1" diff --git a/servers/src/common/hooks.rs b/servers/src/common/hooks.rs index cf7ced9d2..00cb7c493 100644 --- a/servers/src/common/hooks.rs +++ b/servers/src/common/hooks.rs @@ -16,20 +16,23 @@ //! callback simply implement the coresponding trait and add it to the init function extern crate hyper; +extern crate hyper_rustls; extern crate tokio; use crate::chain::BlockStatus; use crate::common::types::{ServerConfig, WebHooksConfig}; use crate::core::core; use crate::core::core::hash::Hashed; +use crate::p2p::types::PeerAddr; use futures::future::Future; use hyper::client::HttpConnector; use hyper::header::HeaderValue; use hyper::Client; use hyper::{Body, Method, Request}; +use hyper_rustls::HttpsConnector; use serde::Serialize; use serde_json::{json, to_string}; -use crate::p2p::types::PeerAddr; +use std::time::Duration; use tokio::runtime::Runtime; /// Returns the list of event hooks that will be initialized for network events @@ -150,8 +153,11 @@ fn parse_url(value: &Option) -> Option { Err(_) => panic!("Invalid url : {}", url), }; let scheme = uri.scheme_part().map(|s| s.as_str()); - if scheme != Some("http") { - panic!("Invalid url scheme {}, expected 'http'", url) + if (scheme != Some("http")) && (scheme != Some("https")) { + panic!( + "Invalid url scheme {}, expected one of ['http', https']", + url + ) }; Some(uri) } @@ -170,7 +176,7 @@ struct WebHook { /// url to POST block data when a new block is accepted by our node (might be a reorg or a fork) block_accepted_url: Option, /// The hyper client to be used for all requests - client: Client, + client: Client>, /// The tokio event loop runtime: Runtime, } @@ -182,13 +188,27 @@ impl WebHook { header_received_url: Option, block_received_url: Option, block_accepted_url: Option, + nthreads: u16, + timeout: u16, ) -> WebHook { + let keep_alive = Duration::from_secs(timeout as u64); + + info!( + "Spawning {} threads for webhooks (timeout set to {} secs)", + nthreads, timeout + ); + + let https = HttpsConnector::new(nthreads as usize); + let client = Client::builder() + .keep_alive_timeout(keep_alive) + .build::<_, hyper::Body>(https); + WebHook { tx_received_url, block_received_url, header_received_url, block_accepted_url, - client: Client::new(), + client, runtime: Runtime::new().unwrap(), } } @@ -200,6 +220,8 @@ impl WebHook { parse_url(&config.header_received_url), parse_url(&config.block_received_url), parse_url(&config.block_accepted_url), + config.nthreads, + config.timeout, ) } diff --git a/servers/src/common/types.rs b/servers/src/common/types.rs index 6b9ed73cb..ee3d19971 100644 --- a/servers/src/common/types.rs +++ b/servers/src/common/types.rs @@ -248,6 +248,20 @@ pub struct WebHooksConfig { pub block_received_url: Option, /// url to POST block data when a new block is accepted by our node (might be a reorg or a fork) pub block_accepted_url: Option, + /// number of worker threads in the tokio runtime + #[serde(default = "default_nthreads")] + pub nthreads: u16, + /// timeout in seconds for the http request + #[serde(default = "default_timeout")] + pub timeout: u16, +} + +fn default_timeout() -> u16 { + 10 +} + +fn default_nthreads() -> u16 { + 4 } impl Default for WebHooksConfig { @@ -257,6 +271,8 @@ impl Default for WebHooksConfig { header_received_url: None, block_received_url: None, block_accepted_url: None, + nthreads: default_nthreads(), + timeout: default_timeout(), } } }