add tracing, env config, dotenvy
bootstrap: tracing-subscriber with RUST_LOG env filter, ServerConfig from env vars (KFRAME_DATABASE_URL, KFRAME_TCP_ADDR, etc.), dotenvy for .env file loading. Replaced all println with tracing macros. tcp-server: replaced println with tracing::info/warn. Added .env.example and .gitignore for db files.
This commit is contained in:
@@ -2,6 +2,7 @@ use std::sync::Arc;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tracing::{info, warn};
|
||||
use crate::broadcaster::TcpBroadcaster;
|
||||
use crate::error::TcpServerError;
|
||||
|
||||
@@ -10,11 +11,11 @@ pub async fn run_tcp_server(
|
||||
broadcaster: Arc<TcpBroadcaster>,
|
||||
) -> Result<(), TcpServerError> {
|
||||
let listener = TcpListener::bind(addr).await.map_err(TcpServerError::Io)?;
|
||||
println!("TCP server listening on {addr}");
|
||||
info!(addr, "TCP server listening");
|
||||
|
||||
loop {
|
||||
let (mut socket, peer) = listener.accept().await.map_err(TcpServerError::Io)?;
|
||||
println!("Client connected: {peer}");
|
||||
info!(%peer, "client connected");
|
||||
|
||||
let mut rx = broadcaster.subscribe();
|
||||
|
||||
@@ -23,13 +24,13 @@ pub async fn run_tcp_server(
|
||||
match rx.recv().await {
|
||||
Ok(frame) => {
|
||||
if socket.write_all(&frame).await.is_err() {
|
||||
println!("Client disconnected: {peer}");
|
||||
info!(%peer, "client disconnected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(broadcast::error::RecvError::Closed) => break,
|
||||
Err(broadcast::error::RecvError::Lagged(n)) => {
|
||||
println!("Client {peer} lagged by {n} messages");
|
||||
warn!(%peer, skipped = n, "client lagged");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user