Skip to content

Commit

Permalink
feat(rest): merge health service into main REST service
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <[email protected]>
  • Loading branch information
niladrih committed Dec 3, 2024
1 parent c52a153 commit 8776d97
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
1 change: 0 additions & 1 deletion control-plane/rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ path = "./src/lib.rs"
rustls = { version = "0.23.19", default-features = false }
rustls-pemfile = "2.2.0"
actix-web = { version = "4.9.0", features = ["rustls-0_23"] }
tokio = { version = "1.41.0", default-features = false, features = ["macros"] }
actix-service = "2.0.2"
opentelemetry = { version = "0.26.0" }
tracing-actix-web = { version = "0.7.14", features = ["opentelemetry_0_26"] }
Expand Down
51 changes: 16 additions & 35 deletions control-plane/rest/service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ use grpc::{client::CoreClient, operations::jsongrpc::client::JsonGrpcClient};
use http::Uri;
use rustls::{pki_types::PrivateKeyDer, ServerConfig};
use rustls_pemfile::{certs, rsa_private_keys};
use std::{fs::File, io::BufReader, net::SocketAddr, time::Duration};
use std::{fs::File, io::BufReader, time::Duration};
use stor_port::transport_api::{RequestMinTimeout, TimeoutOptions};
use tokio::try_join;
use utils::{
tracing_telemetry::{FmtLayer, FmtStyle, KeyValue},
DEFAULT_GRPC_CLIENT_ADDR,
Expand All @@ -40,10 +39,6 @@ pub(crate) struct CliArgs {
#[clap(long)]
http: Option<String>,

/// The bind address for the health REST server.
#[clap(long, default_value = "[::]:9091")]
health_endpoint: SocketAddr,

/// The CORE gRPC Server URL or address to connect to the services.
#[clap(long, short = 'z', default_value = DEFAULT_GRPC_CLIENT_ADDR)]
core_grpc: Uri,
Expand Down Expand Up @@ -96,10 +91,6 @@ pub(crate) struct CliArgs {
#[clap(long, short, default_value_t = num_cpus::get_physical())]
workers: usize,

/// Set the number of health service workers. Uses a minimum of 1 worker.
#[clap(long, default_value_t = 1)]
health_workers: usize,

/// Set the max number of workers to start.
/// The value 0 means the number of available physical CPUs is used.
#[clap(long, short, default_value = utils::DEFAULT_REST_MAX_WORKER_THREADS)]
Expand Down Expand Up @@ -241,8 +232,14 @@ async fn main() -> anyhow::Result<()> {
.with_tracing_tags(cli_args.tracing_tags.clone())
.init("rest-server");

let cached_core_state =
Data::new(CachedCoreState::new(cli_args.core_liveness_check_frequency).await);

let app = move || {
actix_web::App::new()
.app_data(cached_core_state.clone())
.service(liveness)
.service(readiness)
.wrap(tracing_actix_web::TracingLogger::default())
.wrap(middleware::Logger::default())
.app_data(authentication::init(get_jwk_path()))
Expand All @@ -251,7 +248,7 @@ async fn main() -> anyhow::Result<()> {

// Initialize the core client to be used in rest
CORE_CLIENT
.set(CoreClient::new(cli_args.core_grpc.clone(), timeout_opts()).await)
.set(CoreClient::new(cli_args.core_grpc, timeout_opts()).await)
.ok()
.expect("Expect to be initialised only once");

Expand All @@ -263,34 +260,18 @@ async fn main() -> anyhow::Result<()> {
.expect("Expect to be initialised only once");
}

let main_server =
let server =
HttpServer::new(app).bind_rustls_0_23(CliArgs::args().https, get_certificates()?)?;
let main_server = if let Some(http) = CliArgs::args().http {
main_server.bind(http).map_err(anyhow::Error::from)?
let result = if let Some(http) = CliArgs::args().http {
server.bind(http).map_err(anyhow::Error::from)?
} else {
main_server
server
}
.workers(workers(&CliArgs::args()));

let cached_core_state =
Data::new(CachedCoreState::new(cli_args.core_liveness_check_frequency).await);
let health_server = HttpServer::new(move || {
actix_web::App::new()
.app_data(cached_core_state.clone())
.service(liveness)
.service(readiness)
.wrap(tracing_actix_web::TracingLogger::default())
.wrap(middleware::Logger::default())
})
.bind(cli_args.health_endpoint)?
// Use a minimum of 1 worker.
.workers(cli_args.health_workers.max(1));

let result = try_join!(main_server.run(), health_server.run());
.workers(workers(&CliArgs::args()))
.run()
.await;

utils::tracing_telemetry::flush_traces();

result?;

Ok(())
result.map_err(|e| e.into())
}

0 comments on commit 8776d97

Please sign in to comment.