🐛 Turns out I had the modules all wrong. This one works good

main
_ 2020-11-05 23:11:07 -06:00
parent e9d335eec1
commit 80e8183af5
5 changed files with 29 additions and 53 deletions

View File

@ -3,42 +3,17 @@ use std::{
sync::Arc, sync::Arc,
}; };
use tokio::sync::oneshot;
mod load_toml;
use ptth::relay; use ptth::relay;
use ptth::relay::RelayState; use ptth::relay::RelayState;
#[tokio::main] #[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> { async fn main () -> Result <(), Box <dyn Error>> {
let config_file = load_toml::load ("config/ptth_relay.toml"); let config_file = ptth::load_toml::load ("config/ptth_relay.toml");
eprintln! ("ptth_relay Git version: {:?}", ptth::git_version::GIT_VERSION); eprintln! ("ptth_relay Git version: {:?}", ptth::git_version::GIT_VERSION);
let rx = {
let (tx, rx) = oneshot::channel::<()> ();
// I have to put the tx into a Cell here so that if Ctrl-C gets
// called multiple times, we won't send multiple shutdowns to the
// oneshot channel. (Which would be a compile error)
let tx = Some (tx);
let tx = std::cell::Cell::new (tx);
ctrlc::set_handler (move ||{
let tx = tx.replace (None);
if let Some (tx) = tx {
tx.send (()).unwrap ();
}
}).expect ("Error setting Ctrl-C handler");
rx
};
relay::run_relay ( relay::run_relay (
Arc::new (RelayState::from (&config_file)), Arc::new (RelayState::from (&config_file)),
rx ptth::graceful_shutdown::init ()
).await ).await
} }

View File

@ -4,9 +4,6 @@ use std::{
}; };
use structopt::StructOpt; use structopt::StructOpt;
use tokio::sync::oneshot;
mod load_toml;
#[derive (Debug, StructOpt)] #[derive (Debug, StructOpt)]
struct Opt { struct Opt {
@ -18,31 +15,10 @@ struct Opt {
async fn main () -> Result <(), Box <dyn Error>> { async fn main () -> Result <(), Box <dyn Error>> {
tracing_subscriber::fmt::init (); tracing_subscriber::fmt::init ();
let config_file = load_toml::load ("config/ptth_server.toml"); let config_file = ptth::load_toml::load ("config/ptth_server.toml");
let rx = {
let (tx, rx) = oneshot::channel::<()> ();
// I have to put the tx into a Cell here so that if Ctrl-C gets
// called multiple times, we won't send multiple shutdowns to the
// oneshot channel. (Which would be a compile error)
let tx = Some (tx);
let tx = std::cell::Cell::new (tx);
ctrlc::set_handler (move ||{
let tx = tx.replace (None);
if let Some (tx) = tx {
tx.send (()).unwrap ();
}
}).expect ("Error setting Ctrl-C handler");
rx
};
ptth::server::run_server ( ptth::server::run_server (
config_file, config_file,
rx ptth::graceful_shutdown::init ()
).await ).await
} }

23
src/graceful_shutdown.rs Normal file
View File

@ -0,0 +1,23 @@
use std::cell::Cell;
use tokio::sync::oneshot;
pub fn init () -> oneshot::Receiver <()> {
let (tx, rx) = oneshot::channel::<()> ();
// I have to put the tx into a Cell here so that if Ctrl-C gets
// called multiple times, we won't send multiple shutdowns to the
// oneshot channel. (Which would be a compile error)
let tx = Some (tx);
let tx = Cell::new (tx);
ctrlc::set_handler (move ||{
let tx = tx.replace (None);
if let Some (tx) = tx {
tx.send (()).unwrap ();
}
}).expect ("Error setting Ctrl-C handler");
rx
}

View File

@ -11,6 +11,8 @@ pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4";
// test stuff like spawn them both in the same process // test stuff like spawn them both in the same process
pub mod git_version; pub mod git_version;
pub mod graceful_shutdown;
pub mod load_toml;
pub mod relay; pub mod relay;
pub mod server; pub mod server;