add wait many change for watcher

This commit is contained in:
yuzuki999
2023-06-02 10:18:10 +08:00
parent df422c965b
commit 0271d0a884
2 changed files with 23 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ import (
"log" "log"
"os" "os"
"path" "path"
"time"
) )
type Conf struct { type Conf struct {
@@ -63,10 +64,19 @@ func (p *Conf) Watch(filePath string, reload func()) error {
return fmt.Errorf("new watcher error: %s", err) return fmt.Errorf("new watcher error: %s", err)
} }
go func() { go func() {
var pre time.Time
defer watcher.Close() defer watcher.Close()
for { for {
select { 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...") log.Println("config dir changed, reloading...")
*p = *New() *p = *New()
err := p.LoadFromPath(filePath) err := p.LoadFromPath(filePath)

View File

@@ -1,8 +1,19 @@
package conf package conf
import "testing" import (
"log"
"testing"
)
func TestConf_LoadFromPath(t *testing.T) { func TestConf_LoadFromPath(t *testing.T) {
c := New() c := New()
t.Log(c.LoadFromPath("../example/config.yml.example"), c.NodesConfig[0].ControllerConfig.LimitConfig.IPLimit) 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 {}
}