refactor: optimize nodes caching

This commit is contained in:
Skyxim 2022-07-20 08:53:54 +08:00
parent 6b636c051a
commit 6a4063af0d
3 changed files with 27 additions and 25 deletions

View File

@ -19,12 +19,12 @@ type GroupBase struct {
*outbound.Base *outbound.Base
filter *regexp2.Regexp filter *regexp2.Regexp
providers []provider.ProxyProvider providers []provider.ProxyProvider
versions sync.Map // map[string]uint
proxies sync.Map // map[string][]C.Proxy
failedTestMux sync.Mutex failedTestMux sync.Mutex
failedTimes int failedTimes int
failedTime time.Time failedTime time.Time
failedTesting *atomic.Bool failedTesting *atomic.Bool
proxies [][]C.Proxy
versions []atomic.Uint32
} }
type GroupBaseOption struct { type GroupBaseOption struct {
@ -38,12 +38,18 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
if opt.filter != "" { if opt.filter != "" {
filter = regexp2.MustCompile(opt.filter, 0) filter = regexp2.MustCompile(opt.filter, 0)
} }
return &GroupBase{
gb := &GroupBase{
Base: outbound.NewBase(opt.BaseOption), Base: outbound.NewBase(opt.BaseOption),
filter: filter, filter: filter,
providers: opt.providers, providers: opt.providers,
failedTesting: atomic.NewBool(false), failedTesting: atomic.NewBool(false),
} }
gb.proxies = make([][]C.Proxy, len(opt.providers))
gb.versions = make([]atomic.Uint32, len(opt.providers))
return gb
} }
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy { func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
@ -61,43 +67,44 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
return proxies return proxies
} }
for _, pd := range gb.providers { for i, pd := range gb.providers {
if touch { if touch {
pd.Touch() pd.Touch()
} }
if pd.VehicleType() == types.Compatible { if pd.VehicleType() == types.Compatible {
gb.proxies.Store(pd.Name(), pd.Proxies()) gb.versions[i].Store(pd.Version())
gb.versions.Store(pd.Name(), pd.Version()) gb.proxies[i] = pd.Proxies()
continue continue
} }
if version, ok := gb.versions.Load(pd.Name()); !ok || version != pd.Version() { version := gb.versions[i].Load()
if version != pd.Version() && gb.versions[i].CAS(version, pd.Version()) {
var ( var (
proxies []C.Proxy proxies []C.Proxy
newProxies []C.Proxy newProxies []C.Proxy
) )
proxies = pd.Proxies() proxies = pd.Proxies()
for _, p := range proxies { for _, p := range proxies {
if mat, _ := gb.filter.FindStringMatch(p.Name()); mat != nil { if mat, _ := gb.filter.FindStringMatch(p.Name()); mat != nil {
newProxies = append(newProxies, p) newProxies = append(newProxies, p)
} }
} }
gb.proxies.Store(pd.Name(), newProxies) gb.proxies[i] = newProxies
gb.versions.Store(pd.Name(), pd.Version())
} }
} }
var proxies []C.Proxy var proxies []C.Proxy
gb.proxies.Range(func(key, value any) bool { for _, p := range gb.proxies {
proxies = append(proxies, value.([]C.Proxy)...) proxies = append(proxies, p...)
return true }
})
if len(proxies) == 0 { if len(proxies) == 0 {
return append(proxies, tunnel.Proxies()["COMPATIBLE"]) return append(proxies, tunnel.Proxies()["COMPATIBLE"])
} }
return proxies return proxies
} }

View File

@ -7,7 +7,6 @@ import (
"github.com/Dreamacro/clash/common/convert" "github.com/Dreamacro/clash/common/convert"
"github.com/Dreamacro/clash/component/resource" "github.com/Dreamacro/clash/component/resource"
"github.com/dlclark/regexp2" "github.com/dlclark/regexp2"
"math"
"runtime" "runtime"
"time" "time"
@ -35,7 +34,7 @@ type proxySetProvider struct {
*resource.Fetcher[[]C.Proxy] *resource.Fetcher[[]C.Proxy]
proxies []C.Proxy proxies []C.Proxy
healthCheck *HealthCheck healthCheck *HealthCheck
version uint version uint32
} }
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) { func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
@ -48,7 +47,7 @@ func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
}) })
} }
func (pp *proxySetProvider) Version() uint { func (pp *proxySetProvider) Version() uint32 {
return pp.version return pp.version
} }
@ -134,7 +133,7 @@ type compatibleProvider struct {
name string name string
healthCheck *HealthCheck healthCheck *HealthCheck
proxies []C.Proxy proxies []C.Proxy
version uint version uint32
} }
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) { func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
@ -146,7 +145,7 @@ func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
}) })
} }
func (cp *compatibleProvider) Version() uint { func (cp *compatibleProvider) Version() uint32 {
return cp.version return cp.version
} }
@ -209,11 +208,7 @@ func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*Co
func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) { func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
return func(elm []C.Proxy) { return func(elm []C.Proxy) {
pd.setProxies(elm) pd.setProxies(elm)
if pd.version == math.MaxUint { pd.version += 1
pd.version = 0
} else {
pd.version++
}
} }
} }

View File

@ -68,7 +68,7 @@ type ProxyProvider interface {
Proxies() []C.Proxy Proxies() []C.Proxy
Touch() Touch()
HealthCheck() HealthCheck()
Version() uint Version() uint32
} }
// Rule Type // Rule Type