Merge pull request #2049 from prometheus/ctxfn-error

Update common/route vendoring for contextFn errors
This commit is contained in:
Tobias Schmidt 2016-10-03 12:06:46 +03:00 committed by GitHub
commit f07e67650e
3 changed files with 14 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package route
import (
"fmt"
"net/http"
"sync"
@ -32,7 +33,7 @@ func WithParam(ctx context.Context, p, v string) context.Context {
return context.WithValue(ctx, param(p), v)
}
type contextFn func(r *http.Request) context.Context
type contextFn func(r *http.Request) (context.Context, error)
// Router wraps httprouter.Router and adds support for prefixed sub-routers
// and per-request context injections.
@ -45,8 +46,8 @@ type Router struct {
// New returns a new Router.
func New(ctxFn contextFn) *Router {
if ctxFn == nil {
ctxFn = func(r *http.Request) context.Context {
return context.Background()
ctxFn = func(r *http.Request) (context.Context, error) {
return context.Background(), nil
}
}
return &Router{
@ -63,7 +64,12 @@ func (r *Router) WithPrefix(prefix string) *Router {
// handle turns a HandlerFunc into an httprouter.Handle.
func (r *Router) handle(h http.HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
ctx, cancel := context.WithCancel(r.ctxFn(req))
reqCtx, err := r.ctxFn(req)
if err != nil {
http.Error(w, fmt.Sprintf("Error creating request context: %v", err), http.StatusBadRequest)
return
}
ctx, cancel := context.WithCancel(reqCtx)
defer cancel()
for _, p := range params {

4
vendor/vendor.json vendored
View file

@ -211,8 +211,8 @@
{
"checksumSHA1": "CKVJRc1NREmfoAWQLHxqWQlvxo0=",
"path": "github.com/prometheus/common/route",
"revision": "ee21c31a8cbad2a29cce94a8c53855e1dab92bf7",
"revisionTime": "2016-09-21T02:27:24+02:00"
"revision": "a85e6c850a142b95b63f9dd0ffd9645e5e437da7",
"revisionTime": "2016-10-02T22:56:21+02:00"
},
{
"checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=",

View file

@ -120,8 +120,8 @@ type Options struct {
// New initializes a new web Handler.
func New(o *Options) *Handler {
router := route.New(func(r *http.Request) context.Context {
return o.Context
router := route.New(func(r *http.Request) (context.Context, error) {
return o.Context, nil
})
h := &Handler{