mirror of
https://github.com/gohugoio/hugo.git
synced 2025-02-18 01:51:06 +00:00
watcher: use time.NewTicker to prevent leaks
Replace time.Tick with time.NewTicker.
This commit is contained in:
parent
873be9f90a
commit
02ab77da3e
1 changed files with 6 additions and 8 deletions
|
@ -23,7 +23,7 @@ import (
|
||||||
// Batcher batches file watch events in a given interval.
|
// Batcher batches file watch events in a given interval.
|
||||||
type Batcher struct {
|
type Batcher struct {
|
||||||
filenotify.FileWatcher
|
filenotify.FileWatcher
|
||||||
interval time.Duration
|
ticker *time.Ticker
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
|
||||||
Events chan []fsnotify.Event // Events are returned on this channel
|
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 := &Batcher{}
|
||||||
batcher.FileWatcher = watcher
|
batcher.FileWatcher = watcher
|
||||||
batcher.interval = intervalBatcher
|
batcher.ticker = time.NewTicker(intervalBatcher)
|
||||||
batcher.done = make(chan struct{}, 1)
|
batcher.done = make(chan struct{}, 1)
|
||||||
batcher.Events = make(chan []fsnotify.Event, 1)
|
batcher.Events = make(chan []fsnotify.Event, 1)
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
go batcher.run()
|
go batcher.run()
|
||||||
}
|
|
||||||
|
|
||||||
return batcher, nil
|
return batcher, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Batcher) run() {
|
func (b *Batcher) run() {
|
||||||
tick := time.Tick(b.interval)
|
|
||||||
evs := make([]fsnotify.Event, 0)
|
evs := make([]fsnotify.Event, 0)
|
||||||
OuterLoop:
|
OuterLoop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case ev := <-b.FileWatcher.Events():
|
case ev := <-b.FileWatcher.Events():
|
||||||
evs = append(evs, ev)
|
evs = append(evs, ev)
|
||||||
case <-tick:
|
case <-b.ticker.C:
|
||||||
if len(evs) == 0 {
|
if len(evs) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -84,4 +81,5 @@ OuterLoop:
|
||||||
func (b *Batcher) Close() {
|
func (b *Batcher) Close() {
|
||||||
b.done <- struct{}{}
|
b.done <- struct{}{}
|
||||||
b.FileWatcher.Close()
|
b.FileWatcher.Close()
|
||||||
|
b.ticker.Stop()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue