-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
32 lines (26 loc) · 1.05 KB
/
context.go
File metadata and controls
32 lines (26 loc) · 1.05 KB
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
package nicehttp
import (
"context"
"time"
)
// Define key types to avoid collisions
type maxAttemptsKey struct{}
type attemptTimeoutKey struct{}
// SetAttemptTimeoutInContext returns a new context with the given timeout value.
func SetAttemptTimeoutInContext(parent context.Context, timeout time.Duration) context.Context {
return context.WithValue(parent, attemptTimeoutKey{}, timeout)
}
// GetAttemptTimeoutFromContext retrieves the timeout value from the context.
func GetAttemptTimeoutFromContext(ctx context.Context) (time.Duration, bool) {
val, ok := ctx.Value(attemptTimeoutKey{}).(time.Duration)
return val, ok
}
// SetMaxAttemptsInContext returns a new context with the given max attempts value.
func SetMaxAttemptsInContext(parent context.Context, attempts int) context.Context {
return context.WithValue(parent, maxAttemptsKey{}, attempts)
}
// GetMaxAttemptsFromContext retrieves the max attempts value from the context.
func GetMaxAttemptsFromContext(ctx context.Context) (int, bool) {
val, ok := ctx.Value(maxAttemptsKey{}).(int)
return val, ok
}