From 0aca94ba2e4552fa3fe05ad3b96b75fd72c24ce0 Mon Sep 17 00:00:00 2001 From: Akbar Shaikh Date: Sun, 28 Aug 2022 00:42:19 +0530 Subject: [PATCH] fix: max connection error code and added missing reason --- identification/identifier.go | 2 +- services/rest/websocket/connection/upgrader.go | 13 +++++++------ services/rest/websocket/handler.go | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/identification/identifier.go b/identification/identifier.go index 23d81ed7..fb2b6711 100644 --- a/identification/identifier.go +++ b/identification/identifier.go @@ -10,5 +10,5 @@ type Identifier struct { } func (i Identifier) String() string { - return fmt.Sprintf("connection [%s] %s", i.Group, i.ID) + return fmt.Sprintf("[%s] %s", i.Group, i.ID) } diff --git a/services/rest/websocket/connection/upgrader.go b/services/rest/websocket/connection/upgrader.go index 6a8d88d3..e921b8ee 100644 --- a/services/rest/websocket/connection/upgrader.go +++ b/services/rest/websocket/connection/upgrader.go @@ -69,17 +69,18 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request) (Conn, error) } err = u.Table.Store(identifier) if errors.Is(err, errConnDuplicated) { - duplicateConnResp := createEmptyErrorResponse(pb.Code_CODE_MAX_USER_LIMIT_REACHED) + errMsg := fmt.Sprintf("%s: %s,", err.Error(), identifier) + duplicateConnResp := createEmptyErrorResponse(pb.Code_CODE_MAX_USER_LIMIT_REACHED, errMsg) conn.WriteMessage(websocket.BinaryMessage, duplicateConnResp) - conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1008, "Duplicate connection")) + conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1008, "duplicate connection: "+identifier.ID)) metrics.Increment("user_connection_failure_total", fmt.Sprintf("reason=exists,conn_group=%s", identifier.Group)) conn.Close() - return Conn{}, fmt.Errorf("disconnecting %s: already connected", identifier) + return Conn{}, fmt.Errorf("disconnecting connection %s: already connected", identifier) } if errors.Is(err, errMaxConnectionReached) { logger.Errorf("[websocket.Handler] Disconnecting %v, max connection reached", identifier) - maxConnResp := createEmptyErrorResponse(pb.Code_CODE_MAX_USER_LIMIT_REACHED) + maxConnResp := createEmptyErrorResponse(pb.Code_CODE_MAX_CONNECTION_LIMIT_REACHED, err.Error()) conn.WriteMessage(websocket.BinaryMessage, maxConnResp) conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1008, "Max connection reached")) metrics.Increment("user_connection_failure_total", fmt.Sprintf("reason=serverlimit,conn_group=%s", identifier.Group)) @@ -130,12 +131,12 @@ func (u *Upgrader) newIdentifier(h http.Header) identification.Identifier { } } -func createEmptyErrorResponse(errCode pb.Code) []byte { +func createEmptyErrorResponse(errCode pb.Code, errMsg string) []byte { resp := pb.SendEventResponse{ Status: pb.Status_STATUS_ERROR, Code: errCode, SentTime: time.Now().Unix(), - Reason: "", + Reason: errMsg, Data: nil, } duplicateConnResp, _ := proto.Marshal(&resp) diff --git a/services/rest/websocket/handler.go b/services/rest/websocket/handler.go index e2f69b6c..39a877f4 100644 --- a/services/rest/websocket/handler.go +++ b/services/rest/websocket/handler.go @@ -71,7 +71,7 @@ func (h *Handler) Table() *connection.Table { func (h *Handler) HandlerWSEvents(w http.ResponseWriter, r *http.Request) { conn, err := h.upgrader.Upgrade(w, r) if err != nil { - logger.Debugf("[websocket.Handler] %v", err) + logger.Errorf("[websocket.Handler] %v", err) return } defer conn.Close()