Skip to content

Commit

Permalink
device: get rid of peers.empty boolean in timersActive
Browse files Browse the repository at this point in the history
There's no way for len(peers)==0 when a current peer has
isRunning==false.

This requires some struct reshuffling so that the uint64 pointer is
aligned.

Signed-off-by: Jason A. Donenfeld <[email protected]>
  • Loading branch information
zx2c4 committed Feb 25, 2021
1 parent 3c11c03 commit 70b7b71
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
14 changes: 6 additions & 8 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,20 @@ type Device struct {
publicKey NoisePublicKey
}

rate struct {
underLoadUntil int64
limiter ratelimiter.Ratelimiter
}

peers struct {
empty AtomicBool // empty reports whether len(keyMap) == 0
sync.RWMutex // protects keyMap
sync.RWMutex // protects keyMap
keyMap map[NoisePublicKey]*Peer
}

allowedips AllowedIPs
indexTable IndexTable
cookieChecker CookieChecker

rate struct {
underLoadUntil int64
limiter ratelimiter.Ratelimiter
}

pool struct {
messageBuffers *WaitPool
inboundElements *WaitPool
Expand Down Expand Up @@ -135,7 +134,6 @@ func removePeerLocked(device *Device, peer *Peer, key NoisePublicKey) {

// remove from peer map
delete(device.peers.keyMap, key)
device.peers.empty.Set(len(device.peers.keyMap) == 0)
}

// changeState attempts to change the device state to match want.
Expand Down
1 change: 0 additions & 1 deletion device/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {

// add
device.peers.keyMap[pk] = peer
device.peers.empty.Set(false)

// start peer
peer.timersInit()
Expand Down
2 changes: 1 addition & 1 deletion device/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (timer *Timer) IsPending() bool {
}

func (peer *Peer) timersActive() bool {
return peer.isRunning.Get() && peer.device != nil && peer.device.isUp() && !peer.device.peers.empty.Get()
return peer.isRunning.Get() && peer.device != nil && peer.device.isUp()
}

func expiredRetransmitHandshake(peer *Peer) {
Expand Down

0 comments on commit 70b7b71

Please sign in to comment.