fix(player): race condition 2 electric boogaloo
This commit is contained in:
+18
-10
@@ -42,7 +42,8 @@ func main() {
|
|||||||
|
|
||||||
var viewers = make(map[string]Viewer)
|
var viewers = make(map[string]Viewer)
|
||||||
var mirrorActivity = make(map[int]int)
|
var mirrorActivity = make(map[int]int)
|
||||||
var lock sync.Mutex
|
var lockViewers sync.Mutex
|
||||||
|
var lockMirrorActivity sync.Mutex
|
||||||
|
|
||||||
// Handle view counting
|
// Handle view counting
|
||||||
http.HandleFunc("/viewcount", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/viewcount", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -60,18 +61,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the last active time
|
// Update the last active time
|
||||||
lock.Lock()
|
lockViewers.Lock()
|
||||||
viewers[ip] = Viewer{
|
viewers[ip] = Viewer{
|
||||||
LastActive: now,
|
LastActive: now,
|
||||||
ActiveMirror: metric.ActiveMirror,
|
ActiveMirror: metric.ActiveMirror,
|
||||||
}
|
}
|
||||||
|
var viewersCount = len(viewers)
|
||||||
|
lockViewers.Unlock()
|
||||||
|
|
||||||
// Respond
|
// Respond
|
||||||
|
lockMirrorActivity.Lock()
|
||||||
var response = ViewCountResponse{
|
var response = ViewCountResponse{
|
||||||
TotalCount: len(viewers),
|
TotalCount: viewersCount,
|
||||||
ByMirror: mirrorActivity,
|
ByMirror: mirrorActivity,
|
||||||
}
|
}
|
||||||
lock.Unlock()
|
lockMirrorActivity.Unlock()
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@@ -88,13 +92,13 @@ func main() {
|
|||||||
var now = time.Now().Unix()
|
var now = time.Now().Unix()
|
||||||
|
|
||||||
// Remove any viewers that have not been active in the last 10 seconds
|
// Remove any viewers that have not been active in the last 10 seconds
|
||||||
lock.Lock()
|
lockViewers.Lock()
|
||||||
for ip, viewer := range viewers {
|
for ip, viewer := range viewers {
|
||||||
if now-viewer.LastActive > 10 {
|
if now-viewer.LastActive > 10 {
|
||||||
delete(viewers, ip)
|
delete(viewers, ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lock.Unlock()
|
lockViewers.Unlock()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -112,9 +116,9 @@ func main() {
|
|||||||
var last_count = 0
|
var last_count = 0
|
||||||
for {
|
for {
|
||||||
var now = time.Now().Format(time.RFC3339)
|
var now = time.Now().Format(time.RFC3339)
|
||||||
lock.Lock()
|
lockViewers.Lock()
|
||||||
var count = len(viewers)
|
var count = len(viewers)
|
||||||
lock.Unlock()
|
lockViewers.Unlock()
|
||||||
|
|
||||||
// If the viewer count has changed, log it
|
// If the viewer count has changed, log it
|
||||||
if count != last_count {
|
if count != last_count {
|
||||||
@@ -142,21 +146,23 @@ func main() {
|
|||||||
var counts = make(map[int]int)
|
var counts = make(map[int]int)
|
||||||
|
|
||||||
// Loop through all viewers
|
// Loop through all viewers
|
||||||
lock.Lock()
|
lockViewers.Lock()
|
||||||
for _, viewer := range viewers {
|
for _, viewer := range viewers {
|
||||||
// Increment the mirror count
|
// Increment the mirror count
|
||||||
counts[viewer.ActiveMirror]++
|
counts[viewer.ActiveMirror]++
|
||||||
}
|
}
|
||||||
lock.Unlock()
|
lockViewers.Unlock()
|
||||||
|
|
||||||
// Check if the counts have changed
|
// Check if the counts have changed
|
||||||
var changed = false
|
var changed = false
|
||||||
|
lockMirrorActivity.Lock()
|
||||||
for mirror, count := range counts {
|
for mirror, count := range counts {
|
||||||
if count != mirrorActivity[mirror] {
|
if count != mirrorActivity[mirror] {
|
||||||
changed = true
|
changed = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
lockMirrorActivity.Unlock()
|
||||||
|
|
||||||
// If the counts have changed, log them
|
// If the counts have changed, log them
|
||||||
if changed {
|
if changed {
|
||||||
@@ -166,7 +172,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the mirror activity
|
// Update the mirror activity
|
||||||
|
lockMirrorActivity.Lock()
|
||||||
mirrorActivity = counts
|
mirrorActivity = counts
|
||||||
|
lockMirrorActivity.Unlock()
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user