fix data race

This commit is contained in:
“bobbydigitales”
2024-10-08 00:12:33 -07:00
parent ac6846613a
commit 0183537c7e
2 changed files with 6 additions and 1 deletions

View File

@@ -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