From a71fd3b4df596b2b2c859884f58a41c0f5eca056 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 26 May 2022 21:05:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=B5=8Bprovider=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=EF=BC=8C=E5=BC=BA=E5=88=B6=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/provider/fetcher.go | 39 ++++++++++++++++++++---------- rule/provider/fetcher.go | 48 ++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/adapter/provider/fetcher.go b/adapter/provider/fetcher.go index 26029bc6..256670f8 100644 --- a/adapter/provider/fetcher.go +++ b/adapter/provider/fetcher.go @@ -26,6 +26,7 @@ type fetcher struct { done chan struct{} hash [16]byte parser parser + interval time.Duration onUpdate func(any) } @@ -56,6 +57,12 @@ func (f *fetcher) Initial() (any, error) { modTime := stat.ModTime() f.updatedAt = &modTime isLocal = true + if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) { + defer func() { + log.Infoln("[Provider] %s's proxies not updated for a long time") + go f.update() + }() + } } else { buf, err = f.vehicle.Read() } @@ -137,21 +144,11 @@ func (f *fetcher) pullLoop() { for { select { case <-f.ticker.C: - elm, same, err := f.Update() - if err != nil { - log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) + same, err := f.update() + if same || err != nil { continue } - if same { - log.Debugln("[Provider] %s's proxies doesn't change", f.Name()) - continue - } - - log.Infoln("[Provider] %s's proxies update", f.Name()) - if f.onUpdate != nil { - f.onUpdate(elm) - } case <-f.done: f.ticker.Stop() return @@ -159,6 +156,23 @@ func (f *fetcher) pullLoop() { } } +func (f *fetcher) update() (same bool, err error) { + elm, same, err := f.Update() + if err != nil { + log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) + } + + if same { + log.Debugln("[Provider] %s's proxies doesn't change", f.Name()) + } + if f.onUpdate != nil { + f.onUpdate(elm) + } + + log.Infoln("[Provider] %s's proxies update", f.Name()) + return +} + func safeWrite(path string, buf []byte) error { dir := filepath.Dir(path) @@ -184,5 +198,6 @@ func newFetcher(name string, interval time.Duration, vehicle types.Vehicle, pars parser: parser, done: make(chan struct{}, 1), onUpdate: onUpdate, + interval: interval, } } diff --git a/rule/provider/fetcher.go b/rule/provider/fetcher.go index c56dfe29..01b9aaa8 100644 --- a/rule/provider/fetcher.go +++ b/rule/provider/fetcher.go @@ -27,6 +27,7 @@ type fetcher struct { hash [16]byte parser parser onUpdate func(interface{}) error + interval time.Duration } func (f *fetcher) Name() string { @@ -55,6 +56,12 @@ func (f *fetcher) Initial() (interface{}, error) { modTime := stat.ModTime() f.updatedAt = &modTime hasLocal = true + if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) { + defer func() { + log.Infoln("[Provider] %s's rules not updated for a long time", f.Name()) + go f.update() + }() + } } else { buf, err = f.vehicle.Read() } @@ -144,6 +151,7 @@ func newFetcher(name string, interval time.Duration, vehicle P.Vehicle, parser p parser: parser, done: make(chan struct{}, 1), onUpdate: onUpdate, + interval: interval, } } @@ -163,28 +171,34 @@ func (f *fetcher) pullLoop() { for { select { case <-f.ticker.C: - elm, same, err := f.Update() - if err != nil { - log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) + same, err := f.update() + if same || err != nil { continue } - - if same { - log.Debugln("[Provider] %s's rules doesn't change", f.Name()) - continue - } - - log.Infoln("[Provider] %s's rules update", f.Name()) - if f.onUpdate != nil { - err := f.onUpdate(elm) - if err != nil { - log.Infoln("[Provider] %s update failed", f.Name()) - } - } - case <-f.done: f.ticker.Stop() return } } } + +func (f *fetcher) update() (same bool, err error) { + elm, same, err := f.Update() + if err != nil { + log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) + } + + if same { + log.Debugln("[Provider] %s's rules doesn't change", f.Name()) + } + + log.Infoln("[Provider] %s's rules update", f.Name()) + if f.onUpdate != nil { + err := f.onUpdate(elm) + if err != nil { + log.Infoln("[Provider] %s update failed", f.Name()) + } + } + + return +}