forked from Comcast/pulsar-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframedispatcher.go
262 lines (224 loc) · 7.35 KB
/
framedispatcher.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2018 Comcast Cable Communications Management, LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pulsar
import (
"errors"
"fmt"
"sync"
"github.com/Comcast/pulsar-client-go/frame"
)
// newFrameDispatcher returns an instantiated frameDispatcher.
func newFrameDispatcher() *frameDispatcher {
return &frameDispatcher{
prodSeqIDs: make(map[prodSeqKey]asyncResp),
reqIDs: make(map[uint64]asyncResp),
}
}
// frameDispatcher is responsible for handling the request/response
// state of outstanding requests. It allows for users of this
// type to present a synchronous interface to an asynchronous
// process.
type frameDispatcher struct {
// Connected and Pong responses have no requestID,
// therefore a single channel is used as their
// respective frameDispatcher. If the channel is
// nil, there's no outstanding request.
globalMu sync.Mutex // protects following
global *asyncResp
// All responses that are correlated by their
// requestID
reqIDMu sync.Mutex // protects following
reqIDs map[uint64]asyncResp
// All responses that are correlated by their
// (producerID, sequenceID) tuple
prodSeqIDsMu sync.Mutex // protects following
prodSeqIDs map[prodSeqKey]asyncResp
}
// asyncResp manages the state between a request
// and response. Requestors wait on the `resp` channel
// for the corresponding response frame to their request.
// If they are no longer interested in the response (timeout),
// then the `done` channel is closed, signaling to the response
// side that the response is not expected/needed.
type asyncResp struct {
resp chan<- frame.Frame
done <-chan struct{}
}
// prodSeqKey is a composite lookup key for the dispatchers
// that use producerID and sequenceID to correlate responses,
// which are the SendReceipt and SendError responses.
type prodSeqKey struct {
producerID uint64
sequenceID uint64
}
// registerGlobal is used to wait for responses that have no identifying
// id (Pong, Connected responses). Only one outstanding global request
// is allowed at a time. Callers should always call cancel, specifically
// when they're not interested in the response.
func (f *frameDispatcher) registerGlobal() (response <-chan frame.Frame, cancel func(), err error) {
var mu sync.Mutex
done := make(chan struct{})
cancel = func() {
mu.Lock()
defer mu.Unlock()
if done == nil {
return
}
f.globalMu.Lock()
f.global = nil
f.globalMu.Unlock()
close(done)
done = nil
}
resp := make(chan frame.Frame)
f.globalMu.Lock()
if f.global != nil {
f.globalMu.Unlock()
return nil, nil, errors.New("outstanding global request already in progress")
}
f.global = &asyncResp{
resp: resp,
done: done,
}
f.globalMu.Unlock()
return resp, cancel, nil
}
// notifyGlobal should be called with response frames that have
// no identifying id (Pong, Connected).
func (f *frameDispatcher) notifyGlobal(frame frame.Frame) error {
f.globalMu.Lock()
a := f.global
// ensure additional calls to notify
// fail with UnexpectedMsg (unless register is called again)
f.global = nil
f.globalMu.Unlock()
if a == nil {
return newErrUnexpectedMsg(frame.BaseCmd.GetType())
}
select {
case a.resp <- frame:
// sent response back to sender
return nil
case <-a.done:
return newErrUnexpectedMsg(frame.BaseCmd.GetType())
}
}
// registerProdSeqID is used to wait for responses that have (producerID, sequenceID)
// id tuples to correlate them to their request. Callers should always call cancel,
// specifically when they're not interested in the response. It is an error
// to have multiple outstanding requests with the same id tuple.
func (f *frameDispatcher) registerProdSeqIDs(producerID, sequenceID uint64) (response <-chan frame.Frame, cancel func(), err error) {
key := prodSeqKey{producerID, sequenceID}
var mu sync.Mutex
done := make(chan struct{})
cancel = func() {
mu.Lock()
defer mu.Unlock()
if done == nil {
return
}
f.prodSeqIDsMu.Lock()
delete(f.prodSeqIDs, key)
f.prodSeqIDsMu.Unlock()
close(done)
done = nil
}
resp := make(chan frame.Frame)
f.prodSeqIDsMu.Lock()
if _, ok := f.prodSeqIDs[key]; ok {
f.prodSeqIDsMu.Unlock()
return nil, nil, fmt.Errorf("already exists an outstanding response for producerID %d, sequenceID %d", producerID, sequenceID)
}
f.prodSeqIDs[key] = asyncResp{
resp: resp,
done: done,
}
f.prodSeqIDsMu.Unlock()
return resp, cancel, nil
}
// notifyProdSeqIDs should be called with response frames that have
// (producerID, sequenceID) id tuples to correlate them to their requests.
func (f *frameDispatcher) notifyProdSeqIDs(producerID, sequenceID uint64, frame frame.Frame) error {
key := prodSeqKey{producerID, sequenceID}
f.prodSeqIDsMu.Lock()
// fetch response channel from cubbyhole
a, ok := f.prodSeqIDs[key]
// ensure additional calls to notify with same key will
// fail with UnexpectedMsg (unless registerProdSeqIDs with same key is called)
delete(f.prodSeqIDs, key)
f.prodSeqIDsMu.Unlock()
if !ok {
return newErrUnexpectedMsg(frame.BaseCmd.GetType(), producerID, sequenceID)
}
select {
case a.resp <- frame:
// response was correctly pushed into channel
return nil
case <-a.done:
return newErrUnexpectedMsg(frame.BaseCmd.GetType(), producerID, sequenceID)
}
}
// registerReqID is used to wait for responses that have a requestID
// id to correlate them to their request. Callers should always call cancel,
// specifically when they're not interested in the response. It is an error
// to have multiple outstanding requests with the id.
func (f *frameDispatcher) registerReqID(requestID uint64) (response <-chan frame.Frame, cancel func(), err error) {
var mu sync.Mutex
done := make(chan struct{})
cancel = func() {
mu.Lock()
defer mu.Unlock()
if done == nil {
return
}
f.reqIDMu.Lock()
delete(f.reqIDs, requestID)
f.reqIDMu.Unlock()
close(done)
done = nil
}
resp := make(chan frame.Frame)
f.reqIDMu.Lock()
if _, ok := f.reqIDs[requestID]; ok {
f.reqIDMu.Unlock()
return nil, nil, fmt.Errorf("already exists an outstanding response for requestID %d", requestID)
}
f.reqIDs[requestID] = asyncResp{
resp: resp,
done: done,
}
f.reqIDMu.Unlock()
return resp, cancel, nil
}
// notifyReqID should be called with response frames that have
// a requestID to correlate them to their requests.
func (f *frameDispatcher) notifyReqID(requestID uint64, frame frame.Frame) error {
f.reqIDMu.Lock()
// fetch response channel from cubbyhole
a, ok := f.reqIDs[requestID]
// ensure additional calls to notifyReqID with same key will
// fail with UnexpectedMsg (unless addReqID with same key is called)
delete(f.reqIDs, requestID)
f.reqIDMu.Unlock()
if !ok {
return newErrUnexpectedMsg(frame.BaseCmd.GetType(), requestID)
}
// send received message to response channel
select {
case a.resp <- frame:
// response was correctly pushed into channel
return nil
case <-a.done:
return newErrUnexpectedMsg(frame.BaseCmd.GetType(), requestID)
}
}