Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add wrapper struct for HyperClient
  • Loading branch information
Scott Piriou committed Jun 22, 2020
commit cf434c811262004efbf91c1510b12021e70071f7
3 changes: 2 additions & 1 deletion client/offchain/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use sp_core::offchain::{
OpaqueNetworkState, OpaquePeerId, OpaqueMultiaddr, StorageKind,
};
pub use sp_offchain::STORAGE_PREFIX;
pub use http::SharedClient;

#[cfg(not(target_os = "unknown"))]
mod http;
Expand Down Expand Up @@ -260,7 +261,7 @@ impl AsyncApi {
db: S,
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
is_validator: bool,
hyper_client: Arc<hyper::Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>>,
hyper_client: SharedClient,
) -> (Api<S>, Self) {
let (http_api, http_worker) = http::http(hyper_client);

Expand Down
14 changes: 12 additions & 2 deletions client/offchain/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ use std::sync::Arc;
use hyper::{Client as HyperClient, Body, client};
use hyper_rustls::HttpsConnector;

/// Wrapper struct used for keeping the hyper_rustls client running.
#[derive(Clone)]
pub struct SharedClient(Arc<HyperClient<HttpsConnector<client::HttpConnector>, Body>>);

impl SharedClient {
pub fn new() -> Self {
Self(Arc::new(HyperClient::builder().build(HttpsConnector::new())))
}
}

/// Creates a pair of [`HttpApi`] and [`HttpWorker`].
pub fn http(hyper_client: Arc<HyperClient<HttpsConnector<client::HttpConnector>, Body>>) -> (HttpApi, HttpWorker) {
pub fn http(shared_client: SharedClient) -> (HttpApi, HttpWorker) {
let (to_worker, from_api) = tracing_unbounded("mpsc_ocw_to_worker");
let (to_api, from_worker) = tracing_unbounded("mpsc_ocw_to_api");

Expand All @@ -54,7 +64,7 @@ pub fn http(hyper_client: Arc<HyperClient<HttpsConnector<client::HttpConnector>,
let engine = HttpWorker {
to_api,
from_api,
http_client: hyper_client,
http_client: shared_client.0,
requests: Vec::new(),
};

Expand Down
12 changes: 11 additions & 1 deletion client/offchain/src/api/http_dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@
use sp_core::offchain::{HttpRequestId, Timestamp, HttpRequestStatus, HttpError};
use std::{future::Future, pin::Pin, task::Context, task::Poll};

/// Wrapper struct (wrapping nothing in case of http_dummy) used for keeping the hyper_rustls client running.
#[derive(Clone)]
pub struct SharedClient;

impl SharedClient {
pub fn new() -> Self {
Self
}
}

/// Creates a pair of [`HttpApi`] and [`HttpWorker`].
pub fn http() -> (HttpApi, HttpWorker) {
pub fn http(_: SharedClient) -> (HttpApi, HttpWorker) {
(HttpApi, HttpWorker)
}

Expand Down
11 changes: 5 additions & 6 deletions client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ use sc_network::NetworkStateInfo;
use sp_core::{offchain::{self, OffchainStorage}, ExecutionContext, traits::SpawnNamed};
use sp_runtime::{generic::BlockId, traits::{self, Header}};
use futures::{prelude::*, future::ready};
use hyper_rustls::HttpsConnector;
use hyper::{Client as HyperClient, Body, client};

mod api;
use api::SharedClient;

pub use sp_offchain::{OffchainWorkerApi, STORAGE_PREFIX};

Expand All @@ -57,19 +56,19 @@ pub struct OffchainWorkers<Client, Storage, Block: traits::Block> {
db: Storage,
_block: PhantomData<Block>,
thread_pool: Mutex<ThreadPool>,
hyper_client: Arc<HyperClient<HttpsConnector<client::HttpConnector>, Body>>,
shared_client: SharedClient,
}

impl<Client, Storage, Block: traits::Block> OffchainWorkers<Client, Storage, Block> {
/// Creates new `OffchainWorkers`.
pub fn new(client: Arc<Client>, db: Storage) -> Self {
let hyper_client = Arc::new(HyperClient::builder().build(HttpsConnector::new()));
let shared_client = SharedClient::new();
Self {
client,
db,
_block: PhantomData,
thread_pool: Mutex::new(ThreadPool::new(num_cpus::get())),
hyper_client,
shared_client,
}
}
}
Expand Down Expand Up @@ -125,7 +124,7 @@ impl<Client, Storage, Block> OffchainWorkers<
self.db.clone(),
network_state.clone(),
is_validator,
self.hyper_client.clone(),
self.shared_client.clone(),
);
debug!("Spawning offchain workers at {:?}", at);
let header = header.clone();
Expand Down