Performance enhancements for the rules matchers.
This commit is contained in:
parent
333b785061
commit
bf73127e0b
6 changed files with 192 additions and 35 deletions
|
@ -3,7 +3,6 @@ package rules
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
@ -13,6 +12,7 @@ import (
|
|||
"github.com/containous/mux"
|
||||
"github.com/containous/traefik/hostresolver"
|
||||
"github.com/containous/traefik/log"
|
||||
"github.com/containous/traefik/middlewares"
|
||||
"github.com/containous/traefik/types"
|
||||
)
|
||||
|
||||
|
@ -24,17 +24,20 @@ type Rules struct {
|
|||
}
|
||||
|
||||
func (r *Rules) host(hosts ...string) *mux.Route {
|
||||
for i, host := range hosts {
|
||||
hosts[i] = strings.ToLower(host)
|
||||
}
|
||||
|
||||
return r.Route.Route.MatcherFunc(func(req *http.Request, route *mux.RouteMatch) bool {
|
||||
reqHost, _, err := net.SplitHostPort(req.Host)
|
||||
if err != nil {
|
||||
reqHost = req.Host
|
||||
reqHost := middlewares.GetCanonizedHost(req.Context())
|
||||
if len(reqHost) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if r.HostResolver != nil && r.HostResolver.CnameFlattening {
|
||||
reqH, flatH := r.HostResolver.CNAMEFlatten(types.CanonicalDomain(reqHost))
|
||||
reqH, flatH := r.HostResolver.CNAMEFlatten(reqHost)
|
||||
for _, host := range hosts {
|
||||
if types.CanonicalDomain(reqH) == types.CanonicalDomain(host) ||
|
||||
types.CanonicalDomain(flatH) == types.CanonicalDomain(host) {
|
||||
if strings.EqualFold(reqH, host) || strings.EqualFold(flatH, host) {
|
||||
return true
|
||||
}
|
||||
log.Debugf("CNAMEFlattening: request %s which resolved to %s, is not matched to route %s", reqH, flatH, host)
|
||||
|
@ -43,7 +46,7 @@ func (r *Rules) host(hosts ...string) *mux.Route {
|
|||
}
|
||||
|
||||
for _, host := range hosts {
|
||||
if types.CanonicalDomain(reqHost) == types.CanonicalDomain(host) {
|
||||
if reqHost == host {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +57,7 @@ func (r *Rules) host(hosts ...string) *mux.Route {
|
|||
func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
|
||||
router := r.Route.Route.Subrouter()
|
||||
for _, host := range hosts {
|
||||
router.Host(types.CanonicalDomain(host))
|
||||
router.Host(strings.ToLower(host))
|
||||
}
|
||||
return r.Route.Route
|
||||
}
|
||||
|
@ -62,7 +65,7 @@ func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
|
|||
func (r *Rules) path(paths ...string) *mux.Route {
|
||||
router := r.Route.Route.Subrouter()
|
||||
for _, path := range paths {
|
||||
router.Path(strings.TrimSpace(path))
|
||||
router.Path(path)
|
||||
}
|
||||
return r.Route.Route
|
||||
}
|
||||
|
@ -76,14 +79,13 @@ func (r *Rules) pathPrefix(paths ...string) *mux.Route {
|
|||
}
|
||||
|
||||
func buildPath(path string, router *mux.Router) {
|
||||
cleanPath := strings.TrimSpace(path)
|
||||
// {} are used to define a regex pattern in http://www.gorillatoolkit.org/pkg/mux.
|
||||
// if we find a { in the path, that means we use regex, then the gorilla/mux implementation is chosen
|
||||
// otherwise, we use a lightweight implementation
|
||||
if strings.Contains(cleanPath, "{") {
|
||||
router.PathPrefix(cleanPath)
|
||||
if strings.Contains(path, "{") {
|
||||
router.PathPrefix(path)
|
||||
} else {
|
||||
m := &prefixMatcher{prefix: cleanPath}
|
||||
m := &prefixMatcher{prefix: path}
|
||||
router.NewRoute().MatcherFunc(m.Match)
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +119,7 @@ func (r *Rules) pathStripRegex(paths ...string) *mux.Route {
|
|||
r.Route.StripPrefixesRegex = paths
|
||||
router := r.Route.Route.Subrouter()
|
||||
for _, path := range paths {
|
||||
router.Path(strings.TrimSpace(path))
|
||||
router.Path(path)
|
||||
}
|
||||
return r.Route.Route
|
||||
}
|
||||
|
@ -158,7 +160,7 @@ func (r *Rules) pathPrefixStripRegex(paths ...string) *mux.Route {
|
|||
r.Route.StripPrefixesRegex = paths
|
||||
router := r.Route.Route.Subrouter()
|
||||
for _, path := range paths {
|
||||
router.PathPrefix(strings.TrimSpace(path))
|
||||
router.PathPrefix(path)
|
||||
}
|
||||
return r.Route.Route
|
||||
}
|
||||
|
@ -297,5 +299,5 @@ func (r *Rules) ParseDomains(expression string) ([]string, error) {
|
|||
return nil, fmt.Errorf("error parsing domains: %v", err)
|
||||
}
|
||||
|
||||
return fun.Map(types.CanonicalDomain, domains).([]string), nil
|
||||
return fun.Map(strings.ToLower, domains).([]string), nil
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/containous/mux"
|
||||
"github.com/containous/traefik/middlewares"
|
||||
"github.com/containous/traefik/testhelpers"
|
||||
"github.com/containous/traefik/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -13,41 +14,50 @@ import (
|
|||
)
|
||||
|
||||
func TestParseOneRule(t *testing.T) {
|
||||
router := mux.NewRouter()
|
||||
route := router.NewRoute()
|
||||
serverRoute := &types.ServerRoute{Route: route}
|
||||
rules := &Rules{Route: serverRoute}
|
||||
reqHostMid := &middlewares.RequestHost{}
|
||||
rules := &Rules{
|
||||
Route: &types.ServerRoute{
|
||||
Route: mux.NewRouter().NewRoute(),
|
||||
},
|
||||
}
|
||||
|
||||
expression := "Host:foo.bar"
|
||||
|
||||
routeResult, err := rules.Parse(expression)
|
||||
require.NoError(t, err, "Error while building route for %s", expression)
|
||||
|
||||
request := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar", nil)
|
||||
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
|
||||
|
||||
assert.True(t, routeMatch, "Rule %s don't match.", expression)
|
||||
reqHostMid.ServeHTTP(nil, request, func(w http.ResponseWriter, r *http.Request) {
|
||||
routeMatch := routeResult.Match(r, &mux.RouteMatch{Route: routeResult})
|
||||
assert.True(t, routeMatch, "Rule %s don't match.", expression)
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseTwoRules(t *testing.T) {
|
||||
router := mux.NewRouter()
|
||||
route := router.NewRoute()
|
||||
serverRoute := &types.ServerRoute{Route: route}
|
||||
rules := &Rules{Route: serverRoute}
|
||||
reqHostMid := &middlewares.RequestHost{}
|
||||
rules := &Rules{
|
||||
Route: &types.ServerRoute{
|
||||
Route: mux.NewRouter().NewRoute(),
|
||||
},
|
||||
}
|
||||
|
||||
expression := "Host: Foo.Bar ; Path:/FOObar"
|
||||
routeResult, err := rules.Parse(expression)
|
||||
|
||||
routeResult, err := rules.Parse(expression)
|
||||
require.NoError(t, err, "Error while building route for %s.", expression)
|
||||
|
||||
request := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/foobar", nil)
|
||||
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
|
||||
|
||||
assert.False(t, routeMatch, "Rule %s don't match.", expression)
|
||||
reqHostMid.ServeHTTP(nil, request, func(w http.ResponseWriter, r *http.Request) {
|
||||
routeMatch := routeResult.Match(r, &mux.RouteMatch{Route: routeResult})
|
||||
assert.False(t, routeMatch, "Rule %s don't match.", expression)
|
||||
})
|
||||
|
||||
request = testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/FOObar", nil)
|
||||
routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
|
||||
|
||||
assert.True(t, routeMatch, "Rule %s don't match.", expression)
|
||||
reqHostMid.ServeHTTP(nil, request, func(w http.ResponseWriter, r *http.Request) {
|
||||
routeMatch := routeResult.Match(r, &mux.RouteMatch{Route: routeResult})
|
||||
assert.True(t, routeMatch, "Rule %s don't match.", expression)
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseDomains(t *testing.T) {
|
||||
|
@ -91,6 +101,7 @@ func TestParseDomains(t *testing.T) {
|
|||
func TestPriorites(t *testing.T) {
|
||||
router := mux.NewRouter()
|
||||
router.StrictSlash(true)
|
||||
|
||||
rules := &Rules{Route: &types.ServerRoute{Route: router.NewRoute()}}
|
||||
expression01 := "PathPrefix:/foo"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue