This repository has been archived on 2026-03-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
oubliette/internal/storage/retention.go
Torjus Håkestad d655968216 feat: add SQLite storage for login attempts and sessions
Adds persistent storage using modernc.org/sqlite (pure Go). Login
attempts are deduplicated by (username, password, ip) with counts.
Sessions and session logs are tracked with UUID IDs. Includes embedded
SQL migrations, configurable retention with background pruning, and
an in-memory store for tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-14 17:33:45 +01:00

39 lines
814 B
Go

package storage
import (
"context"
"log/slog"
"time"
)
// RunRetention periodically deletes records older than retentionDays.
// It runs one prune immediately on startup, then on the given interval.
// It returns when ctx is cancelled.
func RunRetention(ctx context.Context, store Store, retentionDays int, interval time.Duration, logger *slog.Logger) {
prune := func() {
cutoff := time.Now().UTC().AddDate(0, 0, -retentionDays)
n, err := store.DeleteRecordsBefore(ctx, cutoff)
if err != nil {
logger.Error("retention prune failed", "err", err)
return
}
if n > 0 {
logger.Info("retention prune completed", "deleted_rows", n)
}
}
prune()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
prune()
}
}
}