Skip to content

Commit

Permalink
Use lazy_static share SharedClient amongst OffchainWorkers. Remove th…
Browse files Browse the repository at this point in the history
…e need to raise the fd limit
  • Loading branch information
Scott Piriou committed Jun 22, 2020
1 parent cf434c8 commit 7af9749
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 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 client/offchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sp-runtime = { version = "2.0.0-rc3", path = "../../primitives/runtime" }
sp-utils = { version = "2.0.0-rc3", path = "../../primitives/utils" }
sc-network = { version = "0.8.0-rc3", path = "../network" }
sc-keystore = { version = "2.0.0-rc3", path = "../keystore" }
lazy_static = "1.4.0"

[target.'cfg(not(target_os = "unknown"))'.dependencies]
hyper = "0.13.2"
Expand Down
6 changes: 0 additions & 6 deletions client/offchain/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,6 @@ mod tests {
// server that runs in the background as well.
macro_rules! build_api_server {
() => {{
// We spawn quite a bit of HTTP servers here due to how async API
// works for offchain workers, so be sure to raise the FD limit
// (particularly useful for macOS where the default soft limit may
// not be enough).
fdlimit::raise_fd_limit();

let hyper_client = Arc::new(HyperClient::builder().build(HttpsConnector::new()));
let (api, worker) = http(hyper_client.clone());

Expand Down
8 changes: 7 additions & 1 deletion client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ 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 lazy_static::lazy_static;

mod api;
use api::SharedClient;

pub use sp_offchain::{OffchainWorkerApi, STORAGE_PREFIX};

lazy_static! {
// Using lazy_static to have the `SharedClient` shared amongst the different OffchainWorkers.
static ref SHARED_CLIENT: SharedClient = SharedClient::new();
}

/// An offchain workers manager.
pub struct OffchainWorkers<Client, Storage, Block: traits::Block> {
client: Arc<Client>,
Expand All @@ -62,7 +68,7 @@ pub struct OffchainWorkers<Client, Storage, Block: traits::Block> {
impl<Client, Storage, Block: traits::Block> OffchainWorkers<Client, Storage, Block> {
/// Creates new `OffchainWorkers`.
pub fn new(client: Arc<Client>, db: Storage) -> Self {
let shared_client = SharedClient::new();
let shared_client = SHARED_CLIENT.clone();
Self {
client,
db,
Expand Down

0 comments on commit 7af9749

Please sign in to comment.