mirror of
https://git.zx2c4.com/wireguard-go
synced 2025-11-03 02:43:29 +08:00
device: fix data race in peer.timersActive
Found by the race detector and existing tests. To avoid introducing a lock into this hot path, calculate and cache whether any peers exist. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
@@ -49,8 +49,9 @@ type Device struct {
|
||||
}
|
||||
|
||||
peers struct {
|
||||
sync.RWMutex
|
||||
keyMap map[NoisePublicKey]*Peer
|
||||
empty AtomicBool // empty reports whether len(keyMap) == 0
|
||||
sync.RWMutex // protects keyMap
|
||||
keyMap map[NoisePublicKey]*Peer
|
||||
}
|
||||
|
||||
// unprotected / "self-synchronising resources"
|
||||
@@ -129,6 +130,7 @@ func unsafeRemovePeer(device *Device, peer *Peer, key NoisePublicKey) {
|
||||
// remove from peer map
|
||||
|
||||
delete(device.peers.keyMap, key)
|
||||
device.peers.empty.Set(len(device.peers.keyMap) == 0)
|
||||
}
|
||||
|
||||
func deviceUpdateState(device *Device) {
|
||||
|
||||
Reference in New Issue
Block a user