mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-26 22:55:48 +00:00
fix(watcher): guard debounced callback after Stop
This commit is contained in:
@@ -318,6 +318,9 @@ func (w *Watcher) triggerServerUpdate(cfg *config.Config) {
|
|||||||
if w == nil || w.reloadCallback == nil || cfg == nil {
|
if w == nil || w.reloadCallback == nil || cfg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if w.stopped.Load() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
@@ -343,10 +346,13 @@ func (w *Watcher) triggerServerUpdate(cfg *config.Config) {
|
|||||||
w.serverUpdateTimer.Stop()
|
w.serverUpdateTimer.Stop()
|
||||||
}
|
}
|
||||||
w.serverUpdateTimer = time.AfterFunc(delay, func() {
|
w.serverUpdateTimer = time.AfterFunc(delay, func() {
|
||||||
|
if w.stopped.Load() {
|
||||||
|
return
|
||||||
|
}
|
||||||
w.clientsMutex.RLock()
|
w.clientsMutex.RLock()
|
||||||
latestCfg := w.config
|
latestCfg := w.config
|
||||||
w.clientsMutex.RUnlock()
|
w.clientsMutex.RUnlock()
|
||||||
if latestCfg == nil || w.reloadCallback == nil {
|
if latestCfg == nil || w.reloadCallback == nil || w.stopped.Load() {
|
||||||
w.serverUpdateMu.Lock()
|
w.serverUpdateMu.Lock()
|
||||||
w.serverUpdatePend = false
|
w.serverUpdatePend = false
|
||||||
w.serverUpdateMu.Unlock()
|
w.serverUpdateMu.Unlock()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
@@ -39,6 +40,7 @@ type Watcher struct {
|
|||||||
serverUpdateTimer *time.Timer
|
serverUpdateTimer *time.Timer
|
||||||
serverUpdateLast time.Time
|
serverUpdateLast time.Time
|
||||||
serverUpdatePend bool
|
serverUpdatePend bool
|
||||||
|
stopped atomic.Bool
|
||||||
reloadCallback func(*config.Config)
|
reloadCallback func(*config.Config)
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
lastAuthHashes map[string]string
|
lastAuthHashes map[string]string
|
||||||
@@ -119,6 +121,7 @@ func (w *Watcher) Start(ctx context.Context) error {
|
|||||||
|
|
||||||
// Stop stops the file watcher
|
// Stop stops the file watcher
|
||||||
func (w *Watcher) Stop() error {
|
func (w *Watcher) Stop() error {
|
||||||
|
w.stopped.Store(true)
|
||||||
w.stopDispatch()
|
w.stopDispatch()
|
||||||
w.stopConfigReloadTimer()
|
w.stopConfigReloadTimer()
|
||||||
w.stopServerUpdateTimer()
|
w.stopServerUpdateTimer()
|
||||||
|
|||||||
Reference in New Issue
Block a user