mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Add debounce logic for file events to prevent duplicate reloads
This commit is contained in:
@@ -39,6 +39,8 @@ type Watcher struct {
|
||||
clientsMutex sync.RWMutex
|
||||
reloadCallback func(map[string]interfaces.Client, *config.Config)
|
||||
watcher *fsnotify.Watcher
|
||||
eventTimes map[string]time.Time
|
||||
eventMutex sync.Mutex
|
||||
}
|
||||
|
||||
// NewWatcher creates a new file watcher instance
|
||||
@@ -54,6 +56,7 @@ func NewWatcher(configPath, authDir string, reloadCallback func(map[string]inter
|
||||
reloadCallback: reloadCallback,
|
||||
watcher: watcher,
|
||||
clients: make(map[string]interfaces.Client),
|
||||
eventTimes: make(map[string]time.Time),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -123,6 +126,16 @@ func (w *Watcher) handleEvent(event fsnotify.Event) {
|
||||
now := time.Now()
|
||||
log.Debugf("file system event detected: %s %s", event.Op.String(), event.Name)
|
||||
|
||||
// Debounce logic to prevent rapid reloads
|
||||
w.eventMutex.Lock()
|
||||
if lastTime, ok := w.eventTimes[event.Name]; ok && now.Sub(lastTime) < 500*time.Millisecond {
|
||||
log.Debugf("debouncing event for %s", event.Name)
|
||||
w.eventMutex.Unlock()
|
||||
return
|
||||
}
|
||||
w.eventTimes[event.Name] = now
|
||||
w.eventMutex.Unlock()
|
||||
|
||||
// Handle config file changes
|
||||
if event.Name == w.configPath && (event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create) {
|
||||
log.Infof("config file changed, reloading: %s", w.configPath)
|
||||
|
||||
Reference in New Issue
Block a user