Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new & improved glob matcher #457

Merged
merged 10 commits into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions route/matcher.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package route

import (
"log"
"path"
"strings"
)

Expand All @@ -21,12 +19,7 @@ func prefixMatcher(uri string, r *Route) bool {
return strings.HasPrefix(uri, r.Path)
}

// globMatcher matches path to the routes' path using globbing.
// globMatcher matches path to the routes' path using gobwas/glob.
func globMatcher(uri string, r *Route) bool {
var hasMatch, err = path.Match(r.Path, uri)
if err != nil {
log.Printf("[ERROR] Glob matching error %s for path %s route %s", err, uri, r.Path)
return false
}
return hasMatch
return r.Glob.Match(uri)
}
6 changes: 6 additions & 0 deletions route/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package route

import (
"testing"

"github.com/gobwas/glob"
)

func TestPrefixMatcher(t *testing.T) {
Expand Down Expand Up @@ -38,16 +40,20 @@ func TestGlobMatcher(t *testing.T) {
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/foo/x/bar", matches: true, route: &Route{Path: "/foo/*/bar"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**/bar"}},

// error flows
{uri: "/fo", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fo", matches: false, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo.*"}},
{uri: "/foo/x/y/z/w/baz", matches: false, route: &Route{Path: "/foo/**/bar"}},
}

for _, tt := range tests {
t.Run(tt.uri, func(t *testing.T) {
tt.route.Glob = glob.MustCompile(tt.route.Path)
if got, want := globMatcher(tt.uri, tt.route), tt.matches; got != want {
t.Fatalf("got %v want %v", got, want)
}
Expand Down
4 changes: 4 additions & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/fabiolb/fabio/metrics"
"github.com/gobwas/glob"
)

// Route maps a path prefix to one or more target URLs.
Expand All @@ -36,6 +37,9 @@ type Route struct {
// total contains the total number of requests for this route.
// Used by the RRPicker
total uint64

// Glob represents compiled pattern.
Glob glob.Glob
}

func (r *Route) addTarget(service string, targetURL *url.URL, fixedWeight float64, tags []string, opts map[string]string) {
Expand Down
17 changes: 13 additions & 4 deletions route/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sync/atomic"

"github.com/fabiolb/fabio/metrics"
"github.com/ryanuber/go-glob"
"github.com/gobwas/glob"
)

var errInvalidPrefix = errors.New("route: prefix must not be empty")
Expand Down Expand Up @@ -153,13 +153,21 @@ func (t Table) addRoute(d *RouteDef) error {
switch {
// add new host
case t[host] == nil:
r := &Route{Host: host, Path: path}
g, err := glob.Compile(path)
if err != nil {
return err
}
r := &Route{Host: host, Path: path, Glob: g}
r.addTarget(d.Service, targetURL, d.Weight, d.Tags, d.Opts)
t[host] = Routes{r}

// add new route to existing host
case t[host].find(path) == nil:
r := &Route{Host: host, Path: path}
g, err := glob.Compile(path)
if err != nil {
return err
}
r := &Route{Host: host, Path: path, Glob: g}
r.addTarget(d.Service, targetURL, d.Weight, d.Tags, d.Opts)
t[host] = append(t[host], r)
sort.Sort(t[host])
Expand Down Expand Up @@ -290,7 +298,8 @@ func (t Table) matchingHosts(req *http.Request) (hosts []string) {
host := normalizeHost(req.Host, req.TLS != nil)
for pattern := range t {
normpat := normalizeHost(pattern, req.TLS != nil)
if glob.Glob(normpat, host) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you used the glob matcher here in the first place, can a host contain wildcards ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'll check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can have a route for *.example.com/foo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a small benchmark to verify performance. gobwas is fast but only when the expression is pre-compiled. It is only 1.4 ms but this sits in the hot-path. However, there is no easy way to refactor this since Table is just a map[string]Routes. Might be a good time to finally change this to a struct. I'll have a look.

$ go test -bench .
goos: darwin
goarch: amd64
pkg: globbench
BenchmarkGlob/ryanuber-8         	20000000	        91.4 ns/op
BenchmarkGlob/gobwas-8           	 1000000	      1385 ns/op
BenchmarkGlob/gobwas-precompile-8         	200000000	         7.29 ns/op
PASS
ok  	globbench	5.537s
package main

import "testing"
import ryanuber "github.com/ryanuber/go-glob"
import gobwas "github.com/gobwas/glob"

var n int

func BenchmarkGlob(b *testing.B) {
	b.Run("ryanuber", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			if ryanuber.Glob("*.example.com", "foo.example.com") {
				n++
			}
		}
	})
	b.Run("gobwas", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			m := gobwas.MustCompile("*.example.com")
			if m.Match("foo.example.com") {
				n++
			}
		}
	})
	b.Run("gobwas-precompile", func(b *testing.B) {
		m := gobwas.MustCompile("*.example.com")
		for i := 0; i < b.N; i++ {
			if m.Match("foo.example.com") {
				n++
			}
		}
	})
	b.Log(n)
}

g := glob.MustCompile(normpat)
if g.Match(host) {
hosts = append(hosts, pattern)
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/gobwas/glob/bench.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading