Fix: ticker leak

This commit is contained in:
Dreamacro 2020-07-13 00:25:54 +08:00
parent 35449bfa17
commit e6aa452b51
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,27 +118,33 @@ 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 {
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
continue
}
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())
continue
}
if same {
log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
continue
}
if same {
log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
continue
}
log.Infoln("[Provider] %s's proxies update", f.Name())
if f.onUpdate != nil {
f.onUpdate(elm)
log.Infoln("[Provider] %s's proxies update", f.Name())
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