From db02047268eb7d90d3cb40f698062d1d74a4de29 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 12 Mar 2023 02:19:07 +0400 Subject: [PATCH] Now lastPlayedCache's initial capacity is not hardcoded to 10. And it will be filled slowly, instead of initial read of last N songs from a playlist.log. --- internal/radio/icecast.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/radio/icecast.go b/internal/radio/icecast.go index 117787c..2ad69b9 100644 --- a/internal/radio/icecast.go +++ b/internal/radio/icecast.go @@ -22,7 +22,7 @@ const ( ) var ( - lastPlayedCache []Song = make([]Song, 10) + lastPlayedCache []Song lastPlayedCacheMutex sync.Mutex ) @@ -204,8 +204,15 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) { case mask := <-pw.changed: if mask&syscall.IN_MODIFY > 0 { lastPlayedCacheMutex.Lock() - if songs, err := icecastLastPlayedSongs(playlistPath, n); err == nil { - lastPlayedCache = songs + if lastPlayedCache == nil { + lastPlayedCache = make([]Song, n) + } + if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 { + if len(lastPlayedCache) == n { + lastPlayedCache = append(lastPlayedCache[1:], songs...) + } else { + lastPlayedCache = append(lastPlayedCache, songs...) + } } lastPlayedCacheMutex.Unlock() } else if mask&syscall.IN_IGNORED > 0 {