1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 15:02:19 +00:00

Lock counter map

This commit is contained in:
Yohann D'ANELLO 2020-11-09 18:11:42 +01:00
parent d1c4f81f4e
commit 7e4adb475a

View File

@ -4,23 +4,26 @@ package web
import ( import (
"bytes" "bytes"
"encoding/json" "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" "html/template"
"log" "log"
"net" "net"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "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 ( var (
// Precompile regex // Precompile regex
validPath = regexp.MustCompile("^/[a-z0-9@_-]*$") validPath = regexp.MustCompile("^/[a-z0-9@_-]*$")
counterMutex = new(sync.Mutex)
connectedClients = make(map[string]map[string]int64) 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. // 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... // Yeah, that's not a good way to have stats, but it works...
if connectedClients[name] == nil { if connectedClients[name] == nil {
counterMutex.Lock()
connectedClients[name] = make(map[string]int64) connectedClients[name] = make(map[string]int64)
counterMutex.Unlock()
} }
currentTime := time.Now().Unix() currentTime := time.Now().Unix()
if _, ok := r.URL.Query()["uid"]; ok { if _, ok := r.URL.Query()["uid"]; ok {
uid := r.URL.Query()["uid"][0] uid := r.URL.Query()["uid"][0]
counterMutex.Lock()
connectedClients[name][uid] = currentTime connectedClients[name][uid] = currentTime
counterMutex.Unlock()
} }
toDelete := make([]string, 0) toDelete := make([]string, 0)
counterMutex.Lock()
for uid, oldTime := range connectedClients[name] { for uid, oldTime := range connectedClients[name] {
if currentTime-oldTime > 40 { if currentTime-oldTime > 40 {
toDelete = append(toDelete, uid) toDelete = append(toDelete, uid)
@ -116,6 +124,7 @@ func statisticsHandler(w http.ResponseWriter, r *http.Request) {
for _, uid := range toDelete { for _, uid := range toDelete {
delete(connectedClients[name], uid) delete(connectedClients[name], uid)
} }
counterMutex.Unlock()
// Get requested stream // Get requested stream
stream, err := streams.Get(name) stream, err := streams.Get(name)