Skip to content

Commit

Permalink
udpate topicmapper policy route from map to array to eliminate random…
Browse files Browse the repository at this point in the history
… order (easegress-io#622)
  • Loading branch information
suchen-sci authored and localvar committed Jun 13, 2022
1 parent da4ba22 commit d882747
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pkg/filters/topicmapper/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ import (

type topicMapFunc func(mqttTopic string) (topic string, headers map[string]string, err error)

func getPolicyRoute(routes []*PolicyRe) map[string]*regexp.Regexp {
ans := make(map[string]*regexp.Regexp)
type policyRe struct {
name string
re *regexp.Regexp
}

func getPolicyRoute(routes []*PolicyRe) []*policyRe {
res := make([]*policyRe, 0, len(routes))
for _, route := range routes {
r, err := regexp.Compile(route.MatchExpr)
if err != nil {
logger.SpanErrorf(nil, "topicMapper policy <%s> match expr <%s> compile failed: %v", route.Name, route.MatchExpr, err)
} else {
ans[route.Name] = r
res = append(res, &policyRe{name: route.Name, re: r})
}
}
return ans
return res
}

type topicRouteType map[string][]*regexp.Regexp
Expand Down Expand Up @@ -77,9 +82,9 @@ func getTopicMapFunc(topicMapper *Spec) topicMapFunc {
}

getPolicy := func(level string) *Policy {
for name, r := range policyRoute {
if r.MatchString(level) {
return policyMap[name]
for _, route := range policyRoute {
if route.re.MatchString(level) {
return policyMap[route.name]
}
}
return nil
Expand Down

0 comments on commit d882747

Please sign in to comment.