perf(S01/T01): Add token-bucket rate limiter (1 req/sec), SQLite respon…

- backend/explore/ratelimiter.go
- backend/explore/cache.go
- backend/database/sql/schemas/explore_cache.sql
- backend/database/database.go
This commit is contained in:
2026-03-23 07:55:25 -04:00
parent 398fd5aaae
commit 8fc075c24a
9 changed files with 652 additions and 1 deletions
+73 -1
View File
@@ -349,6 +349,16 @@ func runMigrations(
}
}
// Migration 11: explore_cache table for MusicBrainz/ListenBrainz
// API response caching with TTL expiry and MBID lookups.
if version < 11 {
if err := migration11ExploreCache(
ctx, db, logger,
); err != nil {
return err
}
}
return nil
}
@@ -1222,7 +1232,7 @@ func migration9SmartPlaylists(
// migration10PlayHistory adds play history tracking:
// - play_history table for timestamped play log
// - play_count and last_played columns on audio_files
// - Recreates track_metadata VIEW to expose the new columns
// - Recreates track_metadata VIEW to expose the new columns.
func migration10PlayHistory(
ctx context.Context,
db *sql.DB,
@@ -1352,6 +1362,68 @@ func migration10PlayHistory(
return nil
}
// migration11ExploreCache creates the explore_cache table for
// MusicBrainz and ListenBrainz API response caching. The table
// stores raw JSON keyed by URL with TTL-based expiry and optional
// MBID columns for future autotagging lookups.
func migration11ExploreCache(
ctx context.Context,
db *sql.DB,
logger *slog.Logger,
) error {
logger.Info(
"applying migration 11: explore_cache table",
)
if _, err := db.ExecContext(ctx, `
CREATE TABLE IF NOT EXISTS explore_cache (
url_key TEXT PRIMARY KEY,
response TEXT NOT NULL,
mbid TEXT,
entity_type TEXT,
expires_at DATETIME NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`); err != nil {
return fmt.Errorf(
"migration 11: could not create explore_cache table: %w",
err,
)
}
if _, err := db.ExecContext(ctx, `
CREATE INDEX IF NOT EXISTS idx_explore_cache_expires
ON explore_cache(expires_at)
`); err != nil {
return fmt.Errorf(
"migration 11: could not create expires index: %w",
err,
)
}
if _, err := db.ExecContext(ctx, `
CREATE INDEX IF NOT EXISTS idx_explore_cache_mbid
ON explore_cache(mbid)
`); err != nil {
return fmt.Errorf(
"migration 11: could not create mbid index: %w",
err,
)
}
if _, err := db.ExecContext(
ctx, "PRAGMA user_version = 11",
); err != nil {
return fmt.Errorf(
"could not set user_version to 11: %w", err,
)
}
logger.Info("migration 11 complete")
return nil
}
// readLibraryDirFromTOML reads the TOML config file and returns
// the Library.DirectoryPath value, or "" if not configured.
func readLibraryDirFromTOML(logger *slog.Logger) string {