From 98880ab8d5809e9ee291ae22cd6e6050187d7bb2 Mon Sep 17 00:00:00 2001 From: Kirill Starkov Date: Thu, 11 Jan 2024 11:49:27 +0800 Subject: [PATCH] disable http server if port == 0 --- src/http.rs | 47 ++++++++++++++++++++++++++++++++--------------- src/main.rs | 32 +++++++++++++++++++------------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/http.rs b/src/http.rs index da0103a17..9cc078ad8 100644 --- a/src/http.rs +++ b/src/http.rs @@ -5,7 +5,8 @@ use axum::{Extension, http::{StatusCode, Uri}, response::IntoResponse}; use hyper::Server; use tokio::signal; use tokio::sync::RwLock as ARwLock; -use tracing::info; +use tokio::task::JoinHandle; +use tracing::{error, info}; use crate::global_context::GlobalContext; use crate::http::routers::make_refact_http_server; @@ -50,19 +51,35 @@ pub async fn shutdown_signal(ask_shutdown_receiver: std::sync::mpsc::Receiver>, ask_shutdown_receiver: std::sync::mpsc::Receiver, -) -> Result<(), String> { +) -> Option> { let port = global_context.read().await.cmdline.http_port; - let addr = ([127, 0, 0, 1], port).into(); - let builder = Server::try_bind(&addr).map_err(|e| { - write!(std::io::stderr(), "PORT_BUSY {}\n", e).unwrap(); - std::io::stderr().flush().unwrap(); - format!("port busy, address {}: {}", addr, e) - })?; - info!("HTTP server listening on {}", addr); - let router = make_refact_http_server().layer(Extension(global_context.clone())); - let server = builder - .serve(router.into_make_service()) - .with_graceful_shutdown(shutdown_signal(ask_shutdown_receiver)); - let resp = server.await.map_err(|e| format!("HTTP server error: {}", e)); - resp + if port == 0 { + return None + } + return Some(tokio::spawn(async move { + let addr = ([127, 0, 0, 1], port).into(); + let builder = Server::try_bind(&addr).map_err(|e| { + write!(std::io::stderr(), "PORT_BUSY {}\n", e).unwrap(); + std::io::stderr().flush().unwrap(); + format!("port busy, address {}: {}", addr, e) + }); + match builder { + Ok(builder) => { + info!("HTTP server listening on {}", addr); + let router = make_refact_http_server().layer(Extension(global_context.clone())); + let server = builder + .serve(router.into_make_service()) + .with_graceful_shutdown(shutdown_signal(ask_shutdown_receiver)); + let resp = server.await.map_err(|e| format!("HTTP server error: {}", e)); + if let Err(e) = resp { + error!("server error: {}", e); + } else { + info!("clean shutdown"); + } + } + Err(e) => { + error!("server error: {}", e); + } + } + })); } diff --git a/src/main.rs b/src/main.rs index 3b394ebe6..fc45e92e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::io::Write; +use tokio::task::JoinHandle; use tracing::{error, info, Level}; use tracing_appender; @@ -57,22 +58,27 @@ async fn main() { info!("{:>20} {}", k, v); } } - let mut background_tasks = start_background_tasks(gcx.clone()); - let lsp_task = spawn_lsp_task(gcx.clone(), cmdline.clone()).await; // execution stays inside if stdin-stdout - if lsp_task.is_some() { - background_tasks.push_back(lsp_task.unwrap()) - } - let gcx_clone = gcx.clone(); - let server = http::start_server(gcx_clone, ask_shutdown_receiver); - let server_result = server.await; - if let Err(e) = server_result { - error!("server error: {}", e); - } else { - info!("clean shutdown"); - } + let should_start_http = cmdline.http_port != 0; + let should_start_lsp = (cmdline.lsp_port == 0 && cmdline.lsp_stdin_stdout == 1) || + (cmdline.lsp_port != 0 && cmdline.lsp_stdin_stdout == 0); + let mut main_handle: Option> = None; + if should_start_http { + main_handle = http::start_server(gcx.clone(), ask_shutdown_receiver).await; + } + if should_start_lsp { + if main_handle.is_none() { + main_handle = spawn_lsp_task(gcx.clone(), cmdline.clone()).await; + } else { + background_tasks.push_back(spawn_lsp_task(gcx.clone(), cmdline.clone()).await.unwrap()) + } + } + if main_handle.is_some() { + let _ = main_handle.unwrap().await; + } + background_tasks.abort().await; info!("saving telemetry without sending, so should be quick"); basic_transmit::basic_telemetry_compress(gcx.clone()).await;