diff --git a/device/peer.go b/device/peer.go index 623104622..3b84c02e2 100644 --- a/device/peer.go +++ b/device/peer.go @@ -53,7 +53,8 @@ type Peer struct { inbound *autodrainingInboundQueue // sequential ordering of tun writing } - trick bool + trick bool + stopCh chan int cookieGenerator CookieGenerator trieEntries list.List @@ -79,7 +80,7 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) { // create peer peer := new(Peer) - + peer.stopCh = make(chan int, 1) peer.trick = true peer.cookieGenerator.Init(pk) peer.device = device @@ -267,6 +268,10 @@ func (peer *Peer) Stop() { return } + select { + case peer.stopCh <- 1: + default: + } peer.device.log.Verbosef("%v - Stopping", peer) peer.timersStop() diff --git a/device/send.go b/device/send.go index b1ebe0e63..05479419d 100644 --- a/device/send.go +++ b/device/send.go @@ -105,9 +105,12 @@ func (peer *Peer) sendRandomPackets() { return } - if i < numPackets-1 { + if i < numPackets-1 && peer.isRunning.Load() && !peer.device.isClosed() { + select { + case <-peer.stopCh: // Wait for a random duration between 20 and 250 milliseconds - time.Sleep(time.Duration(randomInt(20, 250)) * time.Millisecond) + case <-time.After(time.Duration(randomInt(20, 250)) * time.Millisecond): + } } } }