Browse Source

don't reuse messages in websockets

main
Stephanie Gredell 5 months ago
parent
commit
ab972d9017
  1. 15
      internal/handlers/home.go

15
internal/handlers/home.go

@ -106,8 +106,8 @@ func (h *hub) Run() { @@ -106,8 +106,8 @@ func (h *hub) Run() {
return true
})
// Return message to pool after all clients processed
h.messagePool.Put(message)
// DON'T return message to pool - let GC handle it
// h.messagePool.Put(message)
}
}
}
@ -139,18 +139,17 @@ func (c *Client) readPump() { @@ -139,18 +139,17 @@ func (c *Client) readPump() {
return
}
// Get a message buffer from the pool
message := c.hub.messagePool.Get().([]byte)
message = message[:len(messageBytes)]
// Create a new message buffer instead of reusing from pool
message := make([]byte, len(messageBytes))
copy(message, messageBytes)
// Non-blocking broadcast
select {
case c.hub.broadcast <- message:
// Message will be returned to pool by hub.Run()
// Message will be sent to all clients
default:
// Broadcast buffer full, return message to pool and drop
c.hub.messagePool.Put(message)
// Broadcast buffer full, drop message
fmt.Printf("Broadcast buffer full, dropping message\n")
}
}
}

Loading…
Cancel
Save