chore: force refresh provider in background
Some checks are pending
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run

This commit is contained in:
wwqgtxx 2024-09-22 00:24:49 +08:00
parent 7dafe7889e
commit 223eae0e06
4 changed files with 17 additions and 22 deletions

View File

@ -128,7 +128,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
go func() { go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
defer cancel() defer cancel()
resp, err := mihomoHttp.HttpRequestWithProxy(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(), resp, err := mihomoHttp.HttpRequestWithProxy(ctx, pp.Vehicle().Url(),
http.MethodGet, nil, nil, pp.Vehicle().Proxy()) http.MethodGet, nil, nil, pp.Vehicle().Proxy())
if err != nil { if err != nil {
return return
@ -137,7 +137,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo")) userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
if userInfoStr == "" { if userInfoStr == "" {
resp2, err := mihomoHttp.HttpRequestWithProxy(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(), resp2, err := mihomoHttp.HttpRequestWithProxy(ctx, pp.Vehicle().Url(),
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil, pp.Vehicle().Proxy()) http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil, pp.Vehicle().Proxy())
if err != nil { if err != nil {
return return

View File

@ -65,8 +65,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
modTime := stat.ModTime() modTime := stat.ModTime()
f.updatedAt = modTime f.updatedAt = modTime
isLocal = true isLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) { if time.Since(modTime) > f.interval {
log.Warnln("[Provider] %s not updated for a long time, force refresh", f.Name())
forceUpdate = true forceUpdate = true
} }
} else { } else {
@ -78,21 +77,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
return lo.Empty[V](), err return lo.Empty[V](), err
} }
var contents V contents, err := f.parser(buf)
if forceUpdate {
var forceBuf []byte
if forceBuf, err = f.vehicle.Read(f.ctx); err == nil {
if contents, err = f.parser(forceBuf); err == nil {
isLocal = false
buf = forceBuf
}
}
}
if err != nil || !forceUpdate {
contents, err = f.parser(buf)
}
if err != nil { if err != nil {
if !isLocal { if !isLocal {
return lo.Empty[V](), err return lo.Empty[V](), err
@ -135,7 +120,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
return lo.Empty[V](), err return lo.Empty[V](), err
} }
} else if f.interval > 0 { } else if f.interval > 0 {
go f.pullLoop() go f.pullLoop(forceUpdate)
} }
return contents, nil return contents, nil
@ -164,7 +149,7 @@ func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
} }
if f.vehicle.Type() != types.File { if f.vehicle.Type() != types.File {
if err := safeWrite(f.vehicle.Path(), buf); err != nil { if err = safeWrite(f.vehicle.Path(), buf); err != nil {
return lo.Empty[V](), false, err return lo.Empty[V](), false, err
} }
} }
@ -183,12 +168,17 @@ func (f *Fetcher[V]) Close() error {
return nil return nil
} }
func (f *Fetcher[V]) pullLoop() { func (f *Fetcher[V]) pullLoop(forceUpdate bool) {
initialInterval := f.interval - time.Since(f.updatedAt) initialInterval := f.interval - time.Since(f.updatedAt)
if initialInterval > f.interval { if initialInterval > f.interval {
initialInterval = f.interval initialInterval = f.interval
} }
if forceUpdate {
log.Warnln("[Provider] %s not updated for a long time, force refresh", f.Name())
f.update(f.vehicle.Path())
}
timer := time.NewTimer(initialInterval) timer := time.NewTimer(initialInterval)
defer timer.Stop() defer timer.Stop()
for { for {

View File

@ -24,6 +24,10 @@ func (f *FileVehicle) Path() string {
return f.path return f.path
} }
func (f *FileVehicle) Url() string {
return "file://" + f.path
}
func (f *FileVehicle) Read(ctx context.Context) ([]byte, error) { func (f *FileVehicle) Read(ctx context.Context) ([]byte, error) {
return os.ReadFile(f.path) return os.ReadFile(f.path)
} }

View File

@ -34,6 +34,7 @@ func (v VehicleType) String() string {
type Vehicle interface { type Vehicle interface {
Read(ctx context.Context) ([]byte, error) Read(ctx context.Context) ([]byte, error)
Path() string Path() string
Url() string
Proxy() string Proxy() string
Type() VehicleType Type() VehicleType
} }