Skip to content

Commit

Permalink
feat: quick return if condition not met
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 29, 2024
1 parent 2b0ef91 commit 16f318d
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions node/pkg/dal/api/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,40 @@ func (c *Hub) handleClientRegistration() {
}

func (c *Hub) addClient(conn *websocket.Conn) {
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
if _, ok := c.clients[conn]; ok {
c.mu.RUnlock()
return
}
c.mu.RUnlock()

c.mu.Lock()
defer c.mu.Unlock()

c.clients[conn] = make(map[string]bool)
}

func (c *Hub) removeClient(conn *websocket.Conn) {
c.mu.RLock()
if _, ok := c.clients[conn]; !ok {
c.mu.RUnlock()
return
}
c.mu.RUnlock()

c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.clients[conn]; ok {
for symbol := range c.clients[conn] {
delete(c.clients[conn], symbol)
}
delete(c.clients, conn)

for symbol := range c.clients[conn] {
delete(c.clients[conn], symbol)
}
if err := conn.WriteControl(
delete(c.clients, conn)

_ = conn.WriteControl(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
time.Now().Add(time.Second),
); err != nil {
log.Error().Err(err).Msg("failed to send close message")
}
)
conn.Close()
}

Expand Down

0 comments on commit 16f318d

Please sign in to comment.