diff --git a/internal/radio/icecast.go b/internal/radio/icecast.go index 15bea43..880da96 100644 --- a/internal/radio/icecast.go +++ b/internal/radio/icecast.go @@ -80,42 +80,27 @@ func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) { } func IcecastLastPlayedSongs(lastNSongs int, playlistPath string) ([]Song, error) { - { - lastPlayedCacheMutex.Lock() - defer lastPlayedCacheMutex.Unlock() - if lpcLen := len(lastPlayedCache); lpcLen > 0 { - if lastNSongs > lpcLen { - lastNSongs = lpcLen - } - var ret []Song = make([]Song, lastNSongs) - copy(ret[:], lastPlayedCache[lpcLen-lastNSongs:]) - return ret, nil + lastPlayedCacheMutex.Lock() + defer lastPlayedCacheMutex.Unlock() + if lpcLen := len(lastPlayedCache); lpcLen > 0 { + if lastNSongs > lpcLen { + lastNSongs = lpcLen } + var ret []Song = make([]Song, lastNSongs) + copy(ret[:], lastPlayedCache[lpcLen-lastNSongs:]) + return ret, nil } - songs, err := icecastLastPlayedSongs(playlistPath, lastNSongs) - if err != nil { - return make([]Song, 0), err - } - - return songs, nil + return nil, nil } -func IcecastLastSong(playlistPath string) (Song, error) { - { - lastPlayedCacheMutex.Lock() - defer lastPlayedCacheMutex.Unlock() - if lpcLen := len(lastPlayedCache); lpcLen > 0 { - return lastPlayedCache[lpcLen-1], nil - } +func IcecastLastSong(playlistPath string) (*Song, error) { + lastPlayedCacheMutex.Lock() + defer lastPlayedCacheMutex.Unlock() + if lpcLen := len(lastPlayedCache); lpcLen > 0 { + return &lastPlayedCache[lpcLen-1], nil } - - song, err := icecastLastPlayedSongs(playlistPath, 1) - if err != nil { - return Song{}, err - } - - return song[0], nil + return nil, nil } func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) { @@ -206,20 +191,20 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) { if mask&syscall.IN_MODIFY > 0 { if isJustStarted { isJustStarted = false - break - } - lastPlayedCacheMutex.Lock() - 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...) + } else { + lastPlayedCacheMutex.Lock() + 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() } - lastPlayedCacheMutex.Unlock() } else if mask&syscall.IN_IGNORED > 0 { pw.Close() pw.Watch(playlistPath, n)