Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb94af0

Browse files
josharianzx2c4
authored andcommitted
device: use defer to simplify peer.NewTimer
This also makes the lifetime of modifyingLock more prominent. Signed-off-by: Josh Bleecher Snyder <[email protected]>
1 parent e950ce7 commit cb94af0

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

device/timers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,17 @@ func (peer *Peer) NewTimer(expirationFunction func(*Peer)) *Timer {
2929
timer := &Timer{}
3030
timer.Timer = time.AfterFunc(time.Hour, func() {
3131
timer.runningLock.Lock()
32+
defer timer.runningLock.Unlock()
3233

3334
timer.modifyingLock.Lock()
3435
if !timer.isPending {
3536
timer.modifyingLock.Unlock()
36-
timer.runningLock.Unlock()
3737
return
3838
}
3939
timer.isPending = false
4040
timer.modifyingLock.Unlock()
4141

4242
expirationFunction(peer)
43-
timer.runningLock.Unlock()
4443
})
4544
timer.Stop()
4645
return timer

0 commit comments

Comments
 (0)