Skip to content

Add new & improved glob matcher#457

Merged
magiconair merged 10 commits into
fabiolb:masterfrom
sharbov:sharbov/glob_improvements
Mar 11, 2018
Merged

Add new & improved glob matcher#457
magiconair merged 10 commits into
fabiolb:masterfrom
sharbov:sharbov/glob_improvements

Conversation

@sharbov

@sharbov sharbov commented Feb 28, 2018

Copy link
Copy Markdown
Contributor

I've added a new matcher using gobwas/glob side-by-side to the existing glob matcher

This might hinder performance due to the fact that the matcher is compiling the pattern each time.

I was thinking of using a cache for the compiled matchers (something like patrickmn/go-cache).

@magiconair what do you think?

#452

@CLAassistant

CLAassistant commented Feb 28, 2018

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@sharbov sharbov force-pushed the sharbov/glob_improvements branch from f65d00b to 48dbe18 Compare February 28, 2018 13:10

@magiconair magiconair left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can cache the compiled pattern in the Route object and update the addRoute method in the Table

https://github.com/fabiolb/fabio/blob/master/route/table.go#L137-L173

Also, If this glob library is backwards-compatible with https://github.com/ryanuber/go-glob then I would suggest to just replace it instead of adding another one. Can you check?

@sharbov

sharbov commented Mar 4, 2018

Copy link
Copy Markdown
Contributor Author

Hey @magiconair,

I've implemented the fixes you've requested.
As far as I can tell the two glob packages are compatible,
I used the same test to validate it (and extended it to test the new capabilities).

Comment thread route/matcher_test.go Outdated
})
}
} No newline at end of file
func getRoute(path string) *Route {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer if you compile the glob matcher in the test since this keeps the changeset smaller, e.g.

for _, tt := range tests {
    t.Run(tt.uri, func(t *testing.T) {
        tt.route.Matcher = glob.MustCompile(tt.route.path)
        ...
    })
}

Comment thread route/route.go Outdated
total uint64

// Matcher represents compiled pattern.
Matcher glob.Glob

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think Glob is a better name than Matcher.

Comment thread route/table.go Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please name this glob and also remove the github.com/ryanuber/go-glob library in a separate commit.

Comment thread route/table.go Outdated
// add new host
case t[host] == nil:
r := &Route{Host: host, Path: path}
r := &Route{Host: host, Path: path, Matcher: glob2.MustCompile(path)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens if this is an invalid pattern? Does it panic? You probably need to catch the error and return it.

Comment thread route/table.go
host := normalizeHost(req.Host, req.TLS != nil)
for pattern := range t {
normpat := normalizeHost(pattern, req.TLS != nil)
if glob.Glob(normpat, host) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants