Skip to content

Commit

Permalink
https support for webhooks (#2660)
Browse files Browse the repository at this point in the history
* add TLS support
* configurable nthreads and timeout
  • Loading branch information
mcdallas authored and hashmap committed Mar 12, 2019
1 parent c3496b4 commit a58d671
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2018"

[dependencies]
hyper = "0.12"
hyper-rustls = "0.14"
fs2 = "0.4"
futures = "0.1"
http = "0.1"
Expand Down
32 changes: 27 additions & 5 deletions servers/src/common/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -150,8 +153,11 @@ fn parse_url(value: &Option<String>) -> Option<hyper::Uri> {
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)
}
Expand All @@ -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<hyper::Uri>,
/// The hyper client to be used for all requests
client: Client<HttpConnector>,
client: Client<HttpsConnector<HttpConnector>>,
/// The tokio event loop
runtime: Runtime,
}
Expand All @@ -182,13 +188,27 @@ impl WebHook {
header_received_url: Option<hyper::Uri>,
block_received_url: Option<hyper::Uri>,
block_accepted_url: Option<hyper::Uri>,
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(),
}
}
Expand All @@ -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,
)
}

Expand Down
16 changes: 16 additions & 0 deletions servers/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ pub struct WebHooksConfig {
pub block_received_url: Option<String>,
/// 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<String>,
/// 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 {
Expand All @@ -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(),
}
}
}
Expand Down

0 comments on commit a58d671

Please sign in to comment.