-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
165 additions
and
129 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use clap::Parser; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct CtlArgs { | ||
/// This controller node's HTTP server port. | ||
/// | ||
/// If not provided, a random port will be chosen. | ||
#[arg(long)] | ||
pub http_port: Option<u16>, | ||
|
||
/// This controller node's Balancer server port. | ||
/// | ||
/// If not provided, a random port will be chosen. | ||
#[arg(long)] | ||
pub balancer_port: Option<u16>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
use axum::Json; | ||
use proto::{common::node::Metrics, ctl::worker::PushWorkerMetricsRes}; | ||
use proto::ctl::worker::{PushWorkerMetricsReq, PushWorkerMetricsRes}; | ||
use tracing::info; | ||
|
||
pub async fn push_metrics(Json(payload): Json<Metrics>) -> Json<PushWorkerMetricsRes> { | ||
pub async fn push_metrics(Json(payload): Json<PushWorkerMetricsReq>) -> Json<PushWorkerMetricsRes> { | ||
info!("{payload:#?}"); | ||
todo!() | ||
Json(PushWorkerMetricsRes {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
use std::{ | ||
net::{IpAddr, Ipv4Addr}, | ||
sync::Arc, | ||
}; | ||
|
||
use clap::Parser; | ||
use tokio::task::JoinSet; | ||
use tracing::info; | ||
use utils::server::mk_listener; | ||
|
||
use crate::{discovery::Discovery, http::HttpState}; | ||
use crate::{args::CtlArgs, discovery::Discovery, http::HttpState}; | ||
|
||
mod args; | ||
mod discovery; | ||
mod http; | ||
|
||
const ANY_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)); | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
async fn main() -> eyre::Result<()> { | ||
utils::setup::tracing(); | ||
info!("started controller"); | ||
|
||
let (discovery, discovery_handle) = Discovery::new(); | ||
let args = Arc::new(CtlArgs::parse()); | ||
info!(?args, "started ctl"); | ||
|
||
let (_balancer_listener, _balancer_port) = mk_listener(ANY_IP, args.balancer_port).await?; | ||
let (http_listener, http_port) = mk_listener(ANY_IP, args.http_port).await?; | ||
|
||
let discovery_actor_handle = tokio::spawn(async move { | ||
let mut bag = JoinSet::new(); | ||
|
||
let (discovery, discovery_handle) = Discovery::new(); | ||
bag.spawn(async move { | ||
discovery.run().await; | ||
}); | ||
|
||
let http_handle = tokio::spawn({ | ||
bag.spawn(async move { | ||
let state = HttpState { | ||
discovery: discovery_handle.clone(), | ||
}; | ||
async move { | ||
http::run_server(state).await; | ||
} | ||
let app = http::mk_app(state); | ||
info!("ctl http listening at {ANY_IP}:{http_port}"); | ||
axum::serve(http_listener, app).await.unwrap(); | ||
}); | ||
|
||
discovery_actor_handle.await.unwrap(); | ||
http_handle.await.unwrap(); | ||
while let Some(res) = bag.join_next().await { | ||
res?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
use std::{convert::Infallible, future::IntoFuture, io}; | ||
use std::net::IpAddr; | ||
|
||
use axum::{ | ||
extract::Request, | ||
response::Response, | ||
serve::{IncomingStream, Serve}, | ||
}; | ||
use tokio::net::{TcpListener, ToSocketAddrs}; | ||
use tower::Service; | ||
use tracing::info; | ||
use eyre::Context; | ||
use tokio::net::TcpListener; | ||
|
||
pub async fn listen<A, M, S>(name: &'static str, mk_svc: M, addr: A) | ||
where | ||
A: ToSocketAddrs, | ||
M: for<'a> Service<IncomingStream<'a>, Error = Infallible, Response = S>, | ||
S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + 'static, | ||
S::Future: Send, | ||
Serve<M, S>: IntoFuture<Output = io::Result<()>>, | ||
{ | ||
let listener = TcpListener::bind(addr).await.unwrap(); | ||
let addr = listener.local_addr().unwrap(); | ||
info!("{name} listening at {addr}"); | ||
axum::serve(listener, mk_svc).await.unwrap(); | ||
/// Creates a new TCP listener. | ||
/// | ||
/// Tries to use the provided port, if any. If the provided port is already in | ||
/// use, this method will return an error. | ||
/// | ||
/// If no port is provided, a random one will be chosen by the OS. | ||
pub async fn mk_listener( | ||
addr: impl Into<IpAddr>, | ||
port: Option<u16>, | ||
) -> eyre::Result<(TcpListener, u16)> { | ||
let addr = addr.into(); | ||
let port = port.unwrap_or(0); | ||
|
||
let listener = TcpListener::bind((addr, port)) | ||
.await | ||
.wrap_err("failed to start tcp listener")?; | ||
|
||
let local_addr = listener.local_addr().expect("local addr must exist"); | ||
let port = local_addr.port(); | ||
|
||
Ok((listener, port)) | ||
} |
Oops, something went wrong.