Skip to content

Commit

Permalink
ircbot: change .random to use more optimized storage queries
Browse files Browse the repository at this point in the history
either a bare `.random` or a `.random <nick>` where <nick> 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
  • Loading branch information
Wessie committed Dec 24, 2024
1 parent 36e2fa3 commit 6399b4a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ircbot/commands_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 6399b4a

Please sign in to comment.