Fix: immediately update provider when modtime too old
This commit is contained in:
parent
fbca37c42b
commit
2301b909d2
1 changed files with 32 additions and 20 deletions
|
@ -21,6 +21,7 @@ type parser = func([]byte) (any, error)
|
|||
type fetcher struct {
|
||||
name string
|
||||
vehicle types.Vehicle
|
||||
interval time.Duration
|
||||
updatedAt *time.Time
|
||||
ticker *time.Ticker
|
||||
done chan struct{}
|
||||
|
@ -42,12 +43,14 @@ func (f *fetcher) Initial() (any, error) {
|
|||
buf []byte
|
||||
err error
|
||||
isLocal bool
|
||||
immediatelyUpdate bool
|
||||
)
|
||||
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
|
||||
buf, err = os.ReadFile(f.vehicle.Path())
|
||||
modTime := stat.ModTime()
|
||||
f.updatedAt = &modTime
|
||||
isLocal = true
|
||||
immediatelyUpdate = time.Since(modTime) > f.interval
|
||||
} else {
|
||||
buf, err = f.vehicle.Read()
|
||||
}
|
||||
|
@ -86,7 +89,7 @@ func (f *fetcher) Initial() (any, error) {
|
|||
|
||||
// pull proxies automatically
|
||||
if f.ticker != nil {
|
||||
go f.pullLoop()
|
||||
go f.pullLoop(immediatelyUpdate)
|
||||
}
|
||||
|
||||
return proxies, nil
|
||||
|
@ -130,25 +133,33 @@ func (f *fetcher) Destroy() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (f *fetcher) pullLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-f.ticker.C:
|
||||
func (f *fetcher) pullLoop(immediately bool) {
|
||||
update := func() {
|
||||
elm, same, err := f.Update()
|
||||
if err != nil {
|
||||
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
if same {
|
||||
log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
log.Infoln("[Provider] %s's proxies update", f.Name())
|
||||
if f.onUpdate != nil {
|
||||
f.onUpdate(elm)
|
||||
}
|
||||
}
|
||||
|
||||
if immediately {
|
||||
update()
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-f.ticker.C:
|
||||
update()
|
||||
case <-f.done:
|
||||
f.ticker.Stop()
|
||||
return
|
||||
|
@ -178,6 +189,7 @@ func newFetcher(name string, interval time.Duration, vehicle types.Vehicle, pars
|
|||
name: name,
|
||||
ticker: ticker,
|
||||
vehicle: vehicle,
|
||||
interval: interval,
|
||||
parser: parser,
|
||||
done: make(chan struct{}, 1),
|
||||
onUpdate: onUpdate,
|
||||
|
|
Loading…
Reference in a new issue