Implement periodic ping frames in WebSocket server for keepalive and update overproduction handling in ProduceWorker to include branch_id. This enhances connection stability and improves notification clarity for overproduction events.

This commit is contained in:
Torsten Schulz (local)
2025-12-01 13:25:23 +01:00
parent 3e9f921f4f
commit b8fa644c97
2 changed files with 28 additions and 6 deletions

View File

@@ -15,6 +15,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpListener;
use tokio::runtime::{Builder, Runtime};
use tokio::sync::{broadcast, mpsc, Mutex};
use tokio::time::{interval, Duration as TokioDuration};
use tokio_rustls::rustls::{self, ServerConfig};
use tokio_rustls::TlsAcceptor;
use tokio_tungstenite::tungstenite::Message;
@@ -473,8 +474,12 @@ async fn handle_connection<S>(
}
};
// Broker-Nachrichten an den Client
// Broker-Nachrichten an den Client + periodische Ping-Frames als Keepalive
let outgoing = async move {
// Regelmäßiges Ping, um inaktiven Verbindungen ein Lebenszeichen zu senden
// und Timeouts auf dem Weg (Proxy/Loadbalancer) zu vermeiden.
let mut ping_interval = interval(TokioDuration::from_secs(240));
loop {
tokio::select! {
// Nachrichten aus dem MessageBroker
@@ -551,6 +556,16 @@ async fn handle_connection<S>(
}
}
}
// Periodisches Ping an den Client
_ = ping_interval.tick() => {
if let Err(e) = ws_sender.send(Message::Ping(Vec::new())).await {
eprintln!(
"[WebSocketServer] Fehler beim Senden von Ping an {}: {}",
peer_addr, e
);
break;
}
}
}
}
};