fix: force update provider happen loopback

This commit is contained in:
Skyxim 2022-06-18 16:05:09 +08:00
parent a562b249a2
commit 54a0947bb4
2 changed files with 42 additions and 16 deletions

View file

@ -43,6 +43,7 @@ func (f *fetcher[V]) Initial() (V, error) {
buf []byte buf []byte
err error err error
isLocal bool isLocal bool
forceUpdate bool
) )
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil { if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
@ -51,10 +52,8 @@ func (f *fetcher[V]) Initial() (V, error) {
f.updatedAt = &modTime f.updatedAt = &modTime
isLocal = true isLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) { if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
defer func() { log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name())
log.Infoln("[Provider] %s's proxies not updated for a long time, force refresh", f.Name()) forceUpdate = true
go f.Update()
}()
} }
} else { } else {
buf, err = f.vehicle.Read() buf, err = f.vehicle.Read()
@ -64,7 +63,21 @@ func (f *fetcher[V]) Initial() (V, error) {
return getZero[V](), err return getZero[V](), err
} }
proxies, err := f.parser(buf) var proxies V
if forceUpdate {
var forceBuf []byte
if forceBuf, err = f.vehicle.Read(); err == nil {
if proxies, err = f.parser(forceBuf); err == nil {
isLocal = false
buf = forceBuf
}
}
}
if err != nil || !forceUpdate {
proxies, err = f.parser(buf)
}
if err != nil { if err != nil {
if !isLocal { if !isLocal {
return getZero[V](), err return getZero[V](), err

View file

@ -43,6 +43,7 @@ func (f *fetcher) Initial() (interface{}, error) {
buf []byte buf []byte
hasLocal bool hasLocal bool
err error err error
forceUpdate bool
) )
defer func() { defer func() {
@ -57,10 +58,8 @@ func (f *fetcher) Initial() (interface{}, error) {
f.updatedAt = &modTime f.updatedAt = &modTime
hasLocal = true hasLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) { if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
defer func() { forceUpdate = true
log.Infoln("[Provider] %s's rules not updated for a long time, force refresh", f.Name()) log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name())
go f.update()
}()
} }
} else { } else {
buf, err = f.vehicle.Read() buf, err = f.vehicle.Read()
@ -70,7 +69,21 @@ func (f *fetcher) Initial() (interface{}, error) {
return nil, err return nil, err
} }
rules, err := f.parser(buf) var rules interface{}
if forceUpdate {
var forceBuf []byte
if forceBuf, err = f.vehicle.Read(); err == nil {
if rules, err = f.parser(forceBuf); err == nil {
hasLocal = false
buf = forceBuf
}
}
}
if err != nil || !forceUpdate {
rules, err = f.parser(buf)
}
if err != nil { if err != nil {
if !hasLocal { if !hasLocal {
return nil, err return nil, err