@@ -3,6 +3,7 @@ package resource
33import (
44 "context"
55 "os"
6+ "sync"
67 "time"
78
89 "github.com/metacubex/mihomo/common/utils"
@@ -27,6 +28,7 @@ type Fetcher[V any] struct {
2728 interval time.Duration
2829 onUpdate func (V )
2930 watcher * fswatch.Watcher
31+ loadBufMutex sync.Mutex
3032}
3133
3234func (f * Fetcher [V ]) Name () string {
@@ -46,17 +48,11 @@ func (f *Fetcher[V]) UpdatedAt() time.Time {
4648}
4749
4850func (f * Fetcher [V ]) Initial () (V , error ) {
49- var (
50- buf []byte
51- contents V
52- err error
53- )
54-
5551 if stat , fErr := os .Stat (f .vehicle .Path ()); fErr == nil {
5652 // local file exists, use it first
57- buf , err = os .ReadFile (f .vehicle .Path ())
53+ buf , err : = os .ReadFile (f .vehicle .Path ())
5854 modTime := stat .ModTime ()
59- contents , _ , err = f .loadBuf (buf , utils .MakeHash (buf ), false )
55+ contents , _ , err : = f .loadBuf (buf , utils .MakeHash (buf ), false )
6056 f .updatedAt = modTime // reset updatedAt to file's modTime
6157
6258 if err == nil {
@@ -69,15 +65,18 @@ func (f *Fetcher[V]) Initial() (V, error) {
6965 }
7066
7167 // parse local file error, fallback to remote
72- contents , _ , err = f .Update ()
68+ contents , _ , updateErr : = f .Update ()
7369
70+ // start the pull loop even if f.Update() failed
71+ err := f .startPullLoop (false )
7472 if err != nil {
7573 return lo .Empty [V ](), err
7674 }
77- err = f . startPullLoop ( false )
78- if err != nil {
79- return lo .Empty [V ](), err
75+
76+ if updateErr != nil {
77+ return lo .Empty [V ](), updateErr
8078 }
79+
8180 return contents , nil
8281}
8382
@@ -94,6 +93,9 @@ func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
9493}
9594
9695func (f * Fetcher [V ]) loadBuf (buf []byte , hash utils.HashType , updateFile bool ) (V , bool , error ) {
96+ f .loadBufMutex .Lock ()
97+ defer f .loadBufMutex .Unlock ()
98+
9799 now := time .Now ()
98100 if f .hash .Equal (hash ) {
99101 if updateFile {
0 commit comments