/
options.go
165 lines (149 loc) · 5.96 KB
/
options.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
// Copyright (c) 2017 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"
"github.com/opentracing/opentracing-go"
backoffapi "go.uber.org/yarpc/api/backoff"
"go.uber.org/yarpc/internal/backoff"
"go.uber.org/zap"
)
// Option allows customizing the YARPC TChannel transport.
// TransportSpec() accepts any TransportOption, and may in the future also
// accept inbound and outbound options.
type Option interface {
tchannelOption()
}
var _ Option = (TransportOption)(nil)
// transportOptions is suitable for conveying options to TChannel transport
// constructors (NewTransport and NewChannelTransport).
// At time of writing, there is only a ChannelTransport constructor, which
// supports options like WithChannel that only apply to this constructor form.
// The transportOptions should also be suitable, albeit with extraneous properties,
// if used for NewTransport, which will return a Transport suitable for YARPC
// peer lists.
// TODO update above when NewTransport is real.
type transportOptions struct {
ch Channel
tracer opentracing.Tracer
logger *zap.Logger
addr string
name string
connTimeout time.Duration
connBackoffStrategy backoffapi.Strategy
}
// newTransportOptions constructs the default transport options struct
func newTransportOptions() transportOptions {
return transportOptions{
tracer: opentracing.GlobalTracer(),
connTimeout: defaultConnTimeout,
connBackoffStrategy: backoff.DefaultExponential,
}
}
// TransportOption customizes the behavior of a TChannel Transport.
type TransportOption func(*transportOptions)
// TransportOption makes all TransportOptions recognizeable as Option so
// TransportSpec will accept them.
func (TransportOption) tchannelOption() {}
// Tracer specifies the request tracer used for RPCs passing through the
// TChannel transport.
func Tracer(tracer opentracing.Tracer) TransportOption {
return func(t *transportOptions) {
t.tracer = tracer
}
}
// Logger sets a logger to use for internal logging.
//
// The default is to not write any logs.
func Logger(logger *zap.Logger) TransportOption {
return func(t *transportOptions) {
t.logger = logger
}
}
// WithChannel specifies the TChannel Channel to use to send and receive YARPC
// requests. The instance may already have handlers registered against it;
// these will be left unchanged.
//
// If this option is not passed, the Transport will build and manage its own
// Channel. The behavior of that TChannel may be customized using the
// ListenAddr and ServiceName options.
//
// This option is disallowed for NewTransport and transports constructed with
// the YARPC configuration system.
func WithChannel(ch Channel) TransportOption {
return func(options *transportOptions) {
options.ch = ch
}
}
// ListenAddr specifies the port the TChannel should listen on. This defaults
// to ":0" (all interfaces, OS-assigned port).
//
// transport := NewChannelTransport(ServiceName("myservice"), ListenAddr(":4040"))
//
// This option has no effect if WithChannel was used and the TChannel was
// already listening, and it is disallowed for transports constructed with the
// YARPC configuration system.
func ListenAddr(addr string) TransportOption {
return func(options *transportOptions) {
options.addr = addr
}
}
// ServiceName informs the NewChannelTransport constructor which service
// name to use if it needs to construct a root Channel object, as when called
// without the WithChannel option.
//
// ServiceName specifies the name of the current service for the YARPC-owned
// TChannel Channel. If the WithChannel option is not specified, the TChannel
// Transport will build its own TChannel Chanel and use this name for that
// Channel.
//
// This option has no effect if WithChannel was used, and it is disallowed for
// transports constructed with the YARPC configuration system.
func ServiceName(name string) TransportOption {
return func(options *transportOptions) {
options.name = name
}
}
// ConnTimeout specifies the time that TChannel will wait for a
// connection attempt to any retained peer.
//
// The default is half of a second.
func ConnTimeout(d time.Duration) TransportOption {
return func(options *transportOptions) {
options.connTimeout = d
}
}
// ConnBackoff specifies the connection backoff strategy for delays between
// connection attempts for each peer.
//
// ConnBackoff accepts a function that creates new backoff instances.
// The transport uses this to make referentially independent backoff instances
// that will not be shared across goroutines.
//
// The backoff instance is a function that accepts connection attempts and
// returns a duration.
//
// The default is exponential backoff starting with 10ms fully jittered,
// doubling each attempt, with a maximum interval of 30s.
func ConnBackoff(s backoffapi.Strategy) TransportOption {
return func(options *transportOptions) {
options.connBackoffStrategy = s
}
}