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
Move httpclient to be part of OffchainWorkers to optimize block import
  • Loading branch information
Scott Piriou committed Jun 21, 2020
commit d8fbf03ae1f5fdf56d6e50774d3a46603504dcd5
3 changes: 2 additions & 1 deletion client/offchain/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ 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>>,
) -> (Api<S>, Self) {
let (http_api, http_worker) = http::http();
let (http_api, http_worker) = http::http(hyper_client);

let api = Api {
db,
Expand Down
9 changes: 6 additions & 3 deletions client/offchain/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ use log::error;
use sp_core::offchain::{HttpRequestId, Timestamp, HttpRequestStatus, HttpError};
use std::{convert::TryFrom, fmt, io::Read as _, pin::Pin, task::{Context, Poll}};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender, TracingUnboundedReceiver};
use std::sync::Arc;
use hyper::{Client, Body, client};
use hyper_rustls::HttpsConnector;

/// Creates a pair of [`HttpApi`] and [`HttpWorker`].
pub fn http() -> (HttpApi, HttpWorker) {
pub fn http(hyper_client: Arc<Client<HttpsConnector<client::HttpConnector>, Body>>) -> (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 @@ -51,7 +54,7 @@ pub fn http() -> (HttpApi, HttpWorker) {
let engine = HttpWorker {
to_api,
from_api,
http_client: hyper::Client::builder().build(hyper_rustls::HttpsConnector::new()),
http_client: hyper_client,
requests: Vec::new(),
};

Expand Down Expand Up @@ -551,7 +554,7 @@ pub struct HttpWorker {
/// Used to receive messages from the `HttpApi`.
from_api: TracingUnboundedReceiver<ApiToWorker>,
/// The engine that runs HTTP requests.
http_client: hyper::Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
http_client: Arc<Client<HttpsConnector<client::HttpConnector>, Body>>,
/// HTTP requests that are being worked on by the engine.
requests: Vec<(HttpRequestId, HttpWorkerRequest)>,
}
Expand Down
6 changes: 6 additions & 0 deletions client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ 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;

Expand All @@ -55,16 +57,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>>,
}

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()));
Self {
client,
db,
_block: PhantomData,
thread_pool: Mutex::new(ThreadPool::new(num_cpus::get())),
hyper_client,
}
}
}
Expand Down Expand Up @@ -120,6 +125,7 @@ impl<Client, Storage, Block> OffchainWorkers<
self.db.clone(),
network_state.clone(),
is_validator,
self.hyper_client.clone(),
);
debug!("Spawning offchain workers at {:?}", at);
let header = header.clone();
Expand Down