Files
OwnCord/Server/ws/event_pruner.go
T
J3vbandClaude Fable 5 1fbedb404c fix(lint): delete dead ws broadcast cluster; demote phase-header comments
golangci-lint had been failing invisibly behind the earlier CI gate
failures. Default-build lint is now clean:

- Delete the unused pre-topic-limiter rate-limit constants, the unused
  bluemonday sanitizer, and the dead broadcast variants superseded by
  their Low/High counterparts (broadcastExclude,
  broadcastToDMParticipants(+Exclude), sendSequencedToUsers,
  PubSub.debugDump). Test references were comments only; updated to
  name the live variants.
- Separate 'Phase X Step Y' file headers from the package clause with a
  blank line so staticcheck ST1000 no longer reads them as malformed
  package comments (proper package docs exist in hub.go/manifest.go).
- Add .gitattributes normalizing line endings to LF on checkout —
  the Windows CI runner materialized CRLF, which made every
  prettier-formatted file fail the format gate.

Known remainder (pre-existing, out of P0 scope): golangci-lint with
-tags wazero reports 3 gosec + 2 staticcheck and -tags otel 1+1; CI
lints the default build. Tracked for the P1 plugin pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-18 12:55:22 +02:00

77 lines
2.2 KiB
Go

// Phase B Step 7 — Event Persistence Layer (pruner).
//
// StartEventPruner runs a background goroutine that deletes events older than
// the configured retention window. It is the bounded-storage half of the
// event persistence design: the persister appends, the pruner trims.
package ws
import (
"context"
"log/slog"
"time"
"github.com/owncord/server/store"
)
// maxStartupDelay caps how long StartEventPruner waits before its first
// prune pass. We want a short delay so a freshly started server with a
// tiny dataset doesn't keep stale rows around for a full interval, but we
// don't want the delay to exceed the interval itself (otherwise a server
// running with interval=5s would wait longer than its own tick).
const maxStartupDelay = time.Minute
// StartEventPruner launches a goroutine that wakes every interval and deletes
// events older than retention. The goroutine exits when ctx is cancelled.
func StartEventPruner(ctx context.Context, s store.EventStore, retention, interval time.Duration) {
if s == nil {
return
}
if retention <= 0 {
retention = 24 * time.Hour
}
if interval <= 0 {
interval = time.Hour
}
// Bound the startup delay by the interval so short test intervals
// (e.g. 100ms in event_pruner_test.go) don't wait a full minute.
startupDelayDuration := maxStartupDelay
if interval < startupDelayDuration {
startupDelayDuration = interval
}
go func() {
// Run once shortly after startup so a tiny dataset stays small.
startupDelay := time.NewTimer(startupDelayDuration)
defer startupDelay.Stop()
select {
case <-ctx.Done():
return
case <-startupDelay.C:
}
runPrune(ctx, s, retention)
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
runPrune(ctx, s, retention)
}
}
}()
}
func runPrune(ctx context.Context, s store.EventStore, retention time.Duration) {
cutoff := time.Now().Add(-retention)
deleted, err := s.PruneEventsOlderThan(ctx, cutoff)
if err != nil {
slog.Warn("event pruner: PruneEventsOlderThan failed", "err", err)
return
}
if deleted > 0 {
slog.Info("event pruner: pruned old events", "deleted", deleted, "cutoff", cutoff.Format(time.RFC3339))
}
}