Fix possible crash for concurrent map writes
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -18,6 +19,8 @@ const (
|
|||||||
statsFilePath = "stats.json"
|
statsFilePath = "stats.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var l = sync.Mutex{}
|
||||||
|
|
||||||
func setupStats() error {
|
func setupStats() error {
|
||||||
s, err := getSavedStats()
|
s, err := getSavedStats()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -82,7 +85,9 @@ func SetClientActive(clientID string) {
|
|||||||
// fmt.Println("Marking client active:", clientID, s.GetViewerCount()+1, "clients connected.")
|
// fmt.Println("Marking client active:", clientID, s.GetViewerCount()+1, "clients connected.")
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
l.Lock()
|
||||||
_stats.Clients[clientID] = time.Now()
|
_stats.Clients[clientID] = time.Now()
|
||||||
|
l.Unlock()
|
||||||
_stats.SessionMaxViewerCount = int(math.Max(float64(len(_stats.Clients)), float64(_stats.SessionMaxViewerCount)))
|
_stats.SessionMaxViewerCount = int(math.Max(float64(len(_stats.Clients)), float64(_stats.SessionMaxViewerCount)))
|
||||||
_stats.OverallMaxViewerCount = int(math.Max(float64(_stats.SessionMaxViewerCount), float64(_stats.OverallMaxViewerCount)))
|
_stats.OverallMaxViewerCount = int(math.Max(float64(_stats.SessionMaxViewerCount), float64(_stats.OverallMaxViewerCount)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user