fix: try not to silently die

This commit is contained in:
Skye 2023-11-09 21:19:22 +09:00
parent 1ade38b4f4
commit af59fe1f82
Signed by: me
GPG key ID: 0104BC05F41B77B8

View file

@ -1,6 +1,6 @@
use std::{net::SocketAddr, sync::Arc, time::Duration}; use std::{net::SocketAddr, sync::Arc, time::Duration, convert::Infallible};
use anyhow::Context; use anyhow::{Context, anyhow};
use axum::{ use axum::{
http::StatusCode, http::StatusCode,
routing::{get, post}, routing::{get, post},
@ -91,6 +91,7 @@ async fn main() -> anyhow::Result<()> {
let routing_table = Box::leak(Box::new(routing::RoutingTable::new( let routing_table = Box::leak(Box::new(routing::RoutingTable::new(
std::env::var("QUICLIME_BASE_DOMAIN").context("Reading QUICLIME_BASE_DOMAIN")?, std::env::var("QUICLIME_BASE_DOMAIN").context("Reading QUICLIME_BASE_DOMAIN")?,
))); )));
#[allow(unreachable_code)]
tokio::try_join!( tokio::try_join!(
listen_quic(endpoint, routing_table), listen_quic(endpoint, routing_table),
listen_control(endpoint, routing_table), listen_control(endpoint, routing_table),
@ -183,17 +184,17 @@ async fn handle_quic(connection: Connecting, routing_table: &RoutingTable) {
async fn listen_quic( async fn listen_quic(
endpoint: &'static Endpoint, endpoint: &'static Endpoint,
routing_table: &'static RoutingTable, routing_table: &'static RoutingTable,
) -> anyhow::Result<()> { ) -> anyhow::Result<Infallible> {
while let Some(connection) = endpoint.accept().await { while let Some(connection) = endpoint.accept().await {
tokio::spawn(handle_quic(connection, routing_table)); tokio::spawn(handle_quic(connection, routing_table));
} }
Ok(()) Err(anyhow!("quiclime endpoint closed"))
} }
async fn listen_control( async fn listen_control(
endpoint: &'static Endpoint, endpoint: &'static Endpoint,
routing_table: &'static RoutingTable, routing_table: &'static RoutingTable,
) -> anyhow::Result<()> { ) -> anyhow::Result<Infallible> {
let app = axum::Router::new() let app = axum::Router::new()
.route( .route(
"/metrics", "/metrics",
@ -228,7 +229,7 @@ async fn listen_control(
) )
.serve(app.into_make_service()) .serve(app.into_make_service())
.await?; .await?;
Ok(()) Err(anyhow!("control endpoint closed"))
} }
async fn try_handle_minecraft( async fn try_handle_minecraft(
@ -298,15 +299,19 @@ async fn handle_minecraft(connection: TcpStream, routing_table: &'static Routing
}; };
} }
async fn listen_minecraft(routing_table: &'static RoutingTable) -> anyhow::Result<()> { async fn listen_minecraft(routing_table: &'static RoutingTable) -> anyhow::Result<Infallible> {
let server = tokio::net::TcpListener::bind( let server = tokio::net::TcpListener::bind(
std::env::var("QUICLIME_BIND_ADDR_MC") std::env::var("QUICLIME_BIND_ADDR_MC")
.context("Reading QUICLIME_BIND_ADDR_MC")? .context("Reading QUICLIME_BIND_ADDR_MC")?
.parse::<SocketAddr>()?, .parse::<SocketAddr>()?,
) )
.await?; .await?;
while let Ok((connection, _)) = server.accept().await { loop {
tokio::spawn(handle_minecraft(connection, routing_table)); match server.accept().await {
Ok((connection, _)) => { tokio::spawn(handle_minecraft(connection, routing_table)); },
Err(e) => {
error!("Error accepting minecraft connection: {}", e);
},
}
} }
Ok(())
} }