diff --git a/common/geodata/domain_matcher_test.go b/common/geodata/domain_matcher_test.go index 83ae60ca..0c0c5080 100644 --- a/common/geodata/domain_matcher_test.go +++ b/common/geodata/domain_matcher_test.go @@ -7,10 +7,11 @@ import ( "testing" "github.com/xtls/xray-core/common/geodata/strmatcher" + "github.com/xtls/xray-core/common/utils" ) func TestCompactDomainMatcher_PreservesCustomRuleIndices(t *testing.T) { - factory := &CompactDomainMatcherFactory{shared: make(map[string]strmatcher.MatcherSet)} + factory := &CompactDomainMatcherFactory{shared: utils.NewWeakCacheMap[string, strmatcher.LinearAnyMatcher]()} matcher, err := factory.BuildMatcher([]*DomainRule{ {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Full, Value: "example.com"}}}, {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Domain, Value: "example.com"}}}, @@ -31,7 +32,7 @@ func TestCompactDomainMatcher_PreservesCustomRuleIndices(t *testing.T) { func TestCompactDomainMatcher_PreservesMixedRuleIndices(t *testing.T) { t.Setenv("xray.location.asset", filepath.Join("..", "..", "resources")) - factory := &CompactDomainMatcherFactory{shared: make(map[string]strmatcher.MatcherSet)} + factory := &CompactDomainMatcherFactory{shared: utils.NewWeakCacheMap[string, strmatcher.LinearAnyMatcher]()} matcher, err := factory.BuildMatcher([]*DomainRule{ {Value: &DomainRule_Geosite{Geosite: &GeoSiteRule{File: DefaultGeoSiteDat, Code: "CN"}}}, {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Full, Value: "163.com"}}}, @@ -50,10 +51,11 @@ func TestCompactDomainMatcher_PreservesMixedRuleIndices(t *testing.T) { } func TestMphDomainMatcher_MatchReturnsDetachedSlice(t *testing.T) { - matcher, err := (&MphDomainMatcherFactory{shared: make(map[string]strmatcher.MatcherGroup)}).BuildMatcher([]*DomainRule{ - {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Full, Value: "example.com"}}}, - {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Domain, Value: "example.com"}}}, - }) + matcher, err := (&MphDomainMatcherFactory{shared: utils.NewWeakCacheMap[string, strmatcher.MphValueMatcher]()}). + BuildMatcher([]*DomainRule{ + {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Full, Value: "example.com"}}}, + {Value: &DomainRule_Custom{Custom: &Domain{Type: Domain_Domain, Value: "example.com"}}}, + }) if err != nil { t.Fatalf("BuildMatcher() failed: %v", err) } diff --git a/common/utils/weak_cache.go b/common/utils/weak_cache.go index c14e1912..a9aaacca 100644 --- a/common/utils/weak_cache.go +++ b/common/utils/weak_cache.go @@ -35,11 +35,11 @@ func (c *WeakCacheMap[K, V]) Store(key K, value *V) { defer c.mu.Unlock() weakPtr := weak.Make(value) c.m[key] = weakPtr - runtime.AddCleanup(value, func(any) { + runtime.AddCleanup(value, func(struct{}) { c.mu.Lock() defer c.mu.Unlock() if c.m[key] == weakPtr { delete(c.m, key) } - }, nil) + }, struct{}{}) }