aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-06-01 14:30:10 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2023-06-01 14:30:10 +0200
commit434601366f09fb47346171c1dff05b4e9f1bd484 (patch)
tree670429ca2155d6da11212b75ed68db008b9e7313
parentdc2942dfeebfdc768a02730681a3c9a798722cfc (diff)
downloadWRMS-434601366f09fb47346171c1dff05b4e9f1bd484.tar.gz
WRMS-434601366f09fb47346171c1dff05b4e9f1bd484.zip
take the lock when adding or modifying songs
-rw-r--r--wrms.go19
1 files changed, 14 insertions, 5 deletions
diff --git a/wrms.go b/wrms.go
index a7cac19..f56711a 100644
--- a/wrms.go
+++ b/wrms.go
@@ -252,16 +252,16 @@ func (wrms *Wrms) Broadcast(ev Event) {
})
}
-func (wrms *Wrms) startPlaying() {
- currentSong := wrms.CurrentSong.Load()
- wrms.Broadcast(newEvent("play", []Song{*currentSong}))
-}
-
func (wrms *Wrms) AddSong(song Song) {
+ wrms.rwlock.Lock()
+
startPlayingAgain := wrms.Playing && wrms.CurrentSong.Load() == nil
wrms.Songs = append(wrms.Songs, song)
s := &wrms.Songs[len(wrms.Songs)-1]
wrms.queue.Add(s)
+
+ wrms.rwlock.Unlock()
+
llog.Info("Added song %s (ptr=%p) to Songs", s.Uri, s)
wrms.Broadcast(newEvent("add", []Song{song}))
@@ -271,6 +271,8 @@ func (wrms *Wrms) AddSong(song Song) {
}
func (wrms *Wrms) DeleteSong(songUri string) {
+ wrms.rwlock.Lock()
+
for i := 0; i < len(wrms.Songs); i++ {
s := &wrms.Songs[i]
if s.Uri != songUri {
@@ -281,6 +283,9 @@ func (wrms *Wrms) DeleteSong(songUri string) {
wrms.Songs = wrms.Songs[:len(wrms.Songs)-1]
wrms.queue.RemoveSong(s)
+
+ wrms.rwlock.Unlock()
+
wrms.Broadcast(newEvent("delete", []Song{*s}))
break
}
@@ -340,6 +345,8 @@ func (wrms *Wrms) PlayPause() {
}
func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string) {
+ wrms.rwlock.Lock()
+
for i := 0; i < len(wrms.Songs); i++ {
s := &wrms.Songs[i]
if s.Uri != songUri {
@@ -397,6 +404,8 @@ func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string
}
wrms.queue.Adjust(s)
+ wrms.rwlock.Unlock()
+
wrms.Broadcast(newEvent("update", []Song{*s}))
break
}