Skip to content

Commit 657e769

Browse files
committed
device: remove atomic pointer to next and rely on lock
name old time/op new time/op delta TrieIPv4Peers100Addresses1000-32 83.5ns ± 0% 89.0ns ± 0% ~ (p=1.000 n=1+1) TrieIPv4Peers10Addresses10-32 33.6ns ± 0% 33.3ns ± 0% ~ (p=1.000 n=1+1) TrieIPv6Peers100Addresses1000-32 83.3ns ± 0% 82.7ns ± 0% ~ (p=1.000 n=1+1) TrieIPv6Peers10Addresses10-32 33.5ns ± 0% 33.4ns ± 0% ~ (p=1.000 n=1+1) Latency-32 216µs ± 0% 211µs ± 0% ~ (p=1.000 n=1+1) Throughput-32 2.31µs ± 0% 2.25µs ± 0% ~ (p=1.000 n=1+1) UAPIGet-32 2.28µs ± 0% 2.12µs ± 0% ~ (p=1.000 n=1+1) WaitPool-32 4.18µs ± 0% 4.06µs ± 0% ~ (p=1.000 n=1+1) name old packet-loss new packet-loss delta Throughput-32 0.00 ± 0% 0.00 ± 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)
1 parent b51010b commit 657e769

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

device/keypair.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Keypairs struct {
3636
sync.RWMutex
3737
current *Keypair
3838
previous *Keypair
39-
next atomic.Pointer[Keypair]
39+
next *Keypair
4040
}
4141

4242
func (kp *Keypairs) Current() *Keypair {

device/noise-protocol.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,12 @@ func (peer *Peer) BeginSymmetricSession() error {
581581
defer keypairs.Unlock()
582582

583583
previous := keypairs.previous
584-
next := keypairs.next.Load()
584+
next := keypairs.next
585585
current := keypairs.current
586586

587587
if isInitiator {
588588
if next != nil {
589-
keypairs.next.Store(nil)
589+
keypairs.next = nil
590590
keypairs.previous = next
591591
device.DeleteKeypair(current)
592592
} else {
@@ -595,7 +595,7 @@ func (peer *Peer) BeginSymmetricSession() error {
595595
device.DeleteKeypair(previous)
596596
keypairs.current = keypair
597597
} else {
598-
keypairs.next.Store(keypair)
598+
keypairs.next = keypair
599599
device.DeleteKeypair(next)
600600
keypairs.previous = nil
601601
device.DeleteKeypair(previous)
@@ -607,18 +607,15 @@ func (peer *Peer) BeginSymmetricSession() error {
607607
func (peer *Peer) ReceivedWithKeypair(receivedKeypair *Keypair) bool {
608608
keypairs := &peer.keypairs
609609

610-
if keypairs.next.Load() != receivedKeypair {
611-
return false
612-
}
613610
keypairs.Lock()
614611
defer keypairs.Unlock()
615-
if keypairs.next.Load() != receivedKeypair {
612+
if keypairs.next != receivedKeypair {
616613
return false
617614
}
618615
old := keypairs.previous
619616
keypairs.previous = keypairs.current
620617
peer.device.DeleteKeypair(old)
621-
keypairs.current = keypairs.next.Load()
622-
keypairs.next.Store(nil)
618+
keypairs.current = keypairs.next
619+
keypairs.next = nil
623620
return true
624621
}

device/noise_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestNoiseHandshake(t *testing.T) {
148148
t.Fatal("failed to derive keypair for peer 2", err)
149149
}
150150

151-
key1 := peer1.keypairs.next.Load()
151+
key1 := peer1.keypairs.next
152152
key2 := peer2.keypairs.current
153153

154154
// encrypting / decryption test

device/peer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ func (peer *Peer) ZeroAndFlushAll() {
202202
keypairs.Lock()
203203
device.DeleteKeypair(keypairs.previous)
204204
device.DeleteKeypair(keypairs.current)
205-
device.DeleteKeypair(keypairs.next.Load())
205+
device.DeleteKeypair(keypairs.next)
206206
keypairs.previous = nil
207207
keypairs.current = nil
208-
keypairs.next.Store(nil)
208+
keypairs.next = nil
209209
keypairs.Unlock()
210210

211211
// clear handshake state
@@ -232,8 +232,8 @@ func (peer *Peer) ExpireCurrentKeypairs() {
232232
if keypairs.current != nil {
233233
keypairs.current.sendNonce.Store(RejectAfterMessages)
234234
}
235-
if next := keypairs.next.Load(); next != nil {
236-
next.sendNonce.Store(RejectAfterMessages)
235+
if keypairs.next != nil {
236+
keypairs.next.sendNonce.Store(RejectAfterMessages)
237237
}
238238
keypairs.Unlock()
239239
}

0 commit comments

Comments
 (0)