fix: replace with sync.map for GroupBase

This commit is contained in:
gVisor bot 2022-04-28 23:43:10 +08:00
parent 265fb0dabd
commit 336d3482d6

View file

@ -7,14 +7,15 @@ import (
types "github.com/Dreamacro/clash/constant/provider" types "github.com/Dreamacro/clash/constant/provider"
"github.com/Dreamacro/clash/tunnel" "github.com/Dreamacro/clash/tunnel"
"github.com/dlclark/regexp2" "github.com/dlclark/regexp2"
"sync"
) )
type GroupBase struct { type GroupBase struct {
*outbound.Base *outbound.Base
filter *regexp2.Regexp filter *regexp2.Regexp
providers []provider.ProxyProvider providers []provider.ProxyProvider
versions map[string]uint versions sync.Map // map[string]uint
proxies map[string][]C.Proxy proxies sync.Map // map[string][]C.Proxy
} }
type GroupBaseOption struct { type GroupBaseOption struct {
@ -32,8 +33,6 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
Base: outbound.NewBase(opt.BaseOption), Base: outbound.NewBase(opt.BaseOption),
filter: filter, filter: filter,
providers: opt.providers, providers: opt.providers,
versions: map[string]uint{},
proxies: map[string][]C.Proxy{},
} }
} }
@ -56,16 +55,16 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
for _, pd := range gb.providers { for _, pd := range gb.providers {
if pd.VehicleType() == types.Compatible { if pd.VehicleType() == types.Compatible {
if touch { if touch {
gb.proxies[pd.Name()] = pd.ProxiesWithTouch() gb.proxies.Store(pd.Name(), pd.ProxiesWithTouch())
} else { } else {
gb.proxies[pd.Name()] = pd.Proxies() gb.proxies.Store(pd.Name(), pd.Proxies())
} }
gb.versions[pd.Name()] = pd.Version() gb.versions.Store(pd.Name(), pd.Version())
continue continue
} }
if version, ok := gb.versions[pd.Name()]; !ok || version != pd.Version() { if version, ok := gb.versions.Load(pd.Name()); !ok || version != pd.Version() {
var ( var (
proxies []C.Proxy proxies []C.Proxy
newProxies []C.Proxy newProxies []C.Proxy
@ -83,14 +82,15 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
} }
} }
gb.proxies[pd.Name()] = newProxies gb.proxies.Store(pd.Name(), newProxies)
gb.versions[pd.Name()] = pd.Version() gb.versions.Store(pd.Name(), pd.Version())
} }
} }
var proxies []C.Proxy var proxies []C.Proxy
for _, v := range gb.proxies { gb.proxies.Range(func(key, value any) bool {
proxies = append(proxies, v...) proxies = append(proxies, value.([]C.Proxy)...)
} return true
})
if len(proxies) == 0 { if len(proxies) == 0 {
return append(proxies, tunnel.Proxies()["COMPATIBLE"]) return append(proxies, tunnel.Proxies()["COMPATIBLE"])
} }