-
Notifications
You must be signed in to change notification settings - Fork 8
/
bayeux_client.go
343 lines (301 loc) · 9.02 KB
/
bayeux_client.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package gobayeux
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/cookiejar"
"net/url"
"sync"
"time"
"github.com/sirupsen/logrus"
"golang.org/x/net/publicsuffix"
)
// BayeuxClient is a way of acting as a client with a given Bayeux server
type BayeuxClient struct {
stateMachine *ConnectionStateMachine
client *http.Client
serverAddress *url.URL
state *clientState
exts []MessageExtender
logger logrus.FieldLogger
}
// NewBayeuxClient initializes a BayeuxClient for the user
func NewBayeuxClient(client *http.Client, transport http.RoundTripper, serverAddress string, logger logrus.FieldLogger) (*BayeuxClient, error) {
if client == nil {
client = http.DefaultClient
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
client.Jar = jar
}
if transport == nil {
transport = http.DefaultTransport
}
client.Transport = transport
parsedAddress, err := url.Parse(serverAddress)
if err != nil {
return nil, err
}
if logger == nil {
logger = logrus.New()
}
return &BayeuxClient{
stateMachine: NewConnectionStateMachine(),
client: client,
serverAddress: parsedAddress,
state: &clientState{},
logger: logger,
}, nil
}
// Handshake sends the handshake request to the Bayeux Server
func (b *BayeuxClient) Handshake(ctx context.Context) ([]Message, error) {
logger := b.logger.WithField("at", "handshake")
start := time.Now()
logger.Debug("starting")
if err := b.stateMachine.ProcessEvent(handshakeSent); err != nil {
logger.WithError(err).Debug("invalid action for current state")
return nil, HandshakeFailedError{err}
}
builder := NewHandshakeRequestBuilder()
if err := builder.AddVersion("1.0"); err != nil {
return nil, HandshakeFailedError{err}
}
if err := builder.AddSupportedConnectionType("long-polling"); err != nil {
return nil, HandshakeFailedError{err}
}
ms, err := builder.Build()
if err != nil {
return nil, HandshakeFailedError{err}
}
resp, err := b.request(ctx, ms)
if err != nil {
logger.WithError(err).Debug("error during request")
return nil, HandshakeFailedError{err}
}
response, err := b.parseResponse(resp)
if err != nil {
logger.WithError(err).Debug("error parsing response")
return response, HandshakeFailedError{err}
}
if len(response) > 1 {
return response, HandshakeFailedError{ErrTooManyMessages}
}
var message Message
for _, m := range response {
if m.Channel == MetaHandshake {
message = m
}
}
if message.Channel == emptyChannel {
return response, HandshakeFailedError{ErrBadChannel}
}
if !message.Successful {
return response, newHandshakeError(message.Error)
}
b.state.SetClientID(message.ClientID)
_ = b.stateMachine.ProcessEvent(successfullyConnected)
logger.WithField("duration", time.Since(start)).Debug("finishing")
return response, nil
}
// Connect sends the connect request to the Bayeux Server. The specification
// says that clients MUST maintain only one outstanding connect request. See
// https://docs.cometd.org/current/reference/#_bayeux_meta_connect
func (b *BayeuxClient) Connect(ctx context.Context) ([]Message, error) {
logger := b.logger.WithField("at", "connect")
start := time.Now()
logger.Debug("starting")
clientID := b.state.GetClientID()
if !b.stateMachine.IsConnected() || clientID == "" {
return nil, ErrClientNotConnected
}
builder := NewConnectRequestBuilder()
builder.AddClientID(clientID)
_ = builder.AddConnectionType(ConnectionTypeLongPolling)
ms, err := builder.Build()
if err != nil {
return nil, ConnectionFailedError{err}
}
resp, err := b.request(ctx, ms)
if err != nil {
logger.WithError(err).Debug("error during request")
return nil, ConnectionFailedError{err}
}
response, err := b.parseResponse(resp)
if err != nil {
logger.WithError(err).Debug("error parsing response")
return response, ConnectionFailedError{err}
}
for _, m := range response {
if m.Channel == MetaConnect && !m.Successful {
return response, ConnectionFailedError{ErrFailedToConnect}
}
}
logger.WithField("duration", time.Since(start)).Debug("finishing")
return response, nil
}
// Subscribe issues a MetaSubscribe request to the server to subscribe to the
// channels in the subscriptions slice
func (b *BayeuxClient) Subscribe(ctx context.Context, subscriptions []Channel) ([]Message, error) {
logger := b.logger.WithField("at", "subscribe")
start := time.Now()
logger.Debug("starting")
clientID := b.state.GetClientID()
if !b.stateMachine.IsConnected() || clientID == "" {
logger.Debug("cannot subscribe because client is not connected")
return nil, SubscriptionFailedError{subscriptions, ErrClientNotConnected}
}
builder := NewSubscribeRequestBuilder()
builder.AddClientID(clientID)
for _, s := range subscriptions {
if err := builder.AddSubscription(s); err != nil {
return nil, SubscriptionFailedError{subscriptions, err}
}
}
ms, err := builder.Build()
if err != nil {
return nil, SubscriptionFailedError{subscriptions, err}
}
resp, err := b.request(ctx, ms)
if err != nil {
return nil, SubscriptionFailedError{subscriptions, err}
}
response, err := b.parseResponse(resp)
if err != nil {
return nil, SubscriptionFailedError{subscriptions, err}
}
for _, m := range response {
if m.Channel == MetaSubscribe && !m.Successful {
return nil, SubscriptionFailedError{
Channels: subscriptions,
Err: newSubscribeError(m.Error),
}
}
}
logger.WithField("duration", time.Since(start)).Debug("finishing")
return response, nil
}
// Unsubscribe issues a MetaUnsubscribe request to the server to subscribe to the
// channels in the subscriptions slice
func (b *BayeuxClient) Unsubscribe(ctx context.Context, subscriptions []Channel) ([]Message, error) {
clientID := b.state.GetClientID()
if !b.stateMachine.IsConnected() || clientID == "" {
return nil, UnsubscribeFailedError{subscriptions, ErrClientNotConnected}
}
builder := NewUnsubscribeRequestBuilder()
builder.AddClientID(clientID)
for _, s := range subscriptions {
if err := builder.AddSubscription(s); err != nil {
return nil, UnsubscribeFailedError{subscriptions, err}
}
}
ms, err := builder.Build()
if err != nil {
return nil, UnsubscribeFailedError{subscriptions, err}
}
resp, err := b.request(ctx, ms)
if err != nil {
return nil, UnsubscribeFailedError{subscriptions, err}
}
response, err := b.parseResponse(resp)
if err != nil {
return response, UnsubscribeFailedError{subscriptions, err}
}
for _, m := range response {
if m.Channel == MetaUnsubscribe && !m.Successful {
return response, UnsubscribeFailedError{
Channels: subscriptions,
Err: newUnsubscribeError(m.Error),
}
}
}
return response, nil
}
// Disconnect sends a /meta/disconnect request to the Bayeux server to
// terminate the session
func (b *BayeuxClient) Disconnect(ctx context.Context) ([]Message, error) {
clientID := b.state.GetClientID()
if !b.stateMachine.IsConnected() || clientID == "" {
return nil, DisconnectFailedError{ErrClientNotConnected}
}
builder := NewDisconnectRequestBuilder()
builder.AddClientID(clientID)
ms, err := builder.Build()
if err != nil {
return nil, DisconnectFailedError{err}
}
resp, err := b.request(ctx, ms)
if err != nil {
return nil, DisconnectFailedError{err}
}
response, err := b.parseResponse(resp)
if err != nil {
return response, DisconnectFailedError{err}
}
for _, m := range response {
if m.Channel == MetaDisconnect && !m.Successful {
return response, DisconnectFailedError{nil}
}
}
return response, nil
}
// UseExtension adds the provided MessageExtender to the list of known
// extensions
func (b *BayeuxClient) UseExtension(ext MessageExtender) error {
for _, registered := range b.exts {
if ext == registered {
return AlreadyRegisteredError{ext}
}
}
b.exts = append(b.exts, ext)
return nil
}
func (b *BayeuxClient) request(ctx context.Context, ms []Message) (*http.Response, error) {
for _, ext := range b.exts {
for _, m := range ms {
ext.Outgoing(&m)
}
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(ms); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST", b.serverAddress.String(), &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
return b.client.Do(req)
}
func (b *BayeuxClient) parseResponse(resp *http.Response) ([]Message, error) {
messages := make([]Message, 0)
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, BadResponseError{resp.StatusCode, resp.Status}
}
if err := json.NewDecoder(resp.Body).Decode(&messages); err != nil {
return nil, err
}
for _, ext := range b.exts {
for _, m := range messages {
ext.Incoming(&m)
}
}
return messages, nil
}
type clientState struct {
clientID string
lock sync.RWMutex
}
func (cs *clientState) GetClientID() string {
cs.lock.RLock()
defer cs.lock.RUnlock()
return cs.clientID
}
func (cs *clientState) SetClientID(clientID string) {
cs.lock.Lock()
defer cs.lock.Unlock()
cs.clientID = clientID
}