Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -12,27 +12,16 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// This file is the chat hub behind GET /ws. It is deliberately a package-level
|
||||
// singleton (one process, one chat room) rather than an injectable type, so the
|
||||
// connection set, the database handle and the allowed origin all live in
|
||||
// package vars initialised once by InitWebSocket.
|
||||
// The chat hub behind GET /ws: one process, one room, so package-level state.
|
||||
|
||||
// maxMessages is both the size of the history replayed to a new client and the
|
||||
// number of rows kept in the database: every insert trims older messages.
|
||||
// History replayed on connect, and rows kept in the database.
|
||||
const maxMessages = 50
|
||||
|
||||
// allowedDomain is the site's domain, set by InitWebSocket and read by
|
||||
// Upgrader.CheckOrigin. It is a package var because the Upgrader is one too.
|
||||
var allowedDomain string
|
||||
|
||||
// Upgrader turns the HTTP request into a WebSocket.
|
||||
//
|
||||
// CheckOrigin is the only cross-origin defence on this endpoint: the browser
|
||||
// sends cookies with a WebSocket handshake regardless of origin and there is
|
||||
// no preflight, so without this check any site could open a socket as a
|
||||
// logged-in visitor. Default gorilla behaviour (same-host only) is too strict
|
||||
// here because the site is reached as both example.com and www.example.com,
|
||||
// and "localhost" is allowed for the Vite dev server.
|
||||
// CheckOrigin is the only cross-origin defence: browsers send cookies on a
|
||||
// WebSocket handshake with no preflight. Gorilla's same-host default would
|
||||
// reject the apex/www split and the Vite dev server.
|
||||
var Upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
@@ -51,29 +40,20 @@ var Upgrader = websocket.Upgrader{
|
||||
}
|
||||
|
||||
var (
|
||||
// clients maps each open connection to whether it belongs to an admin.
|
||||
clients = make(map[*websocket.Conn]bool)
|
||||
// mu guards clients AND serialises the write-then-broadcast sequence, so
|
||||
// that two concurrent senders cannot interleave and deliver messages to
|
||||
// different clients in different orders.
|
||||
// Also serialises write-then-broadcast so all clients see one order.
|
||||
mu sync.Mutex
|
||||
wsDB *gorm.DB
|
||||
// nextAuthorID is a per-process counter handed out on connect. It is NOT
|
||||
// a user id and has no meaning across restarts: the chat is pseudonymous,
|
||||
// and the front end only uses it to colour/group consecutive messages
|
||||
// from the same connection.
|
||||
// Per-process connection number, not a user id; chat is pseudonymous.
|
||||
nextAuthorID uint
|
||||
)
|
||||
|
||||
// Per-connection flood limit, separate from services.RateLimiter because it
|
||||
// counts frames on one socket rather than requests from one IP.
|
||||
// Per-socket flood limit (frames, not requests per IP).
|
||||
const (
|
||||
rateLimitWindow = time.Second
|
||||
rateLimitMaxMsgs = 10
|
||||
)
|
||||
|
||||
// wsIncoming is the envelope clients send over the socket. A plain chat
|
||||
// message carries text/fileUrl/private; an admin delete carries action+id.
|
||||
type wsIncoming struct {
|
||||
Action string `json:"action,omitempty"`
|
||||
ID uint `json:"id,omitempty"`
|
||||
@@ -82,37 +62,28 @@ type wsIncoming struct {
|
||||
Private bool `json:"private,omitempty"`
|
||||
}
|
||||
|
||||
// wsDeleteEvent is broadcast to every client when a message is removed.
|
||||
type wsDeleteEvent struct {
|
||||
Action string `json:"action"`
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
// wsHistoryEvent is sent once on connect with the recent messages the client
|
||||
// may see. Sending it as a single batch lets the client replace its list
|
||||
// atomically, so a reconnect does not blank the chat and reload every image.
|
||||
// Sent as one batch so a reconnect replaces the list without blanking it.
|
||||
type wsHistoryEvent struct {
|
||||
Action string `json:"action"`
|
||||
Messages []models.Message `json:"messages"`
|
||||
}
|
||||
|
||||
// InitWebSocket wires the package-level hub to the database and the origin
|
||||
// allow-list. Call it once, before serving.
|
||||
func InitWebSocket(database *gorm.DB, domain string) {
|
||||
wsDB = database
|
||||
allowedDomain = domain
|
||||
}
|
||||
|
||||
// HandleWebSocket serves one chat connection. isAdmin controls whether the
|
||||
// connection may see private messages, send them, and delete messages.
|
||||
// isAdmin allows seeing, sending and deleting private messages.
|
||||
func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
||||
defer conn.Close()
|
||||
|
||||
// Registration, history read and history send all happen under one lock.
|
||||
// That is the point: it guarantees the client is already in `clients`
|
||||
// before its snapshot is taken, so a message broadcast concurrently is
|
||||
// either in the history or delivered afterwards — never dropped in the
|
||||
// gap, and never delivered before the history that would overwrite it.
|
||||
// Register and snapshot under one lock, so a concurrent broadcast is
|
||||
// either in the history or delivered after it, never lost or reordered.
|
||||
mu.Lock()
|
||||
clients[conn] = isAdmin
|
||||
nextAuthorID++
|
||||
@@ -132,15 +103,12 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// Fixed-window flood limit. Over-limit frames are dropped silently with
|
||||
// no error to the client and without resetting the window.
|
||||
// Over-limit frames are dropped silently.
|
||||
msgCount := 0
|
||||
windowStart := time.Now()
|
||||
|
||||
for {
|
||||
var incoming wsIncoming
|
||||
// Any read error — clean close, network drop or malformed JSON —
|
||||
// ends the connection; there is no attempt to resynchronise.
|
||||
if err := conn.ReadJSON(&incoming); err != nil {
|
||||
break
|
||||
}
|
||||
@@ -166,24 +134,16 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
||||
Content: incoming.Text,
|
||||
FileURL: incoming.FileURL,
|
||||
AuthorID: authorID,
|
||||
// Only admins may mark a message private.
|
||||
Private: incoming.Private && isAdmin,
|
||||
Private: incoming.Private && isAdmin,
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
wsDB.Create(&msg)
|
||||
// Keep only the newest maxMessages rows. This is a soft delete
|
||||
// (models.Message has gorm.DeletedAt), so the rows stay in the table
|
||||
// with deleted_at set and GORM filters them out of later reads; the
|
||||
// table therefore grows forever even though the chat does not.
|
||||
// The subquery is itself soft-delete filtered, so already-trimmed
|
||||
// rows are not re-deleted.
|
||||
// Soft delete, so the table still grows; the chat does not.
|
||||
wsDB.Where("id NOT IN (?)",
|
||||
wsDB.Model(&models.Message{}).Select("id").Order("created_at DESC").Limit(maxMessages),
|
||||
).Delete(&models.Message{})
|
||||
|
||||
// Deleting from a map while ranging over it is explicitly allowed in
|
||||
// Go, so pruning dead clients inline here is safe.
|
||||
for client, clientAdmin := range clients {
|
||||
if msg.Private && !clientAdmin {
|
||||
continue
|
||||
@@ -201,9 +161,7 @@ func HandleWebSocket(conn *websocket.Conn, isAdmin bool) {
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// deleteMessage soft-deletes a message and tells every client to drop it.
|
||||
// Clients that never received the message (non-admins for a private one)
|
||||
// simply ignore the unknown id.
|
||||
// deleteMessage soft-deletes and broadcasts; clients ignore unknown ids.
|
||||
func deleteMessage(id uint) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user