Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move locality arguments to top-level #1064

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/test/tests/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use qt::*;
use quilkin::{components::proxy, net, test::TestConfig};
use tracing::Instrument as _;

trace_test!(server, {
let mut sc = qt::sandbox_config!();
Expand Down
39 changes: 37 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ pub struct AdminCli {
pub address: Option<std::net::SocketAddr>,
}

#[derive(Debug, clap::Parser)]
#[command(next_help_heading = "Locality Options")]
pub struct LocalityCli {
/// The `region` to set in the cluster map for any provider
/// endpoints discovered.
#[clap(long = "locality.region", env = "QUILKIN_LOCALITY_REGION")]
pub region: Option<String>,
/// The `zone` in the `region` to set in the cluster map for any provider
/// endpoints discovered.
#[clap(
long = "locality.region.zone",
requires("region"),
env = "QUILKIN_LOCALITY_ZONE"
)]
pub zone: Option<String>,
/// The `sub_zone` in the `zone` in the `region` to set in the cluster map
/// for any provider endpoints discovered.
#[clap(
long = "locality.region.sub_zone",
requires("zone"),
env = "QUILKIN_LOCALITY_SUB_ZONE"
)]
pub sub_zone: Option<String>,
}

/// Quilkin: a non-transparent UDP proxy specifically designed for use with
/// large scale multiplayer dedicated game servers deployments, to
/// ensure security, access control, telemetry data, metrics and more.
Expand All @@ -94,6 +119,8 @@ pub struct Cli {
pub log_format: LogFormats,
#[command(flatten)]
pub admin: AdminCli,
#[command(flatten)]
pub locality: LocalityCli,
}

/// The various log format options
Expand Down Expand Up @@ -231,6 +258,14 @@ impl Cli {
mode.server(config.clone(), self.admin.address);
}

let locality = self.locality.region.map(|region| {
crate::net::endpoint::Locality::new(
region,
self.locality.zone.unwrap_or_default(),
self.locality.sub_zone.unwrap_or_default(),
)
});

let (shutdown_tx, shutdown_rx) = crate::make_shutdown_channel(Default::default());
crate::alloc::spawn_heap_stats_updates(
std::time::Duration::from_secs(10),
Expand Down Expand Up @@ -261,13 +296,13 @@ impl Cli {

match (self.command, mode) {
(Commands::Agent(agent), Admin::Agent(ready)) => {
agent.run(config, ready, shutdown_rx).await
agent.run(locality, config, ready, shutdown_rx).await
}
(Commands::Proxy(runner), Admin::Proxy(ready)) => {
runner.run(config, ready, tx, shutdown_rx).await
}
(Commands::Manage(manager), Admin::Manage(ready)) => {
manager.run(config, ready, shutdown_rx).await
manager.run(locality, config, ready, shutdown_rx).await
}
(Commands::Relay(relay), Admin::Relay(ready)) => {
relay.run(config, ready, shutdown_rx).await
Expand Down
24 changes: 1 addition & 23 deletions src/cli/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ pub struct Agent {
/// One or more `quilkin relay` endpoints to push configuration changes to.
#[clap(short, long, env = "QUILKIN_MANAGEMENT_SERVER")]
pub relay: Vec<tonic::transport::Endpoint>,
/// The `region` to set in the cluster map for any provider
/// endpoints discovered.
#[clap(long, env = "QUILKIN_REGION")]
pub region: Option<String>,
/// The `zone` in the `region` to set in the cluster map for any provider
/// endpoints discovered.
#[clap(long, env = "QUILKIN_ZONE")]
pub zone: Option<String>,
/// The `sub_zone` in the `zone` in the `region` to set in the cluster map
/// for any provider endpoints discovered.
#[clap(long, env = "QUILKIN_SUB_ZONE")]
pub sub_zone: Option<String>,
/// The configuration source for a management server.
#[clap(subcommand)]
pub provider: Option<crate::config::Providers>,
Expand Down Expand Up @@ -80,9 +68,6 @@ impl Default for Agent {
Self {
qcmp_port: PORT,
relay: <_>::default(),
region: <_>::default(),
zone: <_>::default(),
sub_zone: <_>::default(),
provider: <_>::default(),
icao_code: <_>::default(),
address_type: None,
Expand All @@ -95,18 +80,11 @@ impl Agent {
#[tracing::instrument(skip_all)]
pub async fn run(
self,
locality: Option<crate::net::endpoint::Locality>,
config: Arc<Config>,
ready: Ready,
shutdown_rx: crate::ShutdownRx,
) -> crate::Result<()> {
let locality = self.region.map(|region| {
crate::net::endpoint::Locality::new(
region,
self.zone.unwrap_or_default(),
self.sub_zone.unwrap_or_default(),
)
});

let qcmp_socket = crate::net::raw_socket_with_reuse(self.qcmp_port)?;
let icao_code = Some(self.icao_code);

Expand Down
21 changes: 1 addition & 20 deletions src/cli/manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ pub struct Manage {
/// The TCP port to listen to, to serve discovery responses.
#[clap(short, long, env = super::PORT_ENV_VAR, default_value_t = PORT)]
pub port: u16,
/// The `region` to set in the cluster map for any provider
/// endpoints discovered.
#[clap(long, env = "QUILKIN_REGION")]
pub region: Option<String>,
/// The `zone` in the `region` to set in the cluster map for any provider
/// endpoints discovered.
#[clap(long, env = "QUILKIN_ZONE")]
pub zone: Option<String>,
/// The `sub_zone` in the `zone` in the `region` to set in the cluster map
/// for any provider endpoints discovered.
#[clap(long, env = "QUILKIN_SUB_ZONE")]
pub sub_zone: Option<String>,
/// The configuration source for a management server.
#[clap(subcommand)]
pub provider: crate::config::Providers,
Expand All @@ -57,18 +45,11 @@ impl Manage {
#[tracing::instrument(skip_all)]
pub async fn run(
self,
locality: Option<crate::net::endpoint::Locality>,
config: std::sync::Arc<crate::Config>,
ready: Ready,
shutdown_rx: crate::ShutdownRx,
) -> crate::Result<()> {
let locality = self.region.map(|region| {
crate::net::endpoint::Locality::new(
region,
self.zone.unwrap_or_default(),
self.sub_zone.unwrap_or_default(),
)
});

let listener = crate::net::TcpListener::bind(Some(self.port))?;

manage::Manage {
Expand Down
2 changes: 1 addition & 1 deletion tests/qcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn agent_ping() {
let (_tx, rx) = quilkin::make_shutdown_channel(quilkin::ShutdownKind::Testing);
tokio::spawn(async move {
agent
.run(server_config, Default::default(), rx)
.run(None, server_config, Default::default(), rx)
.await
.expect("Agent should run")
});
Expand Down
Loading