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

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

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