Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package router
import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common/constant"
"regexp"
"strings"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
perrors "github.com/pkg/errors"
)
const (
RoutePattern = `([&!=,]*)\\s*([^&!=,\\s]+)`
)
type ConditionRouter struct {
Pattern string
Url common.URL
Priority int64
Force bool
WhenCondition map[string]MatchPair
ThenCondition map[string]MatchPair
}
func (c *ConditionRouter) Route(invokers []protocol.Invoker, url common.URL, invocation protocol.Invocation) ([]protocol.Invoker, error) {
if len(invokers) == 0 {
return invokers, nil
}
if !c.matchWhen(url, invocation) {
return invokers, nil
}
var result []protocol.Invoker
if len(c.ThenCondition) == 0 {
return result, nil
}
for _, invoker := range invokers {
if c.matchThen(invoker.GetUrl(), url) {
result = append(result, invoker)
}
}
if len(result) > 0 {
return result, nil
} else if c.Force {
//todo 日志
return result, nil
}
return result, nil
}
func (c ConditionRouter) CompareTo(r cluster.Router) int {
var result int
router, ok := r.(*ConditionRouter)
if r == nil || !ok {
return 1
}
if c.Priority == router.Priority {
result = strings.Compare(c.Url.String(), router.Url.String())
} else {
if c.Priority > router.Priority {
result = 1
} else {
result = -1
}
}
return result
}
func newConditionRouter(url common.URL) (*ConditionRouter, error) {
var whenRule string
var thenRule string
//rule := url.GetParam("rule", "")
w, err := parseRule(whenRule)
if err != nil {
return nil, perrors.Errorf("%s", "")
}
t, err := parseRule(thenRule)
if err != nil {
return nil, perrors.Errorf("%s", "")
}
when := If(whenRule == "" || "true" == whenRule, make(map[string]MatchPair), w).(map[string]MatchPair)
then := If(thenRule == "" || "false" == thenRule, make(map[string]MatchPair), t).(map[string]MatchPair)
return &ConditionRouter{
RoutePattern,
url,
url.GetParamInt("Priority", 0),
url.GetParamBool("Force", false),
when,
then,
}, nil
}
func parseRule(rule string) (map[string]MatchPair, error) {
condition := make(map[string]MatchPair)
if rule == "" {
return condition, nil
}
var pair MatchPair
values := make(map[string]interface{})
reg := regexp.MustCompile(`([&!=,]*)\s*([^&!=,\s]+)`)
startIndex := reg.FindIndex([]byte(rule))
matches := reg.FindAllSubmatch([]byte(rule), -1)
for _, groups := range matches {
separator := string(groups[1])
content := string(groups[2])
switch separator {
case "":
pair = MatchPair{}
condition[content] = pair
case "&":
if r, ok := condition[content]; ok {
pair = r
} else {
pair = MatchPair{}
condition[content] = pair
}
case "=":
if &pair == nil {
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex[0], startIndex[0])
}
values = pair.Matches
values[content] = ""
case "!=":
if &pair == nil {
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex[0], startIndex[0])
}
values = pair.Matches
values[content] = ""
case ",":
if len(values) == 0 {
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex[0], startIndex[0])
}
values[content] = ""
default:
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex[0], startIndex[0])
}
}
return condition, nil
//var pair MatchPair
}
func (c *ConditionRouter) matchWhen(url common.URL, invocation protocol.Invocation) bool {
return len(c.WhenCondition) == 0 || len(c.WhenCondition) == 0 || matchCondition(c.WhenCondition, &url, nil, invocation)
}
func (c *ConditionRouter) matchThen(url common.URL, param common.URL) bool {
return !(len(c.ThenCondition) == 0) && matchCondition(c.ThenCondition, &url, ¶m, nil)
}
func matchCondition(pairs map[string]MatchPair, url *common.URL, param *common.URL, invocation protocol.Invocation) bool {
sample := url.ToMap()
result := false
for key, matchPair := range pairs {
var sampleValue string
if invocation != nil && ((constant.METHOD_KEY == key) || (constant.METHOD_KEYS == key)) {
sampleValue = invocation.MethodName()
} else {
sampleValue = sample[key]
if &sampleValue == nil {
sampleValue = sample[constant.DEFAULT_KEY_PREFIX+key]
}
}
if &sampleValue != nil {
if !matchPair.isMatch(sampleValue, param) {
return false
} else {
result = true
}
} else {
if !(len(matchPair.Matches) == 0) {
return false
} else {
result = true
}
}
}
return result
}
func If(b bool, t, f interface{}) interface{} {
if b {
return t
}
return f
}
type MatchPair struct {
Matches map[string]interface{}
Mismatches map[string]interface{}
}
func (pair MatchPair) isMatch(s string, param *common.URL) bool {
return false
}