-
Notifications
You must be signed in to change notification settings - Fork 83
/
subchannel.go
220 lines (189 loc) · 6.58 KB
/
subchannel.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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 (
"fmt"
"sync"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
)
// SubChannelOption are used to set options for subchannels.
type SubChannelOption func(*SubChannel)
// Isolated is a SubChannelOption that creates an isolated subchannel.
func Isolated(s *SubChannel) {
s.Lock()
s.peers = s.topChannel.peers.newSibling()
s.peers.SetStrategy(newLeastPendingCalculator())
s.Unlock()
}
// SubChannel allows calling a specific service on a channel.
// TODO(prashant): Allow creating a subchannel with default call options.
// TODO(prashant): Allow registering handlers on a subchannel.
type SubChannel struct {
sync.RWMutex
serviceName string
topChannel *Channel
defaultCallOptions *CallOptions
peers *PeerList
handler Handler
logger Logger
statsReporter StatsReporter
}
// Map of subchannel and the corresponding service
type subChannelMap struct {
sync.RWMutex
subchannels map[string]*SubChannel
}
func newSubChannel(serviceName string, ch *Channel) *SubChannel {
logger := ch.Logger().WithFields(LogField{"subchannel", serviceName})
return &SubChannel{
serviceName: serviceName,
peers: ch.peers,
topChannel: ch,
handler: &handlerMap{}, // use handlerMap by default
logger: logger,
statsReporter: ch.StatsReporter(),
}
}
// ServiceName returns the service name that this subchannel is for.
func (c *SubChannel) ServiceName() string {
return c.serviceName
}
// BeginCall starts a new call to a remote peer, returning an OutboundCall that can
// be used to write the arguments of the call.
func (c *SubChannel) BeginCall(ctx context.Context, methodName string, callOptions *CallOptions) (*OutboundCall, error) {
if callOptions == nil {
callOptions = defaultCallOptions
}
peer, err := c.peers.Get(callOptions.RequestState.PrevSelectedPeers())
if err != nil {
return nil, err
}
return peer.BeginCall(ctx, c.ServiceName(), methodName, callOptions)
}
// Peers returns the PeerList for this subchannel.
func (c *SubChannel) Peers() *PeerList {
return c.peers
}
// Isolated returns whether this subchannel is an isolated subchannel.
func (c *SubChannel) Isolated() bool {
c.RLock()
defer c.RUnlock()
return c.topChannel.Peers() != c.peers
}
// Register registers a handler on the subchannel for the given method.
//
// This function panics if the Handler for the SubChannel was overwritten with
// SetHandler.
func (c *SubChannel) Register(h Handler, methodName string) {
handlers, ok := c.handler.(*handlerMap)
if !ok {
panic(fmt.Sprintf(
"handler for SubChannel(%v) was changed to disallow method registration",
c.ServiceName(),
))
}
handlers.register(h, methodName)
}
// GetHandlers returns all handlers registered on this subchannel by method name.
//
// This function panics if the Handler for the SubChannel was overwritten with
// SetHandler.
func (c *SubChannel) GetHandlers() map[string]Handler {
handlers, ok := c.handler.(*handlerMap)
if !ok {
panic(fmt.Sprintf(
"handler for SubChannel(%v) was changed to disallow method registration",
c.ServiceName(),
))
}
handlers.RLock()
handlersMap := make(map[string]Handler, len(handlers.handlers))
for k, v := range handlers.handlers {
handlersMap[k] = v
}
handlers.RUnlock()
return handlersMap
}
// SetHandler changes the SubChannel's underlying handler. This may be used to
// set up a catch-all Handler for all requests received by this SubChannel.
//
// Methods registered on this SubChannel using Register() before calling
// SetHandler() will be forgotten. Further calls to Register() on this
// SubChannel after SetHandler() is called will cause panics.
func (c *SubChannel) SetHandler(h Handler) {
c.handler = h
}
// Logger returns the logger for this subchannel.
func (c *SubChannel) Logger() Logger {
return c.logger
}
// StatsReporter returns the stats reporter for this subchannel.
func (c *SubChannel) StatsReporter() StatsReporter {
return c.topChannel.StatsReporter()
}
// StatsTags returns the stats tags for this subchannel.
func (c *SubChannel) StatsTags() map[string]string {
tags := c.topChannel.StatsTags()
tags["subchannel"] = c.serviceName
return tags
}
// Tracer returns OpenTracing Tracer from the top channel.
func (c *SubChannel) Tracer() opentracing.Tracer {
return c.topChannel.Tracer()
}
// Register a new subchannel for the given serviceName
func (subChMap *subChannelMap) registerNewSubChannel(serviceName string, ch *Channel) (_ *SubChannel, added bool) {
subChMap.Lock()
defer subChMap.Unlock()
if subChMap.subchannels == nil {
subChMap.subchannels = make(map[string]*SubChannel)
}
if sc, ok := subChMap.subchannels[serviceName]; ok {
return sc, false
}
sc := newSubChannel(serviceName, ch)
subChMap.subchannels[serviceName] = sc
return sc, true
}
// Get subchannel if, we have one
func (subChMap *subChannelMap) get(serviceName string) (*SubChannel, bool) {
subChMap.RLock()
sc, ok := subChMap.subchannels[serviceName]
subChMap.RUnlock()
return sc, ok
}
// GetOrAdd a subchannel for the given serviceName on the map
func (subChMap *subChannelMap) getOrAdd(serviceName string, ch *Channel) (_ *SubChannel, added bool) {
if sc, ok := subChMap.get(serviceName); ok {
return sc, false
}
return subChMap.registerNewSubChannel(serviceName, ch)
}
func (subChMap *subChannelMap) updatePeer(p *Peer) {
subChMap.RLock()
for _, subCh := range subChMap.subchannels {
if subCh.Isolated() {
subCh.RLock()
subCh.Peers().onPeerChange(p)
subCh.RUnlock()
}
}
subChMap.RUnlock()
}