Skip to content
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **pools** Set ClickHouse `idle_timeout` to 15 seconds
- **api-helper** Box path futures for faster compile times
- Upgrade `async-nats`
- `test-mm-lobby-echo` now handles `SIGTERM` and exits immediately, allows for less resource consumption while testing lobbies

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM clux/muslrust:1.73.0 AS build
FROM clux/muslrust:1.75.0 AS build
RUN cargo new --bin /app
WORKDIR /app
COPY Cargo.toml ./
RUN cargo build --release
RUN rm -r src
COPY ./src ./src
RUN cargo build --release \
&& strip target/x86_64-unknown-linux-musl/release/test-mm-lobby-echo
RUN touch src/main.rs && \
cargo build --release && \
strip target/x86_64-unknown-linux-musl/release/test-mm-lobby-echo

FROM alpine:latest
COPY --from=build /app/target/x86_64-unknown-linux-musl/release/test-mm-lobby-echo /usr/local/bin/app
Expand Down
48 changes: 37 additions & 11 deletions infra/default-builds/dockerfiles/test-mm-lobby-echo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,42 @@ async fn main() -> Result<()> {
// Echo servers (bridge networking)
if let Ok(http_port) = env::var("PORT_test_http") {
let http_port: u16 = http_port.parse()?;
tokio::spawn(echo_http_server(http_port));
tokio::spawn(with_select_term(echo_http_server(http_port)));
}

if let Ok(tcp_port) = env::var("PORT_test_tcp") {
let tcp_port: u16 = tcp_port.parse()?;
tokio::spawn(echo_tcp_server(tcp_port));
tokio::spawn(with_select_term(echo_tcp_server(tcp_port)));
}

if let Ok(udp_port) = env::var("PORT_test_udp") {
let udp_port: u16 = udp_port.parse()?;
tokio::spawn(echo_udp_server(udp_port));
tokio::spawn(with_select_term(echo_udp_server(udp_port)));
}

// Echo servers (host networking)
if let Ok(http_port) = env::var("HOST_PORT_HTTP") {
let http_port: u16 = http_port.parse()?;
tokio::spawn(echo_http_server(http_port));
tokio::spawn(with_select_term(echo_http_server(http_port)));
}

if let Ok(tcp_port) = env::var("HOST_PORT_TCP") {
let tcp_port: u16 = tcp_port.parse()?;
tokio::spawn(echo_tcp_server(tcp_port));
tokio::spawn(with_select_term(echo_tcp_server(tcp_port)));
}

if let Ok(udp_port) = env::var("HOST_PORT_UDP") {
let udp_port: u16 = udp_port.parse()?;
tokio::spawn(echo_udp_server(udp_port));
tokio::spawn(with_select_term(echo_udp_server(udp_port)));
}

// Lobby ready
lobby_ready().await?;

// Wait indefinitely
println!("Waiting indefinitely...");
std::future::pending::<()>().await;
wait_term().await?;
println!("Ctrl+C pressed. Exiting main...");

Ok(())
}
Expand All @@ -82,7 +83,30 @@ async fn lobby_ready() -> Result<()> {
Ok(())
}

async fn echo_http_server(port: u16) {
/// Waits for the SIGTERM signal.
async fn wait_term() -> Result<()> {
use tokio::signal::unix::{signal, SignalKind};

signal(SignalKind::terminate())
.expect("Failed to set up SIGTERM handler")
.recv()
.await;

Ok(())
}

/// Waits until future exits or term.
async fn with_select_term(future: impl std::future::Future<Output = Result<()>>) -> Result<()> {
tokio::select! {
result = future => result,
_ = wait_term() => {
println!("Ctrl+C pressed. Exiting...");
Ok(())
},
}
}

async fn echo_http_server(port: u16) -> Result<()> {
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};

Expand All @@ -98,15 +122,17 @@ async fn echo_http_server(port: u16) {
.serve(make_service)
.await
.expect("hyper server");

Ok(())
}

async fn echo_tcp_server(port: u16) {
async fn echo_tcp_server(port: u16) -> Result<()> {
let addr = SocketAddr::from(([0, 0, 0, 0], port));
println!("TCP: {}", port);

let listener = TcpListener::bind(&addr).await.expect("bind failed");
let listener = TcpListener::bind(&addr).await.context("bind failed")?;
loop {
let (socket, _) = listener.accept().await.expect("accept failed");
let (socket, _) = listener.accept().await.context("accept failed")?;
tokio::spawn(async move {
let mut reader = tokio::io::BufReader::new(socket);
let mut line = String::new();
Expand Down
2 changes: 1 addition & 1 deletion infra/default-builds/outputs/test-mm-lobby-echo-tag.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test-mm-lobby-echo:1698780206
test-mm-lobby-echo:1705383901
4 changes: 2 additions & 2 deletions infra/default-builds/outputs/test-mm-lobby-echo.tar
Git LFS file not shown