@@ -30,11 +30,17 @@ var ErrGroupExisted = errors.New("Group Existed")
30
30
// ErrParamsKeyEmpty is the error returned by HandleFunc when the params key is empty.
31
31
var ErrParamsKeyEmpty = errors .New ("Params key must be not empty" )
32
32
33
- // ContextKey represents the context key.
34
- type ContextKey string
33
+ // contextKey is a key for use with context.WithValue. It's used as
34
+ // a pointer so it fits in an interface{} without allocation.
35
+ type contextKey struct {
36
+ name string
37
+ }
38
+
39
+ // String returns a context key.
40
+ func (k * contextKey ) String () string { return "github.com/hslam/rum context key " + k .name }
35
41
36
- // ContextKeyRecovery is the context key of recovery .
37
- const ContextKeyRecovery = ContextKey ( "mux:context: recovery")
42
+ // RecoveryContextKey is a context key.
43
+ var RecoveryContextKey = & contextKey { " recovery"}
38
44
39
45
// Mux is an HTTP request multiplexer.
40
46
type Mux struct {
@@ -144,7 +150,7 @@ func (m *Mux) serveEntry(entry *Entry, w http.ResponseWriter, r *http.Request) {
144
150
145
151
// Recovery returns a recovery handler function that recovers from any panics and writes a 500 status code.
146
152
func Recovery (w http.ResponseWriter , r * http.Request ) {
147
- err := r .Context ().Value (ContextKeyRecovery )
153
+ err := r .Context ().Value (RecoveryContextKey )
148
154
w .Header ().Set ("Content-Type" , "text/plain; charset=utf-8" )
149
155
w .Header ().Set ("X-Content-Type-Options" , "nosniff" )
150
156
w .WriteHeader (http .StatusInternalServerError )
@@ -155,7 +161,7 @@ func (m *Mux) serveHandler(handler http.Handler, w http.ResponseWriter, r *http.
155
161
if m .recovery != nil {
156
162
defer func () {
157
163
if err := recover (); err != nil {
158
- ctx := context .WithValue (r .Context (), ContextKeyRecovery , err )
164
+ ctx := context .WithValue (r .Context (), RecoveryContextKey , err )
159
165
m .recovery .ServeHTTP (w , r .WithContext (ctx ))
160
166
}
161
167
}()
0 commit comments