forked from improbable-eng/go-httpwares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
41 lines (33 loc) · 981 Bytes
/
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
// Copyright 2016 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package http_retry
import (
"net/http"
"golang.org/x/net/context"
)
type ctxMarker struct{}
var (
ctxEnableRetry = &ctxMarker{}
)
// Enable turns on the retry logic for a given request, regardless of what the retry decider says.
//
// Please make sure you do not pass around this request's context.
func Enable(req *http.Request) *http.Request {
if isEnabled(req.Context()) {
return req
}
return req.WithContext(EnableContext(req.Context()))
}
// Enable turns on the retry logic for a given request's context, regardless of what the retry decider says.
//
// Please make sure you do not pass around this request's context.
func EnableContext(ctx context.Context) context.Context {
if isEnabled(ctx) {
return ctx
}
return context.WithValue(ctx, ctxEnableRetry, true)
}
func isEnabled(ctx context.Context) bool {
_, ok := ctx.Value(ctxEnableRetry).(bool)
return ok
}