Skip to content

Commit

Permalink
update tokio graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus Karl committed Feb 4, 2024
1 parent bdf49e7 commit 3598414
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
64 changes: 34 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ anyhow = "1"
thiserror = "1"

# nat traversal
stun_codec = "0.3.2"
stun_codec = "0.3.3"
bytecodec = "0.4.15"
trackable = "1.3.0"
network-interface = "1"

# shutdown
tokio-graceful-shutdown = "0.13"
tokio-graceful-shutdown = "0.14"

# retry
backoff = {version = "0.4.0", features = [ "futures", "tokio" ] }
Expand Down
6 changes: 3 additions & 3 deletions src/client/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use network_interface::NetworkInterfaceConfig;
use rand::{rngs::OsRng, Rng};
use tokio::net::UdpSocket;
use tokio::sync::Mutex;
use tokio_graceful_shutdown::SubsystemHandle;
use tokio_graceful_shutdown::{SubsystemBuilder, SubsystemHandle};
use tracing::{debug, error, warn};
use tracing_unwrap::ResultExt;
use wirespider::protocol::{
Expand Down Expand Up @@ -152,9 +152,9 @@ pub async fn event_loop(
let monitor_interface = interface.clone();
let monitor_client = client.clone();
let monitor = monitor::Monitor::new(monitor_interface, start_opts.monitor);
subsys.start("monitor", move |subsys| {
subsys.start(SubsystemBuilder::new("monitor", move |subsys| {
monitor.monitor(subsys, &CLIENT_STATE, monitor_client)
});
}));

let overlay_address_list = address_reply
.overlay_ips
Expand Down
7 changes: 4 additions & 3 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use client_state::ClientState;
use interface::{DefaultOverlayInterface, DefaultWireguardInterface};
use peer_identifier::Identifier;
use thiserror::Error;
use tokio_graceful_shutdown::Toplevel;
use tokio_graceful_shutdown::{SubsystemBuilder, Toplevel};
use tonic::codegen::InterceptedService;
use tonic::metadata::Ascii;
use tonic::service::Interceptor;
Expand Down Expand Up @@ -93,9 +93,10 @@ fn set_loglevel(opt: &BaseOptions) -> Result<(), tracing::dispatcher::SetGlobalD

pub async fn client_start(start_opts: ClientStartCommand) -> anyhow::Result<()> {
set_loglevel(&start_opts.base)?;
Toplevel::new()
Toplevel::new(|s| async move {
s.start(SubsystemBuilder::new("Eventloop", |subsys| event_loop(subsys, start_opts)));
})
.catch_signals()
.start("Eventloop", |subsys| event_loop(subsys, start_opts))
.handle_shutdown_requests(Duration::from_millis(1000))
.await?;
Ok(())
Expand Down
10 changes: 5 additions & 5 deletions src/server/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::cli::{
use crate::server::protocol::WirespiderServerState;

use anyhow::Context;
use tokio_graceful_shutdown::SubsystemHandle;
use tokio_graceful_shutdown::{SubsystemBuilder, SubsystemHandle};
use tokio_graceful_shutdown::Toplevel;
use tracing::metadata::LevelFilter;
use tracing_error::ErrorLayer;
Expand Down Expand Up @@ -49,14 +49,14 @@ pub async fn server_run(opt: ServerRunCommand) -> anyhow::Result<()> {
env::set_var("DATABASE_URL", &opt.base.db.database_url);
debug!("Starting");

Toplevel::new()
.start("TonicService", move |handle| {
tonic_service(handle, opt.bind)
Toplevel::new(move |s| async move {
s.start(SubsystemBuilder::new("TonicService", move |handle| {
tonic_service(handle, opt.bind)
}));
})
.catch_signals()
.handle_shutdown_requests(Duration::from_millis(1000))
.await?;

Ok(())
}

Expand Down

0 comments on commit 3598414

Please sign in to comment.