The first public release of Fabled Minstrel #158

Merged
bvandeusen merged 256 commits from dev into main 2026-12-03 21:41:58 -05:00
2 changed files with 71 additions and 65 deletions
Showing only changes of commit 4ca6c6be38 - Show all commits
+49 -65
View File
@@ -7,6 +7,7 @@ package recommendation
import (
"context"
"fmt"
"runtime/debug"
"sync"
"github.com/jackc/pgx/v5/pgtype"
@@ -56,7 +57,7 @@ type HomePayload struct {
YouMightLikeArtists []dbq.ListYouMightLikeArtistsForUserRow
}
// HomeData runs five queries in parallel and assembles the payload.
// HomeData loads the page's sections in parallel and assembles the payload.
// Rediscover sections fan out to inner-limit results (track-derived +
// explicit-like eligibility, plus a daily-stable random fallback when
// the eligibility query is sparse), then the Go layer trims to
@@ -90,78 +91,61 @@ func HomeData(ctx context.Context, pool *pgxpool.Pool, userID pgtype.UUID) (*Hom
YouMightLikeArtists: []dbq.ListYouMightLikeArtistsForUserRow{},
}
wg.Add(7)
go func() {
defer wg.Done()
rows, err := q.ListRecentlyAddedAlbumsWithArtist(ctx, HomeRecentlyAddedLimit)
if err != nil {
fail("recently_added", err)
return
}
out.RecentlyAddedAlbums = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListMostPlayedTracksForUser(ctx, dbq.ListMostPlayedTracksForUserParams{
// section loads one part of the page in its own goroutine. A panic in
// one becomes that section's error, so the call fails the way a query
// error fails it: left unrecovered off the request's goroutine, it
// would stop the whole server, which the HTTP recoverer can't catch.
section := func(stage string, load func() error) {
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
fail(stage, fmt.Errorf("panicked: %v\n%s", r, debug.Stack()))
}
}()
if err := load(); err != nil {
fail(stage, err)
}
}()
}
section("recently_added", func() (err error) {
out.RecentlyAddedAlbums, err = q.ListRecentlyAddedAlbumsWithArtist(ctx, HomeRecentlyAddedLimit)
return err
})
section("most_played", func() (err error) {
out.MostPlayedTracks, err = q.ListMostPlayedTracksForUser(ctx, dbq.ListMostPlayedTracksForUserParams{
UserID: userID, Limit: HomeMostPlayedLimit,
})
if err != nil {
fail("most_played", err)
return
}
out.MostPlayedTracks = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListLastPlayedArtistsForUser(ctx, dbq.ListLastPlayedArtistsForUserParams{
return err
})
section("last_played", func() (err error) {
out.LastPlayedArtists, err = q.ListLastPlayedArtistsForUser(ctx, dbq.ListLastPlayedArtistsForUserParams{
UserID: userID, Limit: HomeLastPlayedLimit,
})
if err != nil {
fail("last_played", err)
return
}
out.LastPlayedArtists = rows
}()
go func() {
defer wg.Done()
rows, err := loadRediscoverAlbums(ctx, q, userID)
if err != nil {
fail("rediscover_albums", err)
return
}
out.RediscoverAlbums = rows
}()
go func() {
defer wg.Done()
rows, err := loadRediscoverArtists(ctx, q, userID)
if err != nil {
fail("rediscover_artists", err)
return
}
out.RediscoverArtists = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListYouMightLikeAlbumsForUser(ctx, dbq.ListYouMightLikeAlbumsForUserParams{
return err
})
section("rediscover_albums", func() (err error) {
out.RediscoverAlbums, err = loadRediscoverAlbums(ctx, q, userID)
return err
})
section("rediscover_artists", func() (err error) {
out.RediscoverArtists, err = loadRediscoverArtists(ctx, q, userID)
return err
})
section("you_might_like_albums", func() (err error) {
out.YouMightLikeAlbums, err = q.ListYouMightLikeAlbumsForUser(ctx, dbq.ListYouMightLikeAlbumsForUserParams{
UserID: userID, Limit: youMightLikeFetch,
})
if err != nil {
fail("you_might_like_albums", err)
return
}
out.YouMightLikeAlbums = rows
}()
go func() {
defer wg.Done()
rows, err := q.ListYouMightLikeArtistsForUser(ctx, dbq.ListYouMightLikeArtistsForUserParams{
return err
})
section("you_might_like_artists", func() (err error) {
out.YouMightLikeArtists, err = q.ListYouMightLikeArtistsForUser(ctx, dbq.ListYouMightLikeArtistsForUserParams{
UserID: userID, Limit: youMightLikeFetch,
})
if err != nil {
fail("you_might_like_artists", err)
return
}
out.YouMightLikeArtists = rows
}()
return err
})
wg.Wait()
if firstErr != nil {
+22
View File
@@ -0,0 +1,22 @@
package recommendation
import (
"context"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgtype"
)
// A section that panics fails the call with its stage named, as a query
// error does, rather than stopping the server from a goroutine nobody
// recovers. With no pool, every section's first query panics.
func TestHomeData_APanickingSectionIsAnError(t *testing.T) {
_, err := HomeData(context.Background(), nil, pgtype.UUID{Valid: true})
if err == nil {
t.Fatal("HomeData with no pool: no error, want the panic as one")
}
if !strings.Contains(err.Error(), "panicked") {
t.Errorf("err = %v, want it to say the section panicked", err)
}
}