Skip to content

Commit

Permalink
watcher: use time.NewTicker to prevent leaks
Browse files Browse the repository at this point in the history
Replace time.Tick with time.NewTicker.
  • Loading branch information
alexandear authored and bep committed Mar 8, 2023
1 parent 873be9f commit 02ab77d
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions watcher/batcher.go
Expand Up @@ -23,8 +23,8 @@ import (
// Batcher batches file watch events in a given interval.
type Batcher struct {
filenotify.FileWatcher
interval time.Duration
done chan struct{}
ticker *time.Ticker
done chan struct{}

Events chan []fsnotify.Event // Events are returned on this channel
}
Expand All @@ -48,26 +48,23 @@ func New(intervalBatcher, intervalPoll time.Duration, poll bool) (*Batcher, erro

batcher := &Batcher{}
batcher.FileWatcher = watcher
batcher.interval = intervalBatcher
batcher.ticker = time.NewTicker(intervalBatcher)
batcher.done = make(chan struct{}, 1)
batcher.Events = make(chan []fsnotify.Event, 1)

if err == nil {
go batcher.run()
}
go batcher.run()

return batcher, nil
}

func (b *Batcher) run() {
tick := time.Tick(b.interval)
evs := make([]fsnotify.Event, 0)
OuterLoop:
for {
select {
case ev := <-b.FileWatcher.Events():
evs = append(evs, ev)
case <-tick:
case <-b.ticker.C:
if len(evs) == 0 {
continue
}
Expand All @@ -84,4 +81,5 @@ OuterLoop:
func (b *Batcher) Close() {
b.done <- struct{}{}
b.FileWatcher.Close()
b.ticker.Stop()
}

0 comments on commit 02ab77d

Please sign in to comment.