forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_transport.go
193 lines (167 loc) · 6.16 KB
/
channel_transport.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2018 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 (
"errors"
"github.com/opentracing/opentracing-go"
"github.com/uber/tchannel-go"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/pkg/lifecycle"
"go.uber.org/zap"
)
var errChannelOrServiceNameIsRequired = errors.New(
"cannot instantiate tchannel.ChannelTransport: " +
"please provide a service name with the ServiceName option " +
"or an existing Channel with the WithChannel option")
// NewChannelTransport is a YARPC transport that facilitates sending and
// receiving YARPC requests through TChannel. It uses a shared TChannel
// Channel for both, incoming and outgoing requests, ensuring reuse of
// connections and other resources.
//
// Either the local service name (with the ServiceName option) or a user-owned
// TChannel (with the WithChannel option) MUST be specified.
//
// ChannelTransport uses the underlying TChannel Channel for load balancing
// and peer managament.
// Use NewTransport and its NewOutbound to support YARPC peer.Choosers.
func NewChannelTransport(opts ...TransportOption) (*ChannelTransport, error) {
var options transportOptions
options.tracer = opentracing.GlobalTracer()
for _, opt := range opts {
opt(&options)
}
// Attempt to construct a channel on behalf of the caller if none given.
// Defer the error until Start since NewChannelTransport does not have
// an error return.
var err error
ch := options.ch
if ch == nil {
if options.name == "" {
err = errChannelOrServiceNameIsRequired
} else {
opts := tchannel.ChannelOptions{Tracer: options.tracer}
ch, err = tchannel.NewChannel(options.name, &opts)
options.ch = ch
}
}
if err != nil {
return nil, err
}
return options.newChannelTransport(), nil
}
func (options transportOptions) newChannelTransport() *ChannelTransport {
logger := options.logger
if logger == nil {
logger = zap.NewNop()
}
return &ChannelTransport{
once: lifecycle.NewOnce(),
ch: options.ch,
addr: options.addr,
tracer: options.tracer,
logger: logger.Named("tchannel"),
originalHeaders: options.originalHeaders,
newResponseWriter: newHandlerWriter,
}
}
// ChannelTransport maintains TChannel peers and creates inbounds and outbounds for
// TChannel.
// If you have a YARPC peer.Chooser, use the unqualified tchannel.Transport
// instead.
type ChannelTransport struct {
once *lifecycle.Once
ch Channel
name string
addr string
tracer opentracing.Tracer
logger *zap.Logger
router transport.Router
originalHeaders bool
newResponseWriter func(inboundCallResponse, tchannel.Format, headerCase) responseWriter
}
// Channel returns the underlying TChannel "Channel" instance.
func (t *ChannelTransport) Channel() Channel {
return t.ch
}
// ListenAddr exposes the listen address of the transport.
func (t *ChannelTransport) ListenAddr() string {
return t.addr
}
// Start starts the TChannel transport. This starts making connections and
// accepting inbound requests. All inbounds must have been assigned a router
// to accept inbound requests before this is called.
func (t *ChannelTransport) Start() error {
return t.once.Start(t.start)
}
func (t *ChannelTransport) start() error {
if t.router != nil {
// Set up handlers. This must occur after construction because the
// dispatcher, or its equivalent, calls SetRouter before Start.
// This also means that SetRouter should be called on every inbound
// before calling Start on any transport or inbound.
services := make(map[string]struct{})
for _, p := range t.router.Procedures() {
services[p.Service] = struct{}{}
}
for s := range services {
sc := t.ch.GetSubChannel(s)
existing := sc.GetHandlers()
sc.SetHandler(handler{existing: existing, router: t.router, tracer: t.tracer, logger: t.logger, newResponseWriter: t.newResponseWriter})
}
}
if t.ch.State() == tchannel.ChannelListening {
// Channel.Start() was called before RPC.Start(). We still want to
// update the Handler and what t.addr means, but nothing else.
t.addr = t.ch.PeerInfo().HostPort
return nil
}
// Default to ListenIP if addr wasn't given.
addr := t.addr
if addr == "" {
listenIP, err := tchannel.ListenIP()
if err != nil {
return err
}
addr = listenIP.String() + ":0"
// TODO(abg): Find a way to export this to users
}
// TODO(abg): If addr was just the port (":4040"), we want to use
// ListenIP() + ":4040" rather than just ":4040".
if err := t.ch.ListenAndServe(addr); err != nil {
return err
}
t.addr = t.ch.PeerInfo().HostPort
return nil
}
// Stop stops the TChannel transport. It starts rejecting incoming requests
// and draining connections before closing them.
// In a future version of YARPC, Stop will block until the underlying channel
// has closed completely.
func (t *ChannelTransport) Stop() error {
return t.once.Stop(t.stop)
}
func (t *ChannelTransport) stop() error {
t.ch.Close()
return nil
}
// IsRunning returns whether the ChannelTransport is running.
func (t *ChannelTransport) IsRunning() bool {
return t.once.IsRunning()
}