Skip to content
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
9 changes: 8 additions & 1 deletion websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const (

// Maximum message size allowed from peer.
maxMessageSize = 1048576 // 1 MB

// Buffer size for the client send channel.
clientSendBuffer = 1024
)

type HandleWebsocketMessage func([]byte) error
Expand Down Expand Up @@ -107,6 +110,7 @@ func (c *Client) Stop() {
}

c.running = false
c.consumer.Close()
c.writeMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
c.conn.Close()
close(c.send)
Expand All @@ -128,7 +132,7 @@ func (c *Client) Start() error {
defer c.mux.Unlock()

c.running = true
c.send = make(chan []byte, 100)
c.send = make(chan []byte, clientSendBuffer)
c.done = make(chan struct{})

go c.runWatcher()
Expand Down Expand Up @@ -289,16 +293,19 @@ func (c *Client) runWatcher() {
if event.Operation == common.DeleteOperation {
slog.InfoContext(c.ctx, "user deleted; closing connection")
c.Stop()
return
}

if !user.Enabled {
slog.InfoContext(c.ctx, "user disabled; closing connection")
c.Stop()
return
}

if user.Generation != c.passwordGeneration {
slog.InfoContext(c.ctx, "password generation mismatch; closing connection")
c.Stop()
return
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ func (h *Hub) run() {
}

if _, err := client.Write(message); err != nil {
slog.WarnContext(h.ctx, "failed to write websocket message",
"client_id", id,
"error", err)
staleClients = append(staleClients, id)
}
}
if len(staleClients) > 0 {
slog.WarnContext(h.ctx, "evicting stale websocket clients",
"count", len(staleClients),
"client_ids", staleClients)
h.mux.Lock()
for _, id := range staleClients {
if client, ok := h.clients[id]; ok {
Expand Down
Loading