Merge pull request #4064 from prometheus/beorn7/vendoring

Update vendoring of prometheus/common/route to include data race fix
This commit is contained in:
Björn Rabenstein 2018-04-09 17:50:06 +02:00 committed by GitHub
commit e65fc8591a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 12 deletions

View file

@ -19,11 +19,12 @@ func WithParam(ctx context.Context, p, v string) context.Context {
return context.WithValue(ctx, param(p), v) return context.WithValue(ctx, param(p), v)
} }
// Router wraps httprouter.Router and adds support for prefixed sub-routers // Router wraps httprouter.Router and adds support for prefixed sub-routers,
// and per-request context injections. // per-request context injections and instrumentation.
type Router struct { type Router struct {
rtr *httprouter.Router rtr *httprouter.Router
prefix string prefix string
instrh func(handlerName string, handler http.HandlerFunc) http.HandlerFunc
} }
// New returns a new Router. // New returns a new Router.
@ -33,13 +34,22 @@ func New() *Router {
} }
} }
// WithInstrumentation returns a router with instrumentation support.
func (r *Router) WithInstrumentation(instrh func(handlerName string, handler http.HandlerFunc) http.HandlerFunc) *Router {
return &Router{rtr: r.rtr, prefix: r.prefix, instrh: instrh}
}
// WithPrefix returns a router that prefixes all registered routes with prefix. // WithPrefix returns a router that prefixes all registered routes with prefix.
func (r *Router) WithPrefix(prefix string) *Router { func (r *Router) WithPrefix(prefix string) *Router {
return &Router{rtr: r.rtr, prefix: r.prefix + prefix} return &Router{rtr: r.rtr, prefix: r.prefix + prefix, instrh: r.instrh}
} }
// handle turns a HandlerFunc into an httprouter.Handle. // handle turns a HandlerFunc into an httprouter.Handle.
func (r *Router) handle(h http.HandlerFunc) httprouter.Handle { func (r *Router) handle(handlerName string, h http.HandlerFunc) httprouter.Handle {
if r.instrh != nil {
// This needs to be outside the closure to avoid data race when reading and writing to 'h'.
h = r.instrh(handlerName, h)
}
return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
ctx, cancel := context.WithCancel(req.Context()) ctx, cancel := context.WithCancel(req.Context())
defer cancel() defer cancel()
@ -53,27 +63,27 @@ func (r *Router) handle(h http.HandlerFunc) httprouter.Handle {
// Get registers a new GET route. // Get registers a new GET route.
func (r *Router) Get(path string, h http.HandlerFunc) { func (r *Router) Get(path string, h http.HandlerFunc) {
r.rtr.GET(r.prefix+path, r.handle(h)) r.rtr.GET(r.prefix+path, r.handle(path, h))
} }
// Options registers a new OPTIONS route. // Options registers a new OPTIONS route.
func (r *Router) Options(path string, h http.HandlerFunc) { func (r *Router) Options(path string, h http.HandlerFunc) {
r.rtr.OPTIONS(r.prefix+path, r.handle(h)) r.rtr.OPTIONS(r.prefix+path, r.handle(path, h))
} }
// Del registers a new DELETE route. // Del registers a new DELETE route.
func (r *Router) Del(path string, h http.HandlerFunc) { func (r *Router) Del(path string, h http.HandlerFunc) {
r.rtr.DELETE(r.prefix+path, r.handle(h)) r.rtr.DELETE(r.prefix+path, r.handle(path, h))
} }
// Put registers a new PUT route. // Put registers a new PUT route.
func (r *Router) Put(path string, h http.HandlerFunc) { func (r *Router) Put(path string, h http.HandlerFunc) {
r.rtr.PUT(r.prefix+path, r.handle(h)) r.rtr.PUT(r.prefix+path, r.handle(path, h))
} }
// Post registers a new POST route. // Post registers a new POST route.
func (r *Router) Post(path string, h http.HandlerFunc) { func (r *Router) Post(path string, h http.HandlerFunc) {
r.rtr.POST(r.prefix+path, r.handle(h)) r.rtr.POST(r.prefix+path, r.handle(path, h))
} }
// Redirect takes an absolute path and sends an internal HTTP redirect for it, // Redirect takes an absolute path and sends an internal HTTP redirect for it,

6
vendor/vendor.json vendored
View file

@ -782,10 +782,10 @@
"revisionTime": "2017-11-04T09:59:07Z" "revisionTime": "2017-11-04T09:59:07Z"
}, },
{ {
"checksumSHA1": "9aDxDuzZt1l7FQJ9qpn2kPcF7NU=", "checksumSHA1": "9doPk0x0LONG/idxK61JnZYcxBs=",
"path": "github.com/prometheus/common/route", "path": "github.com/prometheus/common/route",
"revision": "e3fb1a1acd7605367a2b378bc2e2f893c05174b7", "revision": "38c53a9f4bfcd932d1b00bfc65e256a7fba6b37a",
"revisionTime": "2017-11-04T09:59:07Z" "revisionTime": "2018-03-26T16:04:09Z"
}, },
{ {
"checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=", "checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=",