Skip to content

Commit ea08408

Browse files
committed
add graceful shutdown support for lcp service
Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
1 parent 7b4c218 commit ea08408

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/commands/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl ServiceCmd {
6161
let srv = AppService::new(opts.get_home(), enclave);
6262

6363
info!("start service: addr={addr} mrenclave={mrenclave}");
64-
run_service(srv, rt, addr)
64+
rt.block_on(async { run_service(srv, addr).await })
6565
}
6666
}
6767
}

modules/service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tonic = { version = "0.9", default-features = false }
88
tonic-reflection = { version = "0.9" }
99
tokio = { version = "1.0", features = ["full"] }
1010
anyhow = { version = "1.0.56" }
11+
log = { version = "0.4.8" }
1112

1213
lcp-types = { path = "../types" }
1314
crypto = { path = "../crypto" }

modules/service/src/service.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use lcp_proto::lcp::service::{
44
elc::v1::{msg_server::MsgServer as ELCMsgServer, query_server::QueryServer as ELCQueryServer},
55
enclave::v1::query_server::QueryServer as EnclaveQueryServer,
66
};
7+
use log::*;
78
use std::{marker::PhantomData, net::SocketAddr, path::PathBuf, sync::Arc};
89
use store::transaction::CommitStore;
9-
use tokio::runtime::Runtime;
10+
use tokio::signal::unix::{signal, SignalKind};
1011
use tonic::transport::Server;
1112

1213
pub struct AppService<E, S>
@@ -47,7 +48,7 @@ where
4748
}
4849
}
4950

50-
pub fn run_service<E, S>(srv: AppService<E, S>, rt: Arc<Runtime>, addr: SocketAddr) -> Result<()>
51+
pub async fn run_service<E, S>(srv: AppService<E, S>, addr: SocketAddr) -> Result<()>
5152
where
5253
S: CommitStore,
5354
E: EnclaveProtoAPI<S>,
@@ -59,15 +60,23 @@ where
5960
.register_encoded_file_descriptor_set(lcp_proto::FILE_DESCRIPTOR_SET)
6061
.build()
6162
.expect("failed to create gRPC reflection servicer");
62-
rt.block_on(async {
63-
Server::builder()
64-
.add_service(elc_msg_srv)
65-
.add_service(elc_query_srv)
66-
.add_service(enclave_srv)
67-
.add_service(reflection)
68-
.serve(addr)
69-
.await
70-
.unwrap();
71-
});
63+
64+
let mut sigint = signal(SignalKind::interrupt()).expect("failed to set SIGINT handler");
65+
let mut sigterm = signal(SignalKind::terminate()).expect("failed to set SIGTERM handler");
66+
let shutdown_signal = async {
67+
let signal_type = tokio::select! {
68+
_ = sigint.recv() => "SIGINT",
69+
_ = sigterm.recv() => "SIGTERM",
70+
};
71+
info!("shutdown signal ({}) received, stopping server", signal_type);
72+
};
73+
Server::builder()
74+
.add_service(elc_msg_srv)
75+
.add_service(elc_query_srv)
76+
.add_service(enclave_srv)
77+
.add_service(reflection)
78+
.serve_with_shutdown(addr, shutdown_signal)
79+
.await?;
80+
info!("server stopped");
7281
Ok(())
7382
}

0 commit comments

Comments
 (0)