From 6d3aba024982141ffc78c334d05c36e0531167c6 Mon Sep 17 00:00:00 2001 From: Keyv Chan Date: Thu, 4 Aug 2022 17:53:28 +0800 Subject: [PATCH] feat: restart server when connection closed unexpectlly --- crates/server/src/lib.rs | 3 ++- crates/server/src/main_loop.rs | 2 +- crates/tsls/src/main.rs | 24 +++++++++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index 4cb0083..c5170fd 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -17,7 +17,8 @@ pub fn server_mode() -> Result<(), Box> { // Create the transport. Includes the stdio (stdin and stdout) versions but this could // also be implemented to use sockets or HTTP. - let (connection, io_threads) = Connection::stdio(); + // let (connection, io_threads) = Connection::stdio(); + let (connection, io_threads) = Connection::listen("localhost:12345")?; let (connection_id, initialization_params) = connection.initialize_start()?; diff --git a/crates/server/src/main_loop.rs b/crates/server/src/main_loop.rs index e97ddd7..5733081 100644 --- a/crates/server/src/main_loop.rs +++ b/crates/server/src/main_loop.rs @@ -34,5 +34,5 @@ pub fn main_loop( } } } - Ok(()) + Err("main loop ended".into()) } diff --git a/crates/tsls/src/main.rs b/crates/tsls/src/main.rs index f569fa5..d6740c4 100644 --- a/crates/tsls/src/main.rs +++ b/crates/tsls/src/main.rs @@ -23,7 +23,7 @@ fn main() -> Result<(), Box> { // initial the logger // flexi_logger::Logger::try_with_env_or_str("debug")?.start()?; let _logger = Logger::try_with_str("debug")? - .log_to_file(FileSpec::default()) + // .log_to_file(FileSpec::default()) .write_mode(WriteMode::BufferAndFlush) .start()?; // .start() @@ -37,12 +37,22 @@ fn main() -> Result<(), Box> { cli::setup(args.setup); } - match args.mode.as_str() { - "server" => { - server_mode().unwrap(); - Ok(()) + loop { + match args.mode.as_str() { + "server" => { + match server_mode() { + Ok(()) => { + warn!("shutting down gracefully"); + return Ok(()); + } + Err(e) => { + // warn and restart server + warn!("{}, restart server", e); + } + }; + } + "headless" => unimplemented!("headless mode"), + _ => unimplemented!("unknown mode"), } - "headless" => unimplemented!("headless mode"), - _ => unimplemented!("unknown mode"), } }