Skip to content

Commit

Permalink
Add $APOLLO_ROUTER_COMPUTE_THREADS environment variable (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin authored Feb 11, 2025
1 parent 85976da commit 768f6d4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions apollo-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "Elastic-2.0"
rust-version = "1.83.0"
edition = "2021"
build = "build/main.rs"
default-run = "router"

[[bin]]
name = "router"
Expand Down
16 changes: 12 additions & 4 deletions apollo-router/src/compute_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ use crate::metrics::meter_provider;
/// reaches `QUEUE_SOFT_CAPACITY_PER_THREAD * thread_pool_size()`
const QUEUE_SOFT_CAPACITY_PER_THREAD: usize = 20;

/// Let this thread pool use all available resources if it can.
/// By default, let this thread pool use all available resources if it can.
/// In the worst case, we’ll have moderate context switching cost
/// as the kernel’s scheduler distributes time to it or Tokio or other threads.
fn thread_pool_size() -> usize {
std::thread::available_parallelism()
.expect("available_parallelism() failed")
.get()
// This environment variable is intentionally undocumented.
if let Some(threads) = std::env::var("APOLLO_ROUTER_COMPUTE_THREADS")
.ok()
.and_then(|value| value.parse::<usize>().ok())
{
threads
} else {
std::thread::available_parallelism()
.expect("available_parallelism() failed")
.get()
}
}

type Job = Box<dyn FnOnce() + Send + 'static>;
Expand Down
6 changes: 5 additions & 1 deletion apollo-router/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,16 @@ pub fn main() -> Result<()> {

let mut builder = tokio::runtime::Builder::new_multi_thread();
builder.enable_all();
if let Some(nb) = std::env::var("APOLLO_ROUTER_NUM_CORES")

// This environment variable is intentionally undocumented.
// See also APOLLO_ROUTER_COMPUTE_THREADS in apollo-router/src/compute_job.rs
if let Some(nb) = std::env::var("APOLLO_ROUTER_IO_THREADS")
.ok()
.and_then(|value| value.parse::<usize>().ok())
{
builder.worker_threads(nb);
}

let runtime = builder.build()?;
runtime.block_on(Executable::builder().start())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: apollo-router/src/services/layers/persisted_queries/mod.rs
expression: yaml
snapshot_kind: text
---
- fields:
operation_body: "query SomeQuery { me { id } }"
Expand Down

0 comments on commit 768f6d4

Please sign in to comment.