Skip to content

Commit

Permalink
feat(config): Add a config value for thread counts (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-auer committed Oct 29, 2019
1 parent 6edc714 commit 67440fd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 10 additions & 0 deletions cabi/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 common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ failure = "0.1.5"
lazy_static = "1.3.0"
log = { version = "0.4.8", features = ["serde"] }
human-size = "0.4.0"
num_cpus = "1.10.1"
parking_lot = "0.9.0"
rand = "0.6.5"
regex = "1.2.0"
Expand Down
11 changes: 11 additions & 0 deletions common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ struct Limits {
max_api_file_upload_size: ByteSize,
/// The maximum payload size for chunks
max_api_chunk_upload_size: ByteSize,
/// The maximum number of threads to spawn for CPU and web work, each.
///
/// The total number of threads spawned will roughly be `2 * max_thread_count + 1`. Defaults to
/// the number of logical CPU cores on the host.
max_thread_count: usize,
}

impl Default for Limits {
Expand All @@ -263,6 +268,7 @@ impl Default for Limits {
max_api_payload_size: ByteSize::from_megabytes(20),
max_api_file_upload_size: ByteSize::from_megabytes(40),
max_api_chunk_upload_size: ByteSize::from_megabytes(100),
max_thread_count: num_cpus::get(),
}
}
}
Expand Down Expand Up @@ -802,6 +808,11 @@ impl Config {
self.values.limits.max_concurrent_requests
}

/// Returns the number of cores to use for thread pools.
pub fn cpu_concurrency(&self) -> usize {
self.values.limits.max_thread_count
}

/// Return the Sentry DSN if reporting to Sentry is enabled.
pub fn sentry_dsn(&self) -> Option<&Dsn> {
if self.values.sentry.enabled {
Expand Down
1 change: 0 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ lazy_static = "1.3.0"
listenfd = "0.3.3"
log = "0.4.8"
native-tls = { version = "0.2.3", optional = true }
num_cpus = "1.10.1"
parking_lot = "0.9.0"
r2d2 = { version = "0.8.5", optional = true }
rdkafka = { version = "0.21.0", git = "https://github.com/fede1024/rust-rdkafka", optional = true, rev = "b1a391b3264ca8e0b0e053c6aec7a6db09af3131" }
Expand Down
2 changes: 1 addition & 1 deletion server/src/actors/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl EventManager {
let rate_limiter = RateLimiter::new(&config).context(ServerErrorKind::RedisError)?;

// TODO: Make the number configurable via config file
let thread_count = num_cpus::get();
let thread_count = config.cpu_concurrency();
log::info!("starting {} event processing workers", thread_count);

#[cfg(feature = "processing")]
Expand Down
5 changes: 4 additions & 1 deletion server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ where
pub fn start(state: ServiceState) -> Result<Recipient<server::StopServer>, ServerError> {
let config = state.config();
let mut server = server::new(move || make_app(state.clone()));
server = server.shutdown_timeout(SHUTDOWN_TIMEOUT).disable_signals();
server = server
.workers(config.cpu_concurrency())
.shutdown_timeout(SHUTDOWN_TIMEOUT)
.disable_signals();

let connector = ClientConnector::default()
.limit(config.max_concurrent_requests())
Expand Down

0 comments on commit 67440fd

Please sign in to comment.