Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: max connection error code and added missing reason #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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