Fix: ticker leak

This commit is contained in:
gVisor bot 2020-07-13 00:25:54 +08:00
parent 1ab56b1afb
commit 22f32c3789
3 changed files with 24 additions and 14 deletions

View file

@ -23,6 +23,7 @@ type fetcher struct {
vehicle Vehicle
updatedAt *time.Time
ticker *time.Ticker
done chan struct{}
hash [16]byte
parser parser
onUpdate func(interface{})
@ -117,13 +118,15 @@ func (f *fetcher) Update() (interface{}, bool, error) {
func (f *fetcher) Destroy() error {
if f.ticker != nil {
f.ticker.Stop()
f.done <- struct{}{}
}
return nil
}
func (f *fetcher) pullLoop() {
for range f.ticker.C {
for {
select {
case <-f.ticker.C:
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
@ -139,6 +142,10 @@ func (f *fetcher) pullLoop() {
if f.onUpdate != nil {
f.onUpdate(elm)
}
case <-f.done:
f.ticker.Stop()
return
}
}
}
@ -165,6 +172,7 @@ func newFetcher(name string, interval time.Duration, vehicle Vehicle, parser par
ticker: ticker,
vehicle: vehicle,
parser: parser,
done: make(chan struct{}, 1),
onUpdate: onUpdate,
}
}

View file

@ -63,6 +63,7 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
}
tick := time.NewTicker(time.Millisecond * time.Duration(interval))
defer tick.Stop()
for range tick.C {
if err := sendSnapshot(); err != nil {
break

View file

@ -142,6 +142,7 @@ func traffic(w http.ResponseWriter, r *http.Request) {
}
tick := time.NewTicker(time.Second)
defer tick.Stop()
t := T.DefaultManager
buf := &bytes.Buffer{}
var err error