From 7e4adb475a9ce56490b6a59315709284a35ec2e8 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 9 Nov 2020 18:11:42 +0100 Subject: [PATCH] Lock counter map --- web/handler.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/web/handler.go b/web/handler.go index 9b5b1dd..2a299c9 100644 --- a/web/handler.go +++ b/web/handler.go @@ -4,23 +4,26 @@ package web import ( "bytes" "encoding/json" - "github.com/markbates/pkger" - "gitlab.crans.org/nounous/ghostream/internal/monitoring" - "gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine" - "gitlab.crans.org/nounous/ghostream/stream/webrtc" "html/template" "log" "net" "net/http" "regexp" "strings" + "sync" "time" + + "github.com/markbates/pkger" + "gitlab.crans.org/nounous/ghostream/internal/monitoring" + "gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine" + "gitlab.crans.org/nounous/ghostream/stream/webrtc" ) var ( // Precompile regex validPath = regexp.MustCompile("^/[a-z0-9@_-]*$") + counterMutex = new(sync.Mutex) connectedClients = make(map[string]map[string]int64) ) @@ -100,14 +103,19 @@ func statisticsHandler(w http.ResponseWriter, r *http.Request) { // Each time the client connects to this page, the identifier is renewed. // Yeah, that's not a good way to have stats, but it works... if connectedClients[name] == nil { + counterMutex.Lock() connectedClients[name] = make(map[string]int64) + counterMutex.Unlock() } currentTime := time.Now().Unix() if _, ok := r.URL.Query()["uid"]; ok { uid := r.URL.Query()["uid"][0] + counterMutex.Lock() connectedClients[name][uid] = currentTime + counterMutex.Unlock() } toDelete := make([]string, 0) + counterMutex.Lock() for uid, oldTime := range connectedClients[name] { if currentTime-oldTime > 40 { toDelete = append(toDelete, uid) @@ -116,6 +124,7 @@ func statisticsHandler(w http.ResponseWriter, r *http.Request) { for _, uid := range toDelete { delete(connectedClients[name], uid) } + counterMutex.Unlock() // Get requested stream stream, err := streams.Get(name)