diff --git a/adapter/provider/fetcher.go b/adapter/provider/fetcher.go index 3fcda09a..47b364e6 100644 --- a/adapter/provider/fetcher.go +++ b/adapter/provider/fetcher.go @@ -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 diff --git a/rules/provider/fetcher.go b/rules/provider/fetcher.go index 89dfd5db..b542a8e2 100644 --- a/rules/provider/fetcher.go +++ b/rules/provider/fetcher.go @@ -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