0

Fix possible crash for concurrent map writes

This commit is contained in:
Gabe Kangas 2020-07-22 23:09:11 -07:00
parent 515661804a
commit 43df6c432e

View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"math"
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
@ -18,6 +19,8 @@ const (
statsFilePath = "stats.json"
)
var l = sync.Mutex{}
func setupStats() error {
s, err := getSavedStats()
if err != nil {
@ -82,7 +85,9 @@ func SetClientActive(clientID string) {
// fmt.Println("Marking client active:", clientID, s.GetViewerCount()+1, "clients connected.")
// }
l.Lock()
_stats.Clients[clientID] = time.Now()
l.Unlock()
_stats.SessionMaxViewerCount = int(math.Max(float64(len(_stats.Clients)), float64(_stats.SessionMaxViewerCount)))
_stats.OverallMaxViewerCount = int(math.Max(float64(_stats.SessionMaxViewerCount), float64(_stats.OverallMaxViewerCount)))
}