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

@ -40,9 +40,10 @@ func (f *fetcher[V]) VehicleType() types.VehicleType {
func (f *fetcher[V]) Initial() (V, error) { func (f *fetcher[V]) Initial() (V, error) {
var ( var (
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

@ -40,9 +40,10 @@ func (f *fetcher) VehicleType() P.VehicleType {
func (f *fetcher) Initial() (interface{}, error) { func (f *fetcher) Initial() (interface{}, error) {
var ( var (
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