From 6399b4a0f0b271694db3a86412483cd865650df9 Mon Sep 17 00:00:00 2001 From: Wessie Date: Tue, 24 Dec 2024 21:38:45 +0100 Subject: [PATCH] ircbot: change .random to use more optimized storage queries either a bare `.random` or a `.random ` where had many favorites would hit the 5s command timeout and do nothing. this change tries to fix that problem by requesting a smaller subset from storage --- ircbot/commands_impl.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ircbot/commands_impl.go b/ircbot/commands_impl.go index 082f3b6c..55b1fada 100644 --- a/ircbot/commands_impl.go +++ b/ircbot/commands_impl.go @@ -450,6 +450,7 @@ func RandomTrackRequest(e Event) error { var songs []radio.Song var err error + var limit = 100 // we select random song from a specific set of songs, either: // - favorites of the caller @@ -468,7 +469,7 @@ func RandomTrackRequest(e Event) error { nickname = nick } - songs, err = e.Storage.Song(e.Ctx).FavoritesOfDatabase(nickname) + songs, err = e.Storage.Track(e.Ctx).RandomFavoriteOf(nickname, limit) if err != nil { return errors.E(op, err) } @@ -482,12 +483,17 @@ func RandomTrackRequest(e Event) error { songs = res.Songs } else { // purely random, just select from all tracks - songs, err = e.Storage.Track(e.Ctx).All() + songs, err = e.Storage.Track(e.Ctx).Random(limit) if err != nil { return errors.E(op, err) } } + if len(songs) == 0 { + e.Echo("no songs were found") + return nil + } + rand := config.NewRand(false) // select songs randomly of what we have @@ -521,7 +527,7 @@ func RandomTrackRequest(e Event) error { } } - e.Echo("None of the songs found could be requested") + e.Echo("none of the songs found could be requested") return nil }