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 vehicle Vehicle
updatedAt *time.Time updatedAt *time.Time
ticker *time.Ticker ticker *time.Ticker
done chan struct{}
hash [16]byte hash [16]byte
parser parser parser parser
onUpdate func(interface{}) onUpdate func(interface{})
@ -117,27 +118,33 @@ func (f *fetcher) Update() (interface{}, bool, error) {
func (f *fetcher) Destroy() error { func (f *fetcher) Destroy() error {
if f.ticker != nil { if f.ticker != nil {
f.ticker.Stop() f.done <- struct{}{}
} }
return nil return nil
} }
func (f *fetcher) pullLoop() { func (f *fetcher) pullLoop() {
for range f.ticker.C { for {
elm, same, err := f.Update() select {
if err != nil { case <-f.ticker.C:
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) elm, same, err := f.Update()
continue if err != nil {
} log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
continue
}
if same { if same {
log.Debugln("[Provider] %s's proxies doesn't change", f.Name()) log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
continue continue
} }
log.Infoln("[Provider] %s's proxies update", f.Name()) log.Infoln("[Provider] %s's proxies update", f.Name())
if f.onUpdate != nil { if f.onUpdate != nil {
f.onUpdate(elm) 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, ticker: ticker,
vehicle: vehicle, vehicle: vehicle,
parser: parser, parser: parser,
done: make(chan struct{}, 1),
onUpdate: onUpdate, onUpdate: onUpdate,
} }
} }

View file

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

View file

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