From: Josh Bleecher Snyder Date: Mon, 14 Dec 2020 23:30:10 +0000 (-0800) Subject: device: use defer to simplify peer.NewTimer X-Git-Tag: 0.0.20210212~129 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea8fbb5927d462099103f42a124aeb9922bf3d0f;p=thirdparty%2Fwireguard-go.git device: use defer to simplify peer.NewTimer This also makes the lifetime of modifyingLock more prominent. Signed-off-by: Josh Bleecher Snyder --- diff --git a/device/timers.go b/device/timers.go index 0232eef..48cef94 100644 --- a/device/timers.go +++ b/device/timers.go @@ -29,18 +29,17 @@ func (peer *Peer) NewTimer(expirationFunction func(*Peer)) *Timer { timer := &Timer{} timer.Timer = time.AfterFunc(time.Hour, func() { timer.runningLock.Lock() + defer timer.runningLock.Unlock() timer.modifyingLock.Lock() if !timer.isPending { timer.modifyingLock.Unlock() - timer.runningLock.Unlock() return } timer.isPending = false timer.modifyingLock.Unlock() expirationFunction(peer) - timer.runningLock.Unlock() }) timer.Stop() return timer