Files
anchorage/internal/pkg/wsserver/hub.go
T
57_WolveandClaude Opus 4.7 90ac4b169c
Security / Vulnerability Check (push) Successful in 1m47s
Test / Build & Unit Tests (push) Successful in 5m4s
Test / Lint (push) Successful in 27s
Test / Integration Tests (push) Failing after 1m58s
ci: fix first CI run — tidy, gofmt, ordering test, govulncheck pin
- ids: TestIDsAreTimeOrdered asserted strict lexicographic ordering of
  back-to-back UUIDv7s, but the sub-ms tail is random and not required
  to be monotonic. Sleep between samples so each ID lands in a distinct
  millisecond — the property that actually gives Postgres index
  locality on (org_id, id desc).
- go.mod/go.sum: run go mod tidy. keyfunc/v3, prometheus/client_golang
  and testcontainers-go/modules/postgres are imported directly and
  should not be marked // indirect; also drops stale sum entries.
- gofmt -w across 12 files flagged by the lint job.
- security.yml: pin govulncheck to v1.2.0. @latest triggers a proxy
  lookup every run, which is the step that hung for 16m on the Gitea
  runner.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 09:18:49 -05:00

84 lines
2.0 KiB
Go

// Package wsserver runs the WebSocket hub for pin status events.
//
// One endpoint, /v1/events. Each connection is bound to the
// authenticated client's orgID and optionally to a specific requestID.
// Incoming NATS messages on pin.events.<orgID>.<requestID> are fanned
// out to matching connections as JSON frames.
package wsserver
import (
"encoding/json"
"log/slog"
"sync"
"github.com/gofiber/contrib/websocket"
"github.com/nats-io/nats.go"
"anchorage/internal/pkg/ids"
"anchorage/internal/pkg/pin"
)
// Hub owns subscriptions and dispatch.
type Hub struct {
nc *nats.Conn
mu sync.Mutex
conns map[*websocket.Conn]*clientFilter
}
type clientFilter struct {
OrgID ids.OrgID
RequestID *ids.PinID // nil = all pins for the org
}
// NewHub creates a Hub subscribed to pin.events.>.
func NewHub(nc *nats.Conn) (*Hub, error) {
h := &Hub{nc: nc, conns: map[*websocket.Conn]*clientFilter{}}
if nc != nil {
_, err := nc.Subscribe(pin.EventsSubjectPrefix+".>", func(m *nats.Msg) {
h.broadcast(m)
})
if err != nil {
return nil, err
}
}
return h, nil
}
// Register adds a conn with its filter. Call Unregister on close.
func (h *Hub) Register(c *websocket.Conn, orgID ids.OrgID, rid *ids.PinID) {
h.mu.Lock()
defer h.mu.Unlock()
h.conns[c] = &clientFilter{OrgID: orgID, RequestID: rid}
}
// Unregister removes conn.
func (h *Hub) Unregister(c *websocket.Conn) {
h.mu.Lock()
defer h.mu.Unlock()
delete(h.conns, c)
}
func (h *Hub) broadcast(m *nats.Msg) {
var ev pin.Event
if err := json.Unmarshal(m.Data, &ev); err != nil {
slog.Warn("wsserver: malformed event", "err", err)
return
}
h.mu.Lock()
defer h.mu.Unlock()
for c, f := range h.conns {
if f.OrgID.String() != ev.OrgID {
continue
}
if f.RequestID != nil && f.RequestID.String() != ev.RequestID {
continue
}
if err := c.WriteMessage(websocket.TextMessage, m.Data); err != nil {
slog.Warn("wsserver: write", "err", err)
_ = c.Close()
delete(h.conns, c)
}
}
}