TUN status hack was causing spam during shutdown

This commit is contained in:
Mathias Hall-Andersen
2018-02-11 23:26:54 +01:00
parent 04ded4c631
commit 6cba91999c
4 changed files with 39 additions and 36 deletions

View File

@@ -1,44 +1,52 @@
package main
import (
"sync"
"time"
)
type Timer struct {
pending AtomicBool
mutex sync.Mutex
pending bool
timer *time.Timer
}
/* Starts the timer if not already pending
*/
func (t *Timer) Start(dur time.Duration) bool {
if !t.pending.Swap(true) {
t.mutex.Lock()
defer t.mutex.Unlock()
started := !t.pending
if started {
t.timer.Reset(dur)
return true
}
return false
return started
}
/* Stops the timer
*/
func (t *Timer) Stop() {
if t.pending.Swap(true) {
t.timer.Stop()
select {
case <-t.timer.C:
default:
}
t.mutex.Lock()
defer t.mutex.Unlock()
t.timer.Stop()
select {
case <-t.timer.C:
default:
}
t.pending.Set(false)
t.pending = false
}
func (t *Timer) Pending() bool {
return t.pending.Get()
t.mutex.Lock()
defer t.mutex.Unlock()
return t.pending
}
func (t *Timer) Reset(dur time.Duration) {
t.pending.Set(false)
t.Start(dur)
t.mutex.Lock()
defer t.mutex.Unlock()
t.timer.Reset(dur)
}
func (t *Timer) Wait() <-chan time.Time {
@@ -46,8 +54,8 @@ func (t *Timer) Wait() <-chan time.Time {
}
func NewTimer() (t Timer) {
t.pending.Set(false)
t.timer = time.NewTimer(0)
t.pending = false
t.timer = time.NewTimer(time.Hour)
t.timer.Stop()
select {
case <-t.timer.C: