fix: 启动时检测provider文件是否过期,强制更新

This commit is contained in:
Skyxim 2022-05-26 21:05:00 +08:00
parent 2ebc0383b5
commit a71fd3b4df
2 changed files with 58 additions and 29 deletions

View file

@ -26,6 +26,7 @@ type fetcher struct {
done chan struct{} done chan struct{}
hash [16]byte hash [16]byte
parser parser parser parser
interval time.Duration
onUpdate func(any) onUpdate func(any)
} }
@ -56,6 +57,12 @@ func (f *fetcher) Initial() (any, error) {
modTime := stat.ModTime() modTime := stat.ModTime()
f.updatedAt = &modTime f.updatedAt = &modTime
isLocal = true 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 { } else {
buf, err = f.vehicle.Read() buf, err = f.vehicle.Read()
} }
@ -137,21 +144,11 @@ func (f *fetcher) pullLoop() {
for { for {
select { select {
case <-f.ticker.C: case <-f.ticker.C:
elm, same, err := f.Update() same, err := f.update()
if err != nil { if same || err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
continue 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: case <-f.done:
f.ticker.Stop() f.ticker.Stop()
return 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 { func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path) dir := filepath.Dir(path)
@ -184,5 +198,6 @@ func newFetcher(name string, interval time.Duration, vehicle types.Vehicle, pars
parser: parser, parser: parser,
done: make(chan struct{}, 1), done: make(chan struct{}, 1),
onUpdate: onUpdate, onUpdate: onUpdate,
interval: interval,
} }
} }

View file

@ -27,6 +27,7 @@ type fetcher struct {
hash [16]byte hash [16]byte
parser parser parser parser
onUpdate func(interface{}) error onUpdate func(interface{}) error
interval time.Duration
} }
func (f *fetcher) Name() string { func (f *fetcher) Name() string {
@ -55,6 +56,12 @@ func (f *fetcher) Initial() (interface{}, error) {
modTime := stat.ModTime() modTime := stat.ModTime()
f.updatedAt = &modTime f.updatedAt = &modTime
hasLocal = true 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 { } else {
buf, err = f.vehicle.Read() buf, err = f.vehicle.Read()
} }
@ -144,6 +151,7 @@ func newFetcher(name string, interval time.Duration, vehicle P.Vehicle, parser p
parser: parser, parser: parser,
done: make(chan struct{}, 1), done: make(chan struct{}, 1),
onUpdate: onUpdate, onUpdate: onUpdate,
interval: interval,
} }
} }
@ -163,15 +171,25 @@ func (f *fetcher) pullLoop() {
for { for {
select { select {
case <-f.ticker.C: case <-f.ticker.C:
same, err := f.update()
if same || err != nil {
continue
}
case <-f.done:
f.ticker.Stop()
return
}
}
}
func (f *fetcher) update() (same bool, err error) {
elm, same, err := f.Update() elm, same, err := f.Update()
if err != nil { if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error()) log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
continue
} }
if same { if same {
log.Debugln("[Provider] %s's rules doesn't change", f.Name()) log.Debugln("[Provider] %s's rules doesn't change", f.Name())
continue
} }
log.Infoln("[Provider] %s's rules update", f.Name()) log.Infoln("[Provider] %s's rules update", f.Name())
@ -182,9 +200,5 @@ func (f *fetcher) pullLoop() {
} }
} }
case <-f.done:
f.ticker.Stop()
return return
} }
}
}