From 5290a106c4387ad6eda0be88d23f79edaac42607 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Thu, 8 Sep 2022 14:23:07 -0700 Subject: [PATCH] device: switch to regular mutex for keypair guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta TrieIPv4Peers100Addresses1000-32 83.5ns ± 0% 83.3ns ± 0% ~ (p=1.000 n=1+1) TrieIPv4Peers10Addresses10-32 33.6ns ± 0% 33.4ns ± 0% ~ (p=1.000 n=1+1) TrieIPv6Peers100Addresses1000-32 83.3ns ± 0% 83.2ns ± 0% ~ (p=1.000 n=1+1) TrieIPv6Peers10Addresses10-32 33.5ns ± 0% 33.2ns ± 0% ~ (p=1.000 n=1+1) Latency-32 216µs ± 0% 216µs ± 0% ~ (p=1.000 n=1+1) Throughput-32 2.31µs ± 0% 2.28µs ± 0% ~ (p=1.000 n=1+1) UAPIGet-32 2.28µs ± 0% 2.13µs ± 0% ~ (p=1.000 n=1+1) WaitPool-32 4.18µs ± 0% 4.14µs ± 0% ~ (p=1.000 n=1+1) name old packet-loss new packet-loss delta Throughput-32 0.00 ± 0% 0.01 ± 0% ~ (p=1.000 n=1+1) name old alloc/op new alloc/op delta UAPIGet-32 224B ± 0% 224B ± 0% ~ (all equal) name old allocs/op new allocs/op delta UAPIGet-32 17.0 ± 0% 17.0 ± 0% ~ (all equal) --- device/device.go | 6 ++---- device/keypair.go | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/device/device.go b/device/device.go index f96e27707..4820b8b18 100644 --- a/device/device.go +++ b/device/device.go @@ -392,10 +392,8 @@ func (device *Device) SendKeepalivesToPeersWithCurrentKeypair() { device.peers.RLock() for _, peer := range device.peers.keyMap { - peer.keypairs.RLock() - sendKeepalive := peer.keypairs.current != nil && !peer.keypairs.current.created.Add(RejectAfterTime).Before(time.Now()) - peer.keypairs.RUnlock() - if sendKeepalive { + current := peer.keypairs.Current() + if current.created.Add(RejectAfterTime).Before(time.Now()) { peer.SendKeepalive() } } diff --git a/device/keypair.go b/device/keypair.go index 2d5e86ef2..c130f5abc 100644 --- a/device/keypair.go +++ b/device/keypair.go @@ -33,15 +33,15 @@ type Keypair struct { } type Keypairs struct { - sync.RWMutex + sync.Mutex current *Keypair previous *Keypair next *Keypair } func (kp *Keypairs) Current() *Keypair { - kp.RLock() - defer kp.RUnlock() + kp.Lock() + defer kp.Unlock() return kp.current }