[monitor] 文件检测支持防抖动
This commit is contained in:
parent
826c4a24ec
commit
13f46f4405
|
|
@ -16,12 +16,16 @@ type FileDetector struct {
|
||||||
whiteList map[string]string
|
whiteList map[string]string
|
||||||
storageDir string
|
storageDir string
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
timer map[string]*time.Timer
|
||||||
|
debDuration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileDetector(cfg *config.Config) (*FileDetector, error) {
|
func NewFileDetector(cfg *config.Config) (*FileDetector, error) {
|
||||||
d := &FileDetector{
|
d := &FileDetector{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
storageDir: cfg.Storage.DataDir,
|
storageDir: cfg.Storage.DataDir,
|
||||||
|
timer: make(map[string]*time.Timer),
|
||||||
|
debDuration: 1 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := d.loadWhiteList(); err != nil {
|
if err := d.loadWhiteList(); err != nil {
|
||||||
|
|
@ -46,6 +50,22 @@ func (d *FileDetector) loadWhiteList() error {
|
||||||
func (d *FileDetector) HandleEvent(eventPath string, opStr string) {
|
func (d *FileDetector) HandleEvent(eventPath string, opStr string) {
|
||||||
// Todo: 忽略临时文件等
|
// 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)
|
info, err := storage.GetFileInfo(eventPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Warn("[monitor] 获取文件信息失败", zap.String("path", eventPath), zap.Error(err))
|
logger.Log.Warn("[monitor] 获取文件信息失败", zap.String("path", eventPath), zap.Error(err))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user