Skip to content

Commit

Permalink
Merge pull request #6469 from PsiACE/refactor-httpshutdown
Browse files Browse the repository at this point in the history
refactor: migrate to common-base http shutdown
  • Loading branch information
mergify[bot] authored Jul 5, 2022
2 parents 3c4d5b8 + 893df03 commit 243159a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 134 deletions.
23 changes: 21 additions & 2 deletions common/base/src/base/http_shutdown_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ impl HttpShutdownHandler {

pub async fn start_service(
&mut self,
listening: String,
listening: SocketAddr,
tls_config: Option<RustlsConfig>,
ep: impl Endpoint + 'static,
) -> Result<SocketAddr> {
assert!(self.join_handle.is_none());
assert!(self.abort_handle.is_none());

let mut acceptor = TcpListener::bind(listening.clone())
let mut acceptor = TcpListener::bind(listening)
.into_acceptor()
.await
.map_err(|err| ErrorCode::CannotListenerPort(format!("{}:{}", err, listening)))?
Expand Down Expand Up @@ -144,4 +144,23 @@ impl HttpShutdownHandler {
}
Ok(())
}

pub async fn shutdown(&mut self, graceful: bool) {
if graceful {
if let Some(abort_handle) = self.abort_handle.take() {
let _ = abort_handle.send(());
}
if let Some(join_handle) = self.join_handle.take() {
if let Err(error) = join_handle.await {
tracing::error!(
"Unexpected error during shutdown Http Server {}. cause {}",
self.service_name,
error
);
}
}
} else if let Some(join_handle) = self.join_handle.take() {
join_handle.abort();
}
}
}
10 changes: 6 additions & 4 deletions metasrv/src/api/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::net::SocketAddr;
use std::sync::Arc;

use common_base::base::tokio::sync::broadcast;
Expand Down Expand Up @@ -94,7 +95,7 @@ impl HttpService {
Ok(cfg)
}

async fn start_with_tls(&mut self, listening: String) -> Result<()> {
async fn start_with_tls(&mut self, listening: SocketAddr) -> Result<()> {
tracing::info!("Http API TLS enabled");

let tls_config = Self::build_tls(&self.cfg.clone())?;
Expand All @@ -104,7 +105,7 @@ impl HttpService {
Ok(())
}

async fn start_without_tls(&mut self, listening: String) -> Result<()> {
async fn start_without_tls(&mut self, listening: SocketAddr) -> Result<()> {
tracing::warn!("Http API TLS not set");

self.shutdown_handler
Expand All @@ -118,9 +119,10 @@ impl HttpService {
impl Stoppable for HttpService {
async fn start(&mut self) -> Result<()> {
let conf = self.cfg.clone();
let listening = conf.admin_api_address.parse::<SocketAddr>()?;
match conf.admin_tls_server_key.is_empty() || conf.admin_tls_server_cert.is_empty() {
true => self.start_without_tls(conf.admin_api_address).await,
false => self.start_with_tls(conf.admin_api_address).await,
true => self.start_without_tls(listening).await,
false => self.start_with_tls(listening).await,
}
}

Expand Down
2 changes: 1 addition & 1 deletion query/src/api/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;

use common_base::base::HttpShutdownHandler;
use common_exception::Result;
use common_tracing::tracing;
use poem::get;
Expand All @@ -25,7 +26,6 @@ use poem::Endpoint;
use poem::EndpointExt;
use poem::Route;

use crate::common::service::HttpShutdownHandler;
use crate::servers::Server;
use crate::sessions::SessionManager;
use crate::Config;
Expand Down
1 change: 0 additions & 1 deletion query/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
pub mod context_function;
mod evaluator;
mod expression_evaluator;
pub mod service;

pub use evaluator::*;
pub use expression_evaluator::ExpressionEvaluator;
109 changes: 0 additions & 109 deletions query/src/common/service/http_shutdown_handles.rs

This file was deleted.

15 changes: 0 additions & 15 deletions query/src/common/service/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion query/src/metrics/metric_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use std::net::SocketAddr;
use std::sync::Arc;

use common_base::base::HttpShutdownHandler;
use common_exception::ErrorCode;
use common_exception::Result;
use common_metrics::PrometheusHandle;
use poem::web::Data;
use poem::EndpointExt;
use poem::IntoResponse;

use crate::common::service::HttpShutdownHandler;
use crate::servers::Server;
use crate::sessions::SessionManager;

Expand Down
2 changes: 1 addition & 1 deletion query/src/servers/http/http_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;

use common_base::base::HttpShutdownHandler;
use common_exception::Result;
use common_tracing::tracing;
use poem::get;
Expand All @@ -30,7 +31,6 @@ use poem::EndpointExt;
use poem::Route;

use super::v1::upload_to_stage;
use crate::common::service::HttpShutdownHandler;
use crate::servers::http::middleware::HTTPSessionMiddleware;
use crate::servers::http::v1::clickhouse_router;
use crate::servers::http::v1::query_route;
Expand Down

0 comments on commit 243159a

Please sign in to comment.