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 (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
@@ -11,7 +10,6 @@ import (
|
|||||||
type Deduper struct {
|
type Deduper struct {
|
||||||
w *fsnotify.Watcher
|
w *fsnotify.Watcher
|
||||||
waitTime time.Duration
|
waitTime time.Duration
|
||||||
mutex sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDeduper(w *fsnotify.Watcher, waitTime time.Duration) *Deduper {
|
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 {
|
func (d *Deduper) GetChan() chan fsnotify.Event {
|
||||||
channel := make(chan fsnotify.Event)
|
channel := make(chan fsnotify.Event)
|
||||||
timers := make(map[string]*time.Timer)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
timers := make(map[string]*time.Timer)
|
||||||
for {
|
for {
|
||||||
event, ok := <-d.w.Events
|
event, ok := <-d.w.Events
|
||||||
switch {
|
switch {
|
||||||
@@ -35,17 +33,11 @@ func (d *Deduper) GetChan() chan fsnotify.Event {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
d.mutex.Lock()
|
|
||||||
timer, ok := timers[event.String()]
|
timer, ok := timers[event.String()]
|
||||||
d.mutex.Unlock()
|
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
timer = time.AfterFunc(math.MaxInt64, func() { channel <- event })
|
timer = time.AfterFunc(math.MaxInt64, func() { channel <- event })
|
||||||
timer.Stop()
|
timer.Stop()
|
||||||
|
|
||||||
d.mutex.Lock()
|
|
||||||
timers[event.String()] = timer
|
timers[event.String()] = timer
|
||||||
d.mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timer.Reset(d.waitTime)
|
timer.Reset(d.waitTime)
|
||||||
|
Reference in New Issue
Block a user