mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-14 14:08:04 +08:00
fix: fetcher does not start the pull loop when local file parsing errors occur and the first remote update fails
This commit is contained in:
parent
a013ac32a3
commit
61d6a9abd6
@ -3,6 +3,7 @@ package resource
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
@ -27,6 +28,7 @@ type Fetcher[V any] struct {
|
|||||||
interval time.Duration
|
interval time.Duration
|
||||||
onUpdate func(V)
|
onUpdate func(V)
|
||||||
watcher *fswatch.Watcher
|
watcher *fswatch.Watcher
|
||||||
|
loadBufMutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) Name() string {
|
func (f *Fetcher[V]) Name() string {
|
||||||
@ -46,17 +48,11 @@ func (f *Fetcher[V]) UpdatedAt() time.Time {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) Initial() (V, error) {
|
func (f *Fetcher[V]) Initial() (V, error) {
|
||||||
var (
|
|
||||||
buf []byte
|
|
||||||
contents V
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
|
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
|
||||||
// local file exists, use it first
|
// local file exists, use it first
|
||||||
buf, err = os.ReadFile(f.vehicle.Path())
|
buf, err := os.ReadFile(f.vehicle.Path())
|
||||||
modTime := stat.ModTime()
|
modTime := stat.ModTime()
|
||||||
contents, _, err = f.loadBuf(buf, utils.MakeHash(buf), false)
|
contents, _, err := f.loadBuf(buf, utils.MakeHash(buf), false)
|
||||||
f.updatedAt = modTime // reset updatedAt to file's modTime
|
f.updatedAt = modTime // reset updatedAt to file's modTime
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -69,15 +65,18 @@ func (f *Fetcher[V]) Initial() (V, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parse local file error, fallback to remote
|
// parse local file error, fallback to remote
|
||||||
contents, _, err = f.Update()
|
contents, _, updateErr := f.Update()
|
||||||
|
|
||||||
|
// start the pull loop even if f.Update() failed
|
||||||
|
err := f.startPullLoop(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lo.Empty[V](), err
|
return lo.Empty[V](), err
|
||||||
}
|
}
|
||||||
err = f.startPullLoop(false)
|
|
||||||
if err != nil {
|
if updateErr != nil {
|
||||||
return lo.Empty[V](), err
|
return lo.Empty[V](), updateErr
|
||||||
}
|
}
|
||||||
|
|
||||||
return contents, nil
|
return contents, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,6 +93,9 @@ func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) loadBuf(buf []byte, hash utils.HashType, updateFile bool) (V, bool, error) {
|
func (f *Fetcher[V]) loadBuf(buf []byte, hash utils.HashType, updateFile bool) (V, bool, error) {
|
||||||
|
f.loadBufMutex.Lock()
|
||||||
|
defer f.loadBufMutex.Unlock()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if f.hash.Equal(hash) {
|
if f.hash.Equal(hash) {
|
||||||
if updateFile {
|
if updateFile {
|
||||||
|
Loading…
Reference in New Issue
Block a user