Unreachable container times out gracefully

This commit is contained in:
Timmy
2026-06-29 19:22:47 +01:00
parent 32646c6256
commit 3342d129c2
2 changed files with 20 additions and 7 deletions
@@ -183,13 +183,24 @@ impl BotContext {
Err(e) => return Some(format!("Failed to serialize RPC request: {e}")),
};
let ws_stream = match tokio_tungstenite::connect_async(&ws_url).await {
Ok((stream, _)) => stream,
Err(e) => {
let connect_timeout = std::time::Duration::from_secs(5);
let ws_stream = match tokio::time::timeout(
connect_timeout,
tokio_tungstenite::connect_async(&ws_url),
)
.await
{
Ok(Ok((stream, _))) => stream,
Ok(Err(e)) => {
return Some(format!(
"Failed to connect to project server at {ws_url}: {e}"
));
}
Err(_) => {
return Some(format!(
"Project server at {ws_url} is unreachable (connect timed out after {connect_timeout:?})"
));
}
};
let (mut sink, mut stream) = ws_stream.split();
@@ -198,7 +209,9 @@ impl BotContext {
return Some(format!("Failed to send RPC request: {e}"));
}
while let Some(msg) = stream.next().await {
let response_timeout = std::time::Duration::from_secs(30);
let deadline = tokio::time::Instant::now() + response_timeout;
while let Ok(Some(msg)) = tokio::time::timeout_at(deadline, stream.next()).await {
match msg {
Ok(WsMsg::Text(text)) => {
let Ok(frame) = serde_json::from_str::<serde_json::Value>(&text) else {
@@ -239,7 +252,7 @@ impl BotContext {
}
}
Some("Connection closed before receiving command response".to_string())
Some("Project server did not respond in time (connection closed or timed out)".to_string())
}
}