Skip to content

Commit

Permalink
chore: force refresh provider in background
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 21, 2024
1 parent 7dafe78 commit 223eae0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
4 changes: 2 additions & 2 deletions adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
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())
if err != nil {
return
Expand All @@ -137,7 +137,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {

userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
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())
if err != nil {
return
Expand Down
30 changes: 10 additions & 20 deletions component/resource/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
modTime := stat.ModTime()
f.updatedAt = modTime
isLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
log.Warnln("[Provider] %s not updated for a long time, force refresh", f.Name())
if time.Since(modTime) > f.interval {
forceUpdate = true
}
} else {
Expand All @@ -78,21 +77,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
return lo.Empty[V](), err
}

var contents V
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)
}

contents, err := f.parser(buf)
if err != nil {
if !isLocal {
return lo.Empty[V](), err
Expand Down Expand Up @@ -135,7 +120,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
return lo.Empty[V](), err
}
} else if f.interval > 0 {
go f.pullLoop()
go f.pullLoop(forceUpdate)
}

return contents, nil
Expand Down Expand Up @@ -164,7 +149,7 @@ func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
}

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
}
}
Expand All @@ -183,12 +168,17 @@ func (f *Fetcher[V]) Close() error {
return nil
}

func (f *Fetcher[V]) pullLoop() {
func (f *Fetcher[V]) pullLoop(forceUpdate bool) {
initialInterval := f.interval - time.Since(f.updatedAt)
if 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)
defer timer.Stop()
for {
Expand Down
4 changes: 4 additions & 0 deletions component/resource/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (f *FileVehicle) Path() string {
return f.path
}

func (f *FileVehicle) Url() string {
return "file://" + f.path
}

func (f *FileVehicle) Read(ctx context.Context) ([]byte, error) {
return os.ReadFile(f.path)
}
Expand Down
1 change: 1 addition & 0 deletions constant/provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (v VehicleType) String() string {
type Vehicle interface {
Read(ctx context.Context) ([]byte, error)
Path() string
Url() string
Proxy() string
Type() VehicleType
}
Expand Down

0 comments on commit 223eae0

Please sign in to comment.