From 0271d0a884a5c0eb121214a0e8c7d8e1238a8d87 Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Fri, 2 Jun 2023 10:18:10 +0800 Subject: [PATCH] add wait many change for watcher --- conf/conf.go | 12 +++++++++++- conf/conf_test.go | 13 ++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/conf/conf.go b/conf/conf.go index 47ef9b4..4c1b7c3 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -8,6 +8,7 @@ import ( "log" "os" "path" + "time" ) type Conf struct { @@ -63,10 +64,19 @@ func (p *Conf) Watch(filePath string, reload func()) error { return fmt.Errorf("new watcher error: %s", err) } go func() { + var pre time.Time defer watcher.Close() for { select { - case <-watcher.Events: + case e := <-watcher.Events: + if e.Has(fsnotify.Chmod) { + continue + } + if pre.Add(1 * time.Second).After(time.Now()) { + continue + } + time.Sleep(2 * time.Second) + pre = time.Now() log.Println("config dir changed, reloading...") *p = *New() err := p.LoadFromPath(filePath) diff --git a/conf/conf_test.go b/conf/conf_test.go index 48a2acf..0591ed2 100644 --- a/conf/conf_test.go +++ b/conf/conf_test.go @@ -1,8 +1,19 @@ package conf -import "testing" +import ( + "log" + "testing" +) func TestConf_LoadFromPath(t *testing.T) { c := New() t.Log(c.LoadFromPath("../example/config.yml.example"), c.NodesConfig[0].ControllerConfig.LimitConfig.IPLimit) } + +func TestConf_Watch(t *testing.T) { + c := New() + c.Watch("../example/config.yml.example", func() { + log.Println(1) + }) + select {} +}