improve websocket agent reconnection after network interruptions (#1263)

This commit is contained in:
henrygd
2025-10-09 12:09:52 -04:00
parent d352ce00fa
commit d00c0488c3

View File

@@ -143,7 +143,9 @@ func (client *WebSocketClient) OnOpen(conn *gws.Conn) {
// OnClose handles WebSocket connection closure. // OnClose handles WebSocket connection closure.
// It logs the closure reason and notifies the connection manager. // It logs the closure reason and notifies the connection manager.
func (client *WebSocketClient) OnClose(conn *gws.Conn, err error) { func (client *WebSocketClient) OnClose(conn *gws.Conn, err error) {
slog.Warn("Connection closed", "err", strings.TrimPrefix(err.Error(), "gws: ")) if err != nil {
slog.Warn("Connection closed", "err", strings.TrimPrefix(err.Error(), "gws: "))
}
client.agent.connectionManager.eventChan <- WebSocketDisconnect client.agent.connectionManager.eventChan <- WebSocketDisconnect
} }
@@ -246,7 +248,13 @@ func (client *WebSocketClient) sendMessage(data any) error {
if err != nil { if err != nil {
return err return err
} }
return client.Conn.WriteMessage(gws.OpcodeBinary, bytes) err = client.Conn.WriteMessage(gws.OpcodeBinary, bytes)
if err != nil {
// If writing fails (e.g., broken pipe due to network issues),
// close the connection to trigger reconnection logic (#1263)
client.Close()
}
return err
} }
// sendResponse sends a response with optional request ID for the new protocol // sendResponse sends a response with optional request ID for the new protocol