feat: add client auto-reconnect, heartbeat, and connection status UI

RPC layer (quicprochat-rpc):
- RpcClient now uses tokio::sync::Mutex<Connection> for safe reconnection
- Auto-reconnect with exponential backoff + jitter on retriable errors
- QUIC-level keepalive via quinn TransportConfig
- subscribe_push() returns Option<PushFrame> with None sentinel on break
- RpcError::is_retriable() classifies transient vs permanent errors
- ConnectionState enum (Connected/Reconnecting/Disconnected) with Display
- Configurable max_retries, base_delay, max_backoff, keepalive_secs

SDK layer (quicprochat-sdk):
- QpqClient wraps RpcClient in Arc for safe heartbeat task sharing
- start_heartbeat() spawns background task checking connection every 30s
- connection_state() exposes RPC-layer state to UI
- Reconnecting event added to ClientEvent enum
- disconnect() aborts heartbeat before closing connection

Client UI (quicprochat-client):
- TUI status bar shows Connected/Reconnecting.../Offline with color
- TUI handles Reconnecting event with attempt count display
- REPL event listener prints connection state changes
- REPL /status shows connection state instead of bool
- Both TUI and REPL call start_heartbeat() on startup
This commit is contained in:
2026-03-08 18:00:47 +01:00
parent 66eca065e0
commit e4c5868b31
8 changed files with 526 additions and 99 deletions

View File

@@ -72,3 +72,92 @@ pub enum RpcError {
#[error("payload too large: {size} bytes (max {max})")]
PayloadTooLarge { size: usize, max: usize },
}
impl RpcError {
/// Returns `true` if this error is transient and the operation may succeed
/// on retry (e.g. connection reset, timeout, server 5xx). Returns `false`
/// for permanent failures (auth, bad request, payload limits).
pub fn is_retriable(&self) -> bool {
match self {
Self::Connection(_) | Self::Timeout | Self::StreamClosed => true,
Self::Server { status, .. } => matches!(
status,
RpcStatus::Unavailable
| RpcStatus::DeadlineExceeded
| RpcStatus::Internal
| RpcStatus::RateLimited
),
Self::Encode(_) | Self::Decode(_) | Self::PayloadTooLarge { .. } => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn retriable_errors() {
assert!(RpcError::Connection("reset".into()).is_retriable());
assert!(RpcError::Timeout.is_retriable());
assert!(RpcError::StreamClosed.is_retriable());
assert!(RpcError::Server {
status: RpcStatus::Unavailable,
message: String::new(),
}
.is_retriable());
assert!(RpcError::Server {
status: RpcStatus::Internal,
message: String::new(),
}
.is_retriable());
assert!(RpcError::Server {
status: RpcStatus::DeadlineExceeded,
message: String::new(),
}
.is_retriable());
assert!(RpcError::Server {
status: RpcStatus::RateLimited,
message: String::new(),
}
.is_retriable());
}
#[test]
fn non_retriable_errors() {
assert!(!RpcError::Encode("bad".into()).is_retriable());
assert!(!RpcError::Decode("bad".into()).is_retriable());
assert!(!RpcError::PayloadTooLarge { size: 100, max: 50 }.is_retriable());
assert!(!RpcError::Server {
status: RpcStatus::Unauthorized,
message: String::new(),
}
.is_retriable());
assert!(!RpcError::Server {
status: RpcStatus::BadRequest,
message: String::new(),
}
.is_retriable());
assert!(!RpcError::Server {
status: RpcStatus::Forbidden,
message: String::new(),
}
.is_retriable());
assert!(!RpcError::Server {
status: RpcStatus::NotFound,
message: String::new(),
}
.is_retriable());
}
#[test]
fn connection_state_display() {
use crate::client::ConnectionState;
assert_eq!(ConnectionState::Connected.to_string(), "Connected");
assert_eq!(ConnectionState::Disconnected.to_string(), "Disconnected");
assert_eq!(
ConnectionState::Reconnecting { attempt: 2 }.to_string(),
"Reconnecting (attempt 2)"
);
}
}