Files
minstrel/internal/library/loudness_settings.go
T
bvandeusenandClaude Opus 5.5 285a24f0ab
release / web (push) Successful in 1m13s
release / go (push) Successful in 1m48s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 3m56s
release / android (push) Successful in 4m19s
release / Build signed APK (push) Successful in 4m40s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(db): fold every migration into one baseline at version 106
0106_baseline.up.sql is the schema at version 106 and the settings rows
it starts with, taken from a database migrated through 0001–0106; a
database built from the baseline alone dumps identically. A database
already at 106 skips it. Comments that pointed at a migration number now
name the table or constraint instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 00:57:18 -05:00

124 lines
3.9 KiB
Go

package library
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// Loudness analysis settings: a database row, changed without a restart. One
// instance is shared by the backfill and the admin API, so a save reaches the
// worker at once.
// LoudnessSettings mirrors the loudness_settings row.
type LoudnessSettings struct {
// Enabled off stops the background analysis. Measured tracks keep their
// values, so normalization keeps working for them.
Enabled bool
BackfillConcurrency int32
// LeveledCacheMB caps the disk the leveled-stream renders for Sonos and
// UPnP speakers may use; the least recently played go first.
LeveledCacheMB int32
// UpdatedAt is set by the database; ignored by Set.
UpdatedAt time.Time
}
// DefaultLoudnessSettings mirrors loudness_settings's column defaults,
// so a database that cannot be read still analyzes the way a fresh install
// does.
var DefaultLoudnessSettings = LoudnessSettings{
Enabled: true,
BackfillConcurrency: loudnessBackfillConcurrency,
LeveledCacheMB: 2048,
}
// The leveled-stream cache's bounds, as loudness_settings's CHECK has them.
const (
minLeveledCacheMB = 256
maxLeveledCacheMB = 65536
)
// ErrLoudnessSettingOutOfRange is returned by Set for a value loudness_settings's
// CHECKs would reject, so the API answers 400 naming the field.
var ErrLoudnessSettingOutOfRange = errors.New("loudness setting out of range")
// LoudnessSettingsService caches the settings and owns their persistence.
type LoudnessSettingsService struct {
pool *pgxpool.Pool
mu sync.RWMutex
cur LoudnessSettings
}
// NewLoudnessSettingsService loads once and caches. It always returns a usable
// service, holding the defaults when the load fails; the error says so.
func NewLoudnessSettingsService(ctx context.Context, pool *pgxpool.Pool) (*LoudnessSettingsService, error) {
s := &LoudnessSettingsService{pool: pool, cur: DefaultLoudnessSettings}
row, err := dbq.New(pool).GetLoudnessSettings(ctx)
if err != nil {
return s, fmt.Errorf("loudness settings: load: %w", err)
}
s.cur = loudnessSettingsFromRow(row)
return s, nil
}
// Get returns the cached settings. A nil service answers with the defaults.
func (s *LoudnessSettingsService) Get() LoudnessSettings {
if s == nil {
return DefaultLoudnessSettings
}
s.mu.RLock()
defer s.mu.RUnlock()
return s.cur
}
// Set validates, persists and re-caches.
func (s *LoudnessSettingsService) Set(ctx context.Context, in LoudnessSettings) (LoudnessSettings, error) {
if err := validateLoudnessSettings(in); err != nil {
return LoudnessSettings{}, err
}
if s == nil {
return LoudnessSettings{}, errors.New("loudness settings: no settings service")
}
row, err := dbq.New(s.pool).UpdateLoudnessSettings(ctx, dbq.UpdateLoudnessSettingsParams{
Enabled: in.Enabled,
BackfillConcurrency: in.BackfillConcurrency,
LeveledCacheMb: in.LeveledCacheMB,
})
if err != nil {
return LoudnessSettings{}, fmt.Errorf("loudness settings: save: %w", err)
}
out := loudnessSettingsFromRow(row)
s.mu.Lock()
s.cur = out
s.mu.Unlock()
return out, nil
}
func validateLoudnessSettings(in LoudnessSettings) error {
if in.BackfillConcurrency < minBackfillConcurrency || in.BackfillConcurrency > maxBackfillConcurrency {
return fmt.Errorf("%w: backfill_concurrency must be %d-%d",
ErrLoudnessSettingOutOfRange, minBackfillConcurrency, maxBackfillConcurrency)
}
if in.LeveledCacheMB < minLeveledCacheMB || in.LeveledCacheMB > maxLeveledCacheMB {
return fmt.Errorf("%w: leveled_cache_mb must be %d-%d",
ErrLoudnessSettingOutOfRange, minLeveledCacheMB, maxLeveledCacheMB)
}
return nil
}
func loudnessSettingsFromRow(row dbq.LoudnessSetting) LoudnessSettings {
return LoudnessSettings{
Enabled: row.Enabled,
BackfillConcurrency: row.BackfillConcurrency,
LeveledCacheMB: row.LeveledCacheMb,
UpdatedAt: row.UpdatedAt.Time,
}
}