forked from uber/ringpop-go
/
context_builder.go
170 lines (142 loc) · 5.18 KB
/
context_builder.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"time"
"golang.org/x/net/context"
)
// ContextBuilder stores all TChannel-specific parameters that will
// be stored inside of a context.
type ContextBuilder struct {
// If Timeout is zero, Build will default to defaultTimeout.
Timeout time.Duration
// Headers are application headers that json/thrift will encode into arg2.
Headers map[string]string
// CallOptions are TChannel call options for the specific call.
CallOptions *CallOptions
// TracingDisabled disables trace reporting for calls using this context.
TracingDisabled bool
// RetryOptions are the retry options for this call.
RetryOptions *RetryOptions
// Hidden fields: we do not want users outside of tchannel to set these.
incomingCall IncomingCall
span *Span
}
// NewContextBuilder returns a builder that can be used to create a Context.
func NewContextBuilder(timeout time.Duration) *ContextBuilder {
return &ContextBuilder{
Timeout: timeout,
span: NewRootSpan(),
}
}
// SetTimeout sets the timeout for the Context.
func (cb *ContextBuilder) SetTimeout(timeout time.Duration) *ContextBuilder {
cb.Timeout = timeout
return cb
}
// AddHeader adds a single application header to the Context.
func (cb *ContextBuilder) AddHeader(key, value string) *ContextBuilder {
if cb.Headers == nil {
cb.Headers = map[string]string{key: value}
} else {
cb.Headers[key] = value
}
return cb
}
// SetHeaders sets the application headers for this Context.
func (cb *ContextBuilder) SetHeaders(headers map[string]string) *ContextBuilder {
cb.Headers = headers
return cb
}
// SetShardKey sets the ShardKey call option ("sk" transport header).
func (cb *ContextBuilder) SetShardKey(sk string) *ContextBuilder {
if cb.CallOptions == nil {
cb.CallOptions = new(CallOptions)
}
cb.CallOptions.ShardKey = sk
return cb
}
// SetFormat sets the Format call option ("as" transport header).
func (cb *ContextBuilder) SetFormat(f Format) *ContextBuilder {
if cb.CallOptions == nil {
cb.CallOptions = new(CallOptions)
}
cb.CallOptions.Format = f
return cb
}
// SetRoutingDelegate sets the RoutingDelegate call options ("rd" transport header).
func (cb *ContextBuilder) SetRoutingDelegate(rd string) *ContextBuilder {
if cb.CallOptions == nil {
cb.CallOptions = new(CallOptions)
}
cb.CallOptions.RoutingDelegate = rd
return cb
}
// DisableTracing disables tracing.
func (cb *ContextBuilder) DisableTracing() *ContextBuilder {
cb.TracingDisabled = true
return cb
}
// SetIncomingCallForTest sets an IncomingCall in the context.
// This should only be used in unit tests.
func (cb *ContextBuilder) SetIncomingCallForTest(call IncomingCall) *ContextBuilder {
return cb.setIncomingCall(call)
}
// SetSpanForTest sets a tracing span in the context.
// This should only be used in unit tests.
func (cb *ContextBuilder) SetSpanForTest(span *Span) *ContextBuilder {
return cb.setSpan(span)
}
// SetRetryOptions sets RetryOptions in the context.
func (cb *ContextBuilder) SetRetryOptions(retryOptions *RetryOptions) *ContextBuilder {
cb.RetryOptions = retryOptions
return cb
}
// SetTimeoutPerAttempt sets TimeoutPerAttempt in RetryOptions.
func (cb *ContextBuilder) SetTimeoutPerAttempt(timeoutPerAttempt time.Duration) *ContextBuilder {
if cb.RetryOptions == nil {
cb.RetryOptions = &RetryOptions{}
}
cb.RetryOptions.TimeoutPerAttempt = timeoutPerAttempt
return cb
}
func (cb *ContextBuilder) setSpan(span *Span) *ContextBuilder {
cb.span = span
return cb
}
func (cb *ContextBuilder) setIncomingCall(call IncomingCall) *ContextBuilder {
cb.incomingCall = call
return cb
}
// Build returns a ContextWithHeaders that can be used to make calls.
func (cb *ContextBuilder) Build() (ContextWithHeaders, context.CancelFunc) {
timeout := cb.Timeout
if cb.TracingDisabled {
cb.span.EnableTracing(false)
}
params := &tchannelCtxParams{
options: cb.CallOptions,
span: cb.span,
call: cb.incomingCall,
retryOptions: cb.RetryOptions,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx = context.WithValue(ctx, contextKeyTChannel, params)
return WrapWithHeaders(ctx, cb.Headers), cancel
}