summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/common/route/route.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/prometheus/common/route/route.go')
-rw-r--r--vendor/github.com/prometheus/common/route/route.go50
1 files changed, 6 insertions, 44 deletions
diff --git a/vendor/github.com/prometheus/common/route/route.go b/vendor/github.com/prometheus/common/route/route.go
index 1e5638ed9..bb4688173 100644
--- a/vendor/github.com/prometheus/common/route/route.go
+++ b/vendor/github.com/prometheus/common/route/route.go
@@ -1,26 +1,12 @@
package route
import (
- "fmt"
"net/http"
- "sync"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
)
-var (
- mtx = sync.RWMutex{}
- ctxts = map[*http.Request]context.Context{}
-)
-
-// Context returns the context for the request.
-func Context(r *http.Request) context.Context {
- mtx.RLock()
- defer mtx.RUnlock()
- return ctxts[r]
-}
-
type param string
// Param returns param p for the context.
@@ -33,59 +19,35 @@ func WithParam(ctx context.Context, p, v string) context.Context {
return context.WithValue(ctx, param(p), v)
}
-// ContextFunc returns a new context for a request.
-type ContextFunc func(r *http.Request) (context.Context, error)
-
// Router wraps httprouter.Router and adds support for prefixed sub-routers
// and per-request context injections.
type Router struct {
rtr *httprouter.Router
prefix string
- ctxFn ContextFunc
}
// New returns a new Router.
-func New(ctxFn ContextFunc) *Router {
- if ctxFn == nil {
- ctxFn = func(r *http.Request) (context.Context, error) {
- return context.Background(), nil
- }
- }
+func New() *Router {
return &Router{
- rtr: httprouter.New(),
- ctxFn: ctxFn,
+ rtr: httprouter.New(),
}
}
// WithPrefix returns a router that prefixes all registered routes with prefix.
func (r *Router) WithPrefix(prefix string) *Router {
- return &Router{rtr: r.rtr, prefix: r.prefix + prefix, ctxFn: r.ctxFn}
+ return &Router{rtr: r.rtr, prefix: r.prefix + prefix}
}
// 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) {
- 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)
+ ctx, cancel := context.WithCancel(req.Context())
defer cancel()
for _, p := range params {
ctx = context.WithValue(ctx, param(p.Key), p.Value)
}
-
- mtx.Lock()
- ctxts[req] = ctx
- mtx.Unlock()
-
- h(w, req)
-
- mtx.Lock()
- delete(ctxts, req)
- mtx.Unlock()
+ h(w, req.WithContext(ctx))
}
}
@@ -132,7 +94,7 @@ func FileServe(dir string) http.HandlerFunc {
fs := http.FileServer(http.Dir(dir))
return func(w http.ResponseWriter, r *http.Request) {
- r.URL.Path = Param(Context(r), "filepath")
+ r.URL.Path = Param(r.Context(), "filepath")
fs.ServeHTTP(w, r)
}
}