diff options
author | Oleksandr Redko <oleksandr.red+github@gmail.com> | 2023-03-01 15:26:05 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-03-08 10:16:34 +0100 |
commit | 02ab77da3e6af762b1eececc852fd851fc79b30b (patch) | |
tree | 96dbac6e2d98948e40d3b78c205744fb7e7861a3 | |
parent | 873be9f90aa0e23c1ec8cd99e9753742d9c4a36d (diff) |
watcher: use time.NewTicker to prevent leaks
Replace time.Tick with time.NewTicker.
-rw-r--r-- | watcher/batcher.go | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/watcher/batcher.go b/watcher/batcher.go index 718eea73f..f293552fe 100644 --- a/watcher/batcher.go +++ b/watcher/batcher.go @@ -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 } @@ -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 } @@ -84,4 +81,5 @@ OuterLoop: func (b *Batcher) Close() { b.done <- struct{}{} b.FileWatcher.Close() + b.ticker.Stop() } |