Skip to content

Commit cb580f4

Browse files
committed
feat: Support shutdown with SIGINT
1 parent ab520ca commit cb580f4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

Cargo.lock

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

server/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
tokio = { version = "1.45.1", features = ["full"] }
8-
tonic = { version = "0.13.1" }
7+
elegant-departure = { version = "0.3.1", features = ["tokio"] }
98
proto-codegen = { path = "../proto-codegen" }
109
service = { path = "../service" }
10+
tokio = { version = "1.45.1", features = ["full"] }
11+
tonic = { version = "0.13.1" }

server/src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
//! an `HTTP` layer which can serve files directly to *external clients*.
66
77
use proto_codegen::storage::{AllocateBlobRequest, AllocateBlobResponse, StorageId};
8+
use tokio::signal::unix::SignalKind;
89
use tonic::{Request, Response, Status, transport::Server};
910

1011
use proto_codegen::storage::storage_server::{Storage, StorageServer};
1112

12-
#[derive(Default)]
13+
#[derive(Debug, Default)]
1314
pub struct MockStorage {}
1415

1516
#[tonic::async_trait]
@@ -37,13 +38,19 @@ impl Storage for MockStorage {
3738
#[tokio::main]
3839
async fn main() -> Result<(), Box<dyn std::error::Error>> {
3940
let addr = "0.0.0.0:50051".parse().unwrap();
40-
let greeter = MockStorage::default();
41+
let storage = MockStorage::default();
4142

4243
println!("Server listening on {addr}");
4344

45+
let shutdown = elegant_departure::tokio::depart()
46+
.on_termination()
47+
.on_sigint()
48+
.on_signal(SignalKind::hangup())
49+
.on_signal(SignalKind::quit());
50+
4451
Server::builder()
45-
.add_service(StorageServer::new(greeter))
46-
.serve(addr)
52+
.add_service(StorageServer::new(storage))
53+
.serve_with_shutdown(addr, shutdown)
4754
.await?;
4855

4956
Ok(())

0 commit comments

Comments
 (0)