-
Notifications
You must be signed in to change notification settings - Fork 175
/
context.go
43 lines (35 loc) · 1.05 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
// Copyright (C) 2017 ScyllaDB
package scyllaclient
import (
"context"
"time"
)
// ctxt is a context key type.
type ctxt byte
// ctxt enumeration.
const (
ctxHost ctxt = iota
ctxNoRetry
ctxCustomTimeout
)
// forceHost makes hostPool middleware use the given host instead of selecting
// one.
func forceHost(ctx context.Context, host string) context.Context {
return context.WithValue(ctx, ctxHost, host)
}
// noRetry disables retries.
func noRetry(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxNoRetry, true)
}
// customTimeout allows to pass a custom timeout to timeout middleware.
//
// WARNING: Usually this is a workaround for Scylla or other API slowness
// in field condition i.e. with tons of data. This is the last resort of
// defense please use with care.
func customTimeout(ctx context.Context, d time.Duration) context.Context {
return context.WithValue(ctx, ctxCustomTimeout, d)
}
func hasCustomTimeout(ctx context.Context) (time.Duration, bool) {
v, ok := ctx.Value(ctxCustomTimeout).(time.Duration)
return v, ok
}