forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmex.go
246 lines (205 loc) · 8 KB
/
mex.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 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 (
"errors"
"sync"
"github.com/uber/tchannel-go/typed"
"golang.org/x/net/context"
)
var (
errDuplicateMex = errors.New("multiple attempts to use the message id")
errMexChannelFull = NewSystemError(ErrCodeBusy, "cannot send frame to message exchange channel")
errUnexpectedFrameType = errors.New("unexpected frame received")
)
const (
messageExchangeSetInbound = "inbound"
messageExchangeSetOutbound = "outbound"
// mexChannelBufferSize is the size of the message exchange channel buffer.
mexChannelBufferSize = 2
)
// A messageExchange tracks this Connections's side of a message exchange with a
// peer. Each message exchange has a channel that can be used to receive
// frames from the peer, and a Context that can controls when the exchange has
// timed out or been cancelled.
type messageExchange struct {
recvCh chan *Frame
ctx context.Context
msgID uint32
msgType messageType
mexset *messageExchangeSet
framePool FramePool
}
// forwardPeerFrame forwards a frame from a peer to the message exchange, where
// it can be pulled by whatever application thread is handling the exchange
func (mex *messageExchange) forwardPeerFrame(frame *Frame) error {
select {
case mex.recvCh <- frame:
return nil
case <-mex.ctx.Done():
// Note: One slow reader processing a large request could stall the connection.
// If we see this, we need to increase the recvCh buffer size.
return mex.ctx.Err()
}
}
// recvPeerFrame waits for a new frame from the peer, or until the context
// expires or is cancelled
func (mex *messageExchange) recvPeerFrame() (*Frame, error) {
select {
case frame := <-mex.recvCh:
return frame, nil
case <-mex.ctx.Done():
return nil, mex.ctx.Err()
}
}
// recvPeerFrameOfType waits for a new frame of a given type from the peer, failing
// if the next frame received is not of that type
func (mex *messageExchange) recvPeerFrameOfType(msgType messageType) (*Frame, error) {
frame, err := mex.recvPeerFrame()
if err != nil {
return nil, err
}
switch frame.Header.messageType {
case msgType:
return frame, nil
case messageTypeError:
errMsg := errorMessage{
id: frame.Header.ID,
}
var rbuf typed.ReadBuffer
rbuf.Wrap(frame.SizedPayload())
if err := errMsg.read(&rbuf); err != nil {
return nil, err
}
return nil, errMsg.AsSystemError()
default:
// TODO(mmihic): Should be treated as a protocol error
mex.mexset.log.Warnf("Received unexpected message %d for %d",
int(frame.Header.messageType), frame.Header.ID)
return nil, errUnexpectedFrameType
}
}
// shutdown shuts down the message exchange, removing it from the message
// exchange set so that it cannot receive more messages from the peer. The
// receive channel remains open, however, in case there are concurrent
// goroutines sending to it.
func (mex *messageExchange) shutdown() {
mex.mexset.removeExchange(mex.msgID)
}
// inboundTimeout is called when an exchange times out, but a handler may still be
// running in the background. Since the handler may still write to the exchange, we
// cannot shutdown the exchange, but we should remove it from the connection's
// exchange list.
func (mex *messageExchange) inboundTimeout() {
mex.mexset.timeoutExchange(mex.msgID)
}
// A messageExchangeSet manages a set of active message exchanges. It is
// mainly used to route frames from a peer to the appropriate messageExchange,
// or to cancel or mark a messageExchange as being in error. Each Connection
// maintains two messageExchangeSets, one to manage exchanges that it has
// initiated (outbound), and another to manage exchanges that the peer has
// initiated (inbound). The message-type specific handlers are responsible for
// ensuring that their message exchanges are properly registered and removed
// from the corresponding exchange set.
type messageExchangeSet struct {
log Logger
name string
onRemoved func()
exchanges map[uint32]*messageExchange
sendChRefs sync.WaitGroup
mut sync.RWMutex
}
// newExchange creates and adds a new message exchange to this set
func (mexset *messageExchangeSet) newExchange(ctx context.Context, framePool FramePool,
msgType messageType, msgID uint32, bufferSize int) (*messageExchange, error) {
mexset.log.Debugf("Creating new %s message exchange for [%v:%d]", mexset.name, msgType, msgID)
mex := &messageExchange{
msgType: msgType,
msgID: msgID,
ctx: ctx,
recvCh: make(chan *Frame, bufferSize),
mexset: mexset,
framePool: framePool,
}
mexset.mut.Lock()
defer mexset.mut.Unlock()
if existingMex := mexset.exchanges[mex.msgID]; existingMex != nil {
if existingMex == mex {
mexset.log.Warnf("%s mex for %s, %d registered multiple times",
mexset.name, mex.msgType, mex.msgID)
} else {
mexset.log.Warnf("msg id %d used for both active mex %s and new mex %s",
mex.msgID, existingMex.msgType, mex.msgType)
}
return nil, errDuplicateMex
}
mexset.exchanges[mex.msgID] = mex
mexset.sendChRefs.Add(1)
// TODO(mmihic): Put into a deadline ordered heap so we can garbage collected expired exchanges
return mex, nil
}
// removeExchange removes a message exchange from the set, if it exists.
// It decrements the sendChRefs wait group, signalling that this exchange no longer has
// any active goroutines that will try to send to sendCh.
func (mexset *messageExchangeSet) removeExchange(msgID uint32) {
mexset.log.Debugf("Removing %s message exchange %d", mexset.name, msgID)
mexset.mut.Lock()
delete(mexset.exchanges, msgID)
mexset.mut.Unlock()
mexset.sendChRefs.Done()
mexset.onRemoved()
}
// timeoutExchange is similar to removeExchange, however it does not decrement
// the sendChRefs wait group.
func (mexset *messageExchangeSet) timeoutExchange(msgID uint32) {
mexset.log.Debugf("Removing %s message exchange %d due to timeout", mexset.name, msgID)
mexset.mut.Lock()
delete(mexset.exchanges, msgID)
mexset.mut.Unlock()
mexset.onRemoved()
}
// waitForSendCh waits for all goroutines with references to sendCh to complete.
func (mexset *messageExchangeSet) waitForSendCh() {
mexset.sendChRefs.Wait()
}
func (mexset *messageExchangeSet) count() int {
mexset.mut.RLock()
count := len(mexset.exchanges)
mexset.mut.RUnlock()
return count
}
// forwardPeerFrame forwards a frame from the peer to the appropriate message
// exchange
func (mexset *messageExchangeSet) forwardPeerFrame(frame *Frame) error {
mexset.log.Debugf("forwarding %s %s", mexset.name, frame.Header)
mexset.mut.RLock()
mex := mexset.exchanges[frame.Header.ID]
mexset.mut.RUnlock()
if mex == nil {
// This is ok since the exchange might have expired or been cancelled
mexset.log.Warnf("received frame %s for message exchange that no longer exists", frame.Header)
return nil
}
if err := mex.forwardPeerFrame(frame); err != nil {
mexset.log.Warnf("Unable to forward frame ID %v type %v length %v to peer: %v",
frame.Header.ID, frame.Header.messageType, frame.Header.FrameSize(), err)
return err
}
return nil
}