fix data race
This commit is contained in:
7
main.go
7
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
|
||||
|
||||
Reference in New Issue
Block a user