fix: force update provider happen loopback

This commit is contained in:
gVisor bot 2022-06-18 16:05:09 +08:00
parent 22226d5e68
commit fd40911fde
2 changed files with 42 additions and 16 deletions

View file

@ -43,6 +43,7 @@ func (f *fetcher[V]) Initial() (V, error) {
buf []byte
err error
isLocal bool
forceUpdate bool
)
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
@ -51,10 +52,8 @@ func (f *fetcher[V]) Initial() (V, error) {
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, force refresh", f.Name())
go f.Update()
}()
log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name())
forceUpdate = true
}
} else {
buf, err = f.vehicle.Read()
@ -64,7 +63,21 @@ func (f *fetcher[V]) Initial() (V, error) {
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 !isLocal {
return getZero[V](), err

View file

@ -43,6 +43,7 @@ func (f *fetcher) Initial() (interface{}, error) {
buf []byte
hasLocal bool
err error
forceUpdate bool
)
defer func() {
@ -57,10 +58,8 @@ func (f *fetcher) Initial() (interface{}, error) {
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, force refresh", f.Name())
go f.update()
}()
forceUpdate = true
log.Infoln("[Provider] %s not updated for a long time, force refresh", f.Name())
}
} else {
buf, err = f.vehicle.Read()
@ -70,7 +69,21 @@ func (f *fetcher) Initial() (interface{}, error) {
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 !hasLocal {
return nil, err