From 94804f5b97ee26cdb6443d566d6649cab54e668e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbobbydigitales=E2=80=9D?= Date: Tue, 8 Oct 2024 00:12:33 -0700 Subject: [PATCH] fix data race --- main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 02fe8b4..4552dcd 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ import ( var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { origin := r.Header.Get("Origin") - return origin == "https://ddlion.net" || origin == "https://ddln.app" + return origin == "https://ddlion.net" || origin == "https://ddln.app" || origin == "https://localhost:6789" }, } @@ -68,6 +68,7 @@ type Peer struct { conn *websocket.Conn send chan []byte lastActive time.Time + m sync.Mutex closeOnce sync.Once } @@ -118,7 +119,9 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) { break } + peer.m.Lock() peer.lastActive = time.Now() + peer.m.Unlock() // fmt.Println("ws<-", connectionPeers[conn], ":", string(message[:min(80, len(message))])) @@ -413,9 +416,11 @@ func main() { // Collect inactive peers var inactivePeers []string for peerID, peer := range peerConnections { + peer.m.Lock() if now.Sub(peer.lastActive) > 60*time.Second { inactivePeers = append(inactivePeers, peerID) } + peer.m.Unlock() } // Remove inactive peers