diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index 639b101..7d8ac30 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -123,6 +123,8 @@ func main() { } return case syscall.SIGHUP: + plylst.Lock() + defer plylst.Unlock() if err := plylst.Reload(); err != nil { log.Println(err) } diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go index 575af65..9e1347d 100644 --- a/internal/http/dj_handlers.go +++ b/internal/http/dj_handlers.go @@ -69,7 +69,9 @@ const defaultTitleTagNoArtist = "[And no title tag either! Pffft]" func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { w.Header().Add("Content-Type", "text/plain") + dj.playlist.Lock() nxt := dj.playlist.Next() + dj.playlist.Unlock() if nxt == "" { log.Println("the end of a playlist has been reached") if nxt = dj.fallbackSong; nxt == "" { diff --git a/internal/radio/playlist.go b/internal/radio/playlist.go index 669e8f4..2721b5a 100644 --- a/internal/radio/playlist.go +++ b/internal/radio/playlist.go @@ -10,7 +10,7 @@ import ( // Playlist holds a list of paths to a song files. type Playlist struct { - mutex sync.Mutex + sync.Mutex filePath string playlist []string cur int @@ -27,8 +27,6 @@ func NewPlaylist(filePath string, repeat bool) (*Playlist, error) { // Next returns the next song to play. Returns an empty string if repeat is // false and the end of a playlist was reached. func (p *Playlist) Next() (song string) { - p.mutex.Lock() - defer p.mutex.Unlock() if p.cur == len(p.playlist) { // If the end of a playlist was reached and repeat is set to true, // then go back to the head of it, thus repeating it. Return an empty @@ -55,10 +53,8 @@ func (p *Playlist) load() error { return errors.New("a playlist file is empty. Did not update") } - p.mutex.Lock() p.playlist = strings.Split(string(data), "\n") p.cur = 0 - p.mutex.Unlock() return nil }