Skip to content

Commit

Permalink
Merge pull request #24 from ckb-cell/feature/increase-fd-soft-limit
Browse files Browse the repository at this point in the history
feat: try increasing file descriptor soft limit for service at start
  • Loading branch information
yangby-cryptape authored Apr 11, 2024
2 parents 5f9bbc8 + dc062a1 commit b17edc5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1 deletion.
11 changes: 11 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ zeroize = { version = "1.7", features = ["derive"] }
url = "2.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
fdlimit = "0.3"

reqwest = { version = "0.11", default-features = false, features = ["json", "blocking"] }
jsonrpc-core = "18.0"
Expand Down
3 changes: 3 additions & 0 deletions src/cli/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
},
prelude::*,
result::{Error, Result},
utilities::try_raise_fd_limit,
};

#[derive(Parser)]
Expand Down Expand Up @@ -83,6 +84,8 @@ impl Args {
pub fn execute(&self) -> Result<()> {
log::info!("Starting the Bitcoin SPV service");

try_raise_fd_limit();

let storage = Storage::new(&self.data_dir)?;
if !storage.is_initialized()? {
let msg = format!(
Expand Down
3 changes: 3 additions & 0 deletions src/cli/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
components::{ApiServiceConfig, SpvService, Storage},
prelude::*,
result::{Error, Result},
utilities::try_raise_fd_limit,
};

#[derive(Parser)]
Expand Down Expand Up @@ -45,6 +46,8 @@ impl Args {
pub fn execute(&self) -> Result<()> {
log::info!("Starting the Bitcoin SPV service (readonly)");

try_raise_fd_limit();

let storage = Storage::new(&self.data_dir)?;
if !storage.is_initialized()? {
let msg = format!(
Expand Down
1 change: 0 additions & 1 deletion src/components/api_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ impl SpvRpc for SpvRpcImpl {
})?;
log::trace!(">>> tip height in local storage is {stg_tip_height}");

// TODO Define server errors with enum.
if stg_tip_height < target_height {
let desc = format!(
"target transaction is in header#{target_height}, \
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Utilities.
mod key;
mod platform;
mod type_id;
pub(crate) mod value_parsers;

pub(crate) use key::Key256Bits;
pub(crate) use platform::try_raise_fd_limit;
pub(crate) use type_id::calculate_type_id;
17 changes: 17 additions & 0 deletions src/utilities/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Enhance the platform environment for continuously running services.
use fdlimit::{raise_fd_limit, Outcome};

pub fn try_raise_fd_limit() {
match raise_fd_limit() {
Ok(Outcome::LimitRaised { from, to }) => {
log::info!("raise file descriptor resource limit from {from} to {to}");
}
Ok(Outcome::Unsupported) => {
log::warn!("raising limit is not supported on this platform");
}
Err(err) => {
log::error!("failed to raise file descriptor resource limit, since {err}");
}
}
}

0 comments on commit b17edc5

Please sign in to comment.