This commit is contained in:
parent
ac8812be2f
commit
29ff12f115
7 changed files with 111 additions and 69 deletions
73
src/main.rs
73
src/main.rs
|
|
@ -35,63 +35,30 @@ async fn main() {
|
|||
let listener = TcpListener::bind(config.bind_addr.clone()).await.unwrap();
|
||||
tracing::info!(bind_addr = config.bind_addr, "started tcp server");
|
||||
|
||||
let conn_task = tokio::spawn(async move {
|
||||
loop {
|
||||
let (socket, addr) = listener.accept().await.unwrap();
|
||||
let api = api.clone();
|
||||
|
||||
let config = config.clone();
|
||||
tokio::spawn(async move {
|
||||
tracing::debug!(
|
||||
addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()),
|
||||
"Client connected"
|
||||
);
|
||||
if let Err(e) = proxy::process_connection(socket, addr, api, config).await {
|
||||
match e.level {
|
||||
tracing::Level::ERROR => tracing::error!(
|
||||
// addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()),
|
||||
trace = %e.print_span_trace(),
|
||||
err = format!("{}", e.context),
|
||||
"Client disconnected"
|
||||
),
|
||||
tracing::Level::WARN => tracing::warn!(
|
||||
// addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()),
|
||||
trace = %e.print_span_trace(),
|
||||
err = format!("{}", e.context),
|
||||
"Client disconnected"
|
||||
),
|
||||
tracing::Level::INFO => tracing::info!(
|
||||
// addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()),
|
||||
trace = %e.print_span_trace(),
|
||||
err = format!("{}", e.context),
|
||||
"Client disconnected"
|
||||
),
|
||||
_ => {
|
||||
tracing::error!(
|
||||
// addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()),
|
||||
trace = %e.print_span_trace(),
|
||||
err = format!("{}", e.context),
|
||||
actual_level = ?e.level,
|
||||
"Client disconnected (bad level)"
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::debug!(
|
||||
addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()),
|
||||
"Client disconnected"
|
||||
);
|
||||
}
|
||||
});
|
||||
let mut sigterm = match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||
{
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
tracing::error!(error = ?e, "could not initilaize SIGTERM handling channel!");
|
||||
return;
|
||||
}
|
||||
});
|
||||
};
|
||||
let cancel_token = tokio_util::sync::CancellationToken::new();
|
||||
|
||||
let mut conn_task = proxy::start_proxy(listener, api, config, cancel_token.clone());
|
||||
|
||||
tokio::select! {
|
||||
result = api_task => {
|
||||
tracing::error!("The api tokio:spawn'ed task ran to completion, which should not happen!");
|
||||
_ = api_task => {
|
||||
tracing::error!("the MinecraftApi tokio:spawn'ed task ran to completion, which should not happen!");
|
||||
}
|
||||
result = conn_task => {
|
||||
tracing::error!("The connection handling tokio:spawn'ed task ran to completion, which should not happen!");
|
||||
_ = &mut conn_task => {
|
||||
tracing::error!("the connection handling tokio:spawn'ed task ran to completion, which should not happen!");
|
||||
}
|
||||
result = sigterm.recv() => {
|
||||
tracing::info!(sigterm_signal = ?result,"SIGTERM received");
|
||||
cancel_token.cancel();
|
||||
let res = conn_task.await;
|
||||
tracing::info!(api_task_result = ?res, "shutdown complete");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue