Skip to content

Commit 7e5ac1c

Browse files
committed
perf(router): adjust the order of rules to optimize performance
1 parent 2a5b568 commit 7e5ac1c

File tree

2 files changed

+26
-40
lines changed

2 files changed

+26
-40
lines changed

app/router/condition.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,6 @@ var matcherTypeMap = map[Domain_Type]strmatcher.Type{
4747
Domain_Full: strmatcher.Full,
4848
}
4949

50-
func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
51-
matcherType, f := matcherTypeMap[domain.Type]
52-
if !f {
53-
return nil, errors.New("unsupported domain type", domain.Type)
54-
}
55-
56-
matcher, err := matcherType.New(domain.Value)
57-
if err != nil {
58-
return nil, errors.New("failed to create domain matcher").Base(err)
59-
}
60-
61-
return matcher, nil
62-
}
63-
6450
type DomainMatcher struct {
6551
matchers strmatcher.IndexMatcher
6652
}

app/router/config.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,16 @@ func (r *Rule) Apply(ctx routing.Context) bool {
3232
func (rr *RoutingRule) BuildCondition() (Condition, error) {
3333
conds := NewConditionChan()
3434

35-
if len(rr.Domain) > 0 {
36-
matcher, err := NewMphMatcherGroup(rr.Domain)
37-
if err != nil {
38-
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
39-
}
40-
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
41-
conds.Add(matcher)
42-
}
43-
44-
if len(rr.UserEmail) > 0 {
45-
conds.Add(NewUserMatcher(rr.UserEmail))
35+
if len(rr.InboundTag) > 0 {
36+
conds.Add(NewInboundTagMatcher(rr.InboundTag))
4637
}
4738

48-
if rr.VlessRouteList != nil {
49-
conds.Add(NewPortMatcher(rr.VlessRouteList, MatcherAsType_VlessRoute))
39+
if len(rr.Networks) > 0 {
40+
conds.Add(NewNetworkMatcher(rr.Networks))
5041
}
5142

52-
if len(rr.InboundTag) > 0 {
53-
conds.Add(NewInboundTagMatcher(rr.InboundTag))
43+
if len(rr.Protocol) > 0 {
44+
conds.Add(NewProtocolMatcher(rr.Protocol))
5445
}
5546

5647
if rr.PortList != nil {
@@ -65,8 +56,20 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
6556
conds.Add(NewPortMatcher(rr.LocalPortList, MatcherAsType_Local))
6657
}
6758

68-
if len(rr.Networks) > 0 {
69-
conds.Add(NewNetworkMatcher(rr.Networks))
59+
if rr.VlessRouteList != nil {
60+
conds.Add(NewPortMatcher(rr.VlessRouteList, MatcherAsType_VlessRoute))
61+
}
62+
63+
if len(rr.UserEmail) > 0 {
64+
conds.Add(NewUserMatcher(rr.UserEmail))
65+
}
66+
67+
if len(rr.Attributes) > 0 {
68+
configuredKeys := make(map[string]*regexp.Regexp)
69+
for key, value := range rr.Attributes {
70+
configuredKeys[strings.ToLower(key)] = regexp.MustCompile(value)
71+
}
72+
conds.Add(&AttributeMatcher{configuredKeys})
7073
}
7174

7275
if len(rr.Geoip) > 0 {
@@ -94,16 +97,13 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
9497
errors.LogWarning(context.Background(), "Due to some limitations, in UDP connections, localIP is always equal to listen interface IP, so \"localIP\" rule condition does not work properly on UDP inbound connections that listen on all interfaces")
9598
}
9699

97-
if len(rr.Protocol) > 0 {
98-
conds.Add(NewProtocolMatcher(rr.Protocol))
99-
}
100-
101-
if len(rr.Attributes) > 0 {
102-
configuredKeys := make(map[string]*regexp.Regexp)
103-
for key, value := range rr.Attributes {
104-
configuredKeys[strings.ToLower(key)] = regexp.MustCompile(value)
100+
if len(rr.Domain) > 0 {
101+
matcher, err := NewMphMatcherGroup(rr.Domain)
102+
if err != nil {
103+
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
105104
}
106-
conds.Add(&AttributeMatcher{configuredKeys})
105+
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
106+
conds.Add(matcher)
107107
}
108108

109109
if conds.Len() == 0 {

0 commit comments

Comments
 (0)