From 13f46f440526b3684cb58de135fc6eb76aebc7ef Mon Sep 17 00:00:00 2001 From: wuko233 Date: Wed, 8 Apr 2026 14:52:52 +0800 Subject: [PATCH] =?UTF-8?q?[monitor]=20=E6=96=87=E4=BB=B6=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=94=AF=E6=8C=81=E9=98=B2=E6=8A=96=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/monitor/detector/file_detector.go | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/internal/monitor/detector/file_detector.go b/internal/monitor/detector/file_detector.go index 5df7f45..845d94a 100644 --- a/internal/monitor/detector/file_detector.go +++ b/internal/monitor/detector/file_detector.go @@ -12,16 +12,20 @@ import ( ) type FileDetector struct { - cfg *config.Config - whiteList map[string]string - storageDir string - mu sync.RWMutex + cfg *config.Config + whiteList map[string]string + storageDir string + mu sync.RWMutex + timer map[string]*time.Timer + debDuration time.Duration } func NewFileDetector(cfg *config.Config) (*FileDetector, error) { d := &FileDetector{ - cfg: cfg, - storageDir: cfg.Storage.DataDir, + cfg: cfg, + storageDir: cfg.Storage.DataDir, + timer: make(map[string]*time.Timer), + debDuration: 1 * time.Second, } if err := d.loadWhiteList(); err != nil { @@ -46,6 +50,22 @@ func (d *FileDetector) loadWhiteList() error { func (d *FileDetector) HandleEvent(eventPath string, opStr string) { // Todo: 忽略临时文件等 + d.mu.Lock() + defer d.mu.Unlock() + + if t, exists := d.timer[eventPath]; exists { + t.Stop() + } + + d.timer[eventPath] = time.AfterFunc(d.debDuration, func() { + d.processEvent(eventPath) + d.mu.Lock() + delete(d.timer, eventPath) + d.mu.Unlock() + }) +} + +func (d *FileDetector) processEvent(eventPath string) { info, err := storage.GetFileInfo(eventPath) if err != nil { logger.Log.Warn("[monitor] 获取文件信息失败", zap.String("path", eventPath), zap.Error(err))