Skip to content

Commit

Permalink
fix: max connection error code and added missing reason (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkbaraliShaikh committed Aug 29, 2022
1 parent 8bcf4f7 commit b895a6c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion identification/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 7 additions & 6 deletions services/rest/websocket/connection/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion services/rest/websocket/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b895a6c

Please sign in to comment.