-
Notifications
You must be signed in to change notification settings - Fork 33
/
context.go
49 lines (41 loc) · 1.27 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"net/http"
"time"
)
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "github.com/mozilla-services/autograph context value " + k.name
}
var (
// ctxReqID is the string identifier of a request ID in a context
contextKeyRequestID = contextKey{name: "reqID"}
// ctxReqStartTime is the string identifier of a timestamp that
// marks the beginning of processing of a request in a context
contextKeyRequestStartTime = contextKey{name: "reqStartTime"}
)
// addToContext add the given key value pair to the given request's context
func addToContext(r *http.Request, key contextKey, value interface{}) *http.Request {
ctx := r.Context()
return r.WithContext(context.WithValue(ctx, key, value))
}
// getRequestID retrieves an ID from the request context, or returns "-" is none is found
func getRequestID(r *http.Request) string {
val, ok := r.Context().Value(contextKeyRequestID).(string)
if ok {
return val
}
return "-"
}
// getRequestStartTime retrieves a start time from the request context,
// or returns the current time is none is found
func getRequestStartTime(r *http.Request) time.Time {
val, ok := r.Context().Value(contextKeyRequestStartTime).(time.Time)
if ok {
return val
}
return time.Now()
}