Skip to content

Commit

Permalink
fix: graceful shutdown (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus authored Jan 26, 2024
1 parent e7ec282 commit 66ca513
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 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 @@ -45,6 +45,7 @@ tokio = { version = "1.35", features = [
"macros",
"parking_lot",
"rt-multi-thread",
"signal",
"sync",
"time",
] }
Expand Down
30 changes: 30 additions & 0 deletions graph-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use thegraph::{
client as subgraph_client,
types::{attestation, DeploymentId},
};
use tokio::signal::unix::SignalKind;
use tokio::spawn;
use toolshed::{
buffer_queue::{self, QueueWriter},
Expand Down Expand Up @@ -408,8 +409,37 @@ async fn main() {
// disable Nagel's algorithm
.tcp_nodelay(true)
.serve(router.into_make_service_with_connect_info::<SocketAddr>())
.with_graceful_shutdown(await_shutdown_signals())
.await
.expect("Failed to start API server");
tracing::warn!("shutdown");
}

async fn await_shutdown_signals() {
#[cfg(unix)]
let sigint = async {
tokio::signal::unix::signal(SignalKind::interrupt())
.expect("install SIGINT handler")
.recv()
.await;
};
#[cfg(not(unix))]
let sigint = std::future::pending::<()>();

#[cfg(unix)]
let sigterm = async {
tokio::signal::unix::signal(SignalKind::terminate())
.expect("install SIGTERM handler")
.recv()
.await;
};
#[cfg(not(unix))]
let sigterm = std::future::pending::<()>();

tokio::select! {
_ = sigint => (),
_ = sigterm => (),
}
}

fn update_from_eventual<V, F>(eventual: Eventual<V>, writer: QueueWriter<Update>, f: F)
Expand Down

0 comments on commit 66ca513

Please sign in to comment.