Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Jul 7, 2018
2 parents 8864ef0 + adbc77e commit ae9532e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
sudo: false
language: go
go:
- 1.7
- 1.8
- 1.9
- tip
- 1.10
before_install:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The router is optimized for high performance and a small memory footprint. It sc

**Parameters in your routing pattern:** Stop parsing the requested URL path, just give the path segment a name and the router delivers the dynamic value to you. Because of the design of the router, path parameters are very cheap.

**Zero Garbage:** The matching and dispatching process generates zero bytes of garbage. In fact, the only heap allocations that are made, is by building the slice of the key-value pairs for path parameters. If the request path contains no parameters, not a single heap allocation is necessary.
**Zero Garbage:** The matching and dispatching process generates zero bytes of garbage. The only heap allocations that are made are building the slice of the key-value pairs for path parameters, and building new context and request objects (the latter only in the standard `Handler`/`HandlerFunc` api). In the 3-argument API, if the request path contains no parameters not a single heap allocation is necessary.

**Best Performance:** [Benchmarks speak for themselves](https://github.com/acoshift/go-http-routing-benchmark). See below for technical details of the implementation.

Expand Down Expand Up @@ -110,7 +110,7 @@ Priority Path Handle

Every `*<num>` represents the memory address of a handler function (a pointer). If you follow a path trough the tree from the root to the leaf, you get the complete route path, e.g `\blog\:post\`, where `:post` is just a placeholder ([*parameter*](#named-parameters)) for an actual post name. Unlike hash-maps, a tree structure also allows us to use dynamic parts like the `:post` parameter, since we actually match against the routing patterns instead of just comparing hashes. [As benchmarks show](https://github.com/acoshift/go-http-routing-benchmark), this works very well and efficient.

Since URL paths have a hierarchical structure and make use only of a limited set of characters (byte values), it is very likely that there are a lot of common prefixes. This allows us to easily reduce the routing into ever smaller problems. Moreover the router manages a separate tree for every request method. For one thing it is more space efficient than holding a method->handle map in every single node, for another thing is also allows us to greatly reduce the routing problem before even starting the look-up in the prefix-tree.
Since URL paths have a hierarchical structure and make use only of a limited set of characters (byte values), it is very likely that there are a lot of common prefixes. This allows us to easily reduce the routing into ever smaller problems. Moreover the router manages a separate tree for every request method. For one thing it is more space efficient than holding a method->handle map in every single node, it also allows us to greatly reduce the routing problem before even starting the look-up in the prefix-tree.

For even better scalability, the child nodes on each tree level are ordered by priority, where the priority is just the number of handles registered in sub nodes (children, grandchildren, and so on..). This helps in two ways:

Expand Down
11 changes: 5 additions & 6 deletions context.go → params.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// +build go1.7

package httprouter

import "context"
import (
"context"
)

type paramsKey struct{}

// WithParams adds the params into the context. A modified context is returned.
func WithParams(parent context.Context, ps Params) context.Context {
return context.WithValue(parent, paramsKey{}, ps)
}

// GetParams gets params from context.
func GetParams(ctx context.Context) Params {
ps, _ := ctx.Value(paramsKey{}).(Params)
Expand Down
2 changes: 1 addition & 1 deletion path.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func CleanPath(p string) string {
buf[0] = '/'
}

trailing := n > 2 && p[n-1] == '/'
trailing := n > 1 && p[n-1] == '/'

// A bit more clunky without a 'lazybuf' like the path package, but the loop
// gets completely inlined (bufApp). So in contrast to the path package this
Expand Down
1 change: 1 addition & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var cleanTests = []struct {

// missing root
{"", "/"},
{"a/", "/a/"},
{"abc", "/abc"},
{"abc/def", "/abc/def"},
{"a/b/c", "/a/b/c"},
Expand Down
13 changes: 6 additions & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
package httprouter

import (
"context"
"net/http"
)

Expand Down Expand Up @@ -329,7 +330,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {

if root := r.trees[req.Method]; root != nil {
if handle, ps, tsr := root.getValue(path); handle != nil {
handle.ServeHTTP(w, req.WithContext(WithParams(req.Context(), ps)))
handle.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), paramsKey{}, ps)))
return
} else if req.Method != http.MethodConnect && path != "/" {
code := 301 // Permanent redirect, request with GET method
Expand Down Expand Up @@ -364,13 +365,11 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}

if req.Method == http.MethodOptions {
if req.Method == http.MethodOptions && r.HandleOptions {
// Handle OPTIONS requests
if r.HandleOptions {
if allow := r.allowed(path, req.Method); len(allow) > 0 {
w.Header().Set("Allow", allow)
return
}
if allow := r.allowed(path, req.Method); len(allow) > 0 {
w.Header().Set("Allow", allow)
return
}
} else {
// Handle 405
Expand Down

0 comments on commit ae9532e

Please sign in to comment.