From 7b0efc235fb16cf1dceb633216f000e35eebc426 Mon Sep 17 00:00:00 2001 From: in-void Date: Wed, 1 Feb 2023 13:05:12 +0100 Subject: [PATCH] websocket api new debug logging points --- internal/websocket/common/client.go | 4 ++++ internal/websocket/common/handler.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/websocket/common/client.go b/internal/websocket/common/client.go index 245b8f3..47b8cf0 100644 --- a/internal/websocket/common/client.go +++ b/internal/websocket/common/client.go @@ -67,6 +67,10 @@ func (c *Client) readPump() { logging.WithFields(logging.Fields{ "reason": err.Error(), }).Error("Websocket connection closed unexpected") + } else { + logging.WithFields(logging.Fields{ + "reason": err.Error(), + }).Info("Connection closed") } break diff --git a/internal/websocket/common/handler.go b/internal/websocket/common/handler.go index dc645ca..daf2606 100644 --- a/internal/websocket/common/handler.go +++ b/internal/websocket/common/handler.go @@ -39,6 +39,11 @@ func (h *ConnectionHandler) Handle() gin.HandlerFunc { return func(c *gin.Context) { channel := c.Request.URL.Path + logging.WithDefaultField("channel", channel) + logging.WithDefaultField("ip", c.ClientIP()) + + logging.Info("New channel subscriber") + hub := h.getHub(channel) h.serveWs(hub, c.Writer, c.Request) @@ -71,10 +76,13 @@ func (h *ConnectionHandler) serveWs(hub *Hub, w http.ResponseWriter, r *http.Req go client.readPump() go func() { - <-time.After(time.Duration(3) * time.Minute) + disconnectAfter := time.Duration(3) * time.Minute + + <-time.After(disconnectAfter) defer func() { - logging.Debug("Disconnect websocket client") + logging.Info("Connection closed after", disconnectAfter, "minutes") + client.hub.unregister <- client client.conn.Close() }()