Skip to content

Commit

Permalink
tonic-mt thread limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-barber committed Apr 24, 2021
1 parent d4bc0e3 commit a06f7dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions rust_tonic_mt_bench/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = [ "-C", "target-cpu=native" ]
22 changes: 19 additions & 3 deletions rust_tonic_mt_bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,24 @@ impl Greeter for MyGreeter {
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cpus = std::env::var("GRPC_SERVER_CPUS")
.map(|v| v.parse().unwrap())
.unwrap_or(1);

println!("Running with {} threads", cpus);

// Esentially the same as tokio::main, but with number of threads set to
// avoid thrashing when cggroup limits are applied by Docker.
tokio::runtime::Builder::new_multi_thread()
.worker_threads(cpus)
.enable_all()
.build()
.unwrap()
.block_on(serve())
}

async fn serve() -> Result<(), Box<dyn std::error::Error>> {
let addr = "0.0.0.0:50051".parse().unwrap();
let greeter = MyGreeter::default();

Expand All @@ -36,4 +52,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions rust_tonic_st_bench/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = [ "-C", "target-cpu=native" ]

0 comments on commit a06f7dd

Please sign in to comment.