mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
refactor(fsnotifyext): handle Deduper timers in own goroutine, avoid mutex use
This commit is contained in:
committed by
Andrey Nering
parent
09f85844ba
commit
b4c8f5a0fe
@@ -2,7 +2,6 @@ package fsnotifyext
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
@@ -11,7 +10,6 @@ import (
|
||||
type Deduper struct {
|
||||
w *fsnotify.Watcher
|
||||
waitTime time.Duration
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func NewDeduper(w *fsnotify.Watcher, waitTime time.Duration) *Deduper {
|
||||
@@ -23,9 +21,9 @@ func NewDeduper(w *fsnotify.Watcher, waitTime time.Duration) *Deduper {
|
||||
|
||||
func (d *Deduper) GetChan() chan fsnotify.Event {
|
||||
channel := make(chan fsnotify.Event)
|
||||
timers := make(map[string]*time.Timer)
|
||||
|
||||
go func() {
|
||||
timers := make(map[string]*time.Timer)
|
||||
for {
|
||||
event, ok := <-d.w.Events
|
||||
switch {
|
||||
@@ -35,17 +33,11 @@ func (d *Deduper) GetChan() chan fsnotify.Event {
|
||||
continue
|
||||
}
|
||||
|
||||
d.mutex.Lock()
|
||||
timer, ok := timers[event.String()]
|
||||
d.mutex.Unlock()
|
||||
|
||||
if !ok {
|
||||
timer = time.AfterFunc(math.MaxInt64, func() { channel <- event })
|
||||
timer.Stop()
|
||||
|
||||
d.mutex.Lock()
|
||||
timers[event.String()] = timer
|
||||
d.mutex.Unlock()
|
||||
}
|
||||
|
||||
timer.Reset(d.waitTime)
|
||||
|
Reference in New Issue
Block a user