-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
661 lines (584 loc) · 15.7 KB
/
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package realtime_pubsub
import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"net"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
writeWait = 10 * time.Second // Time allowed to write a message to the connection
pongWait = 15 * time.Second // Time allowed to read the next Pong message from the server
pingPeriod = (pongWait * 9) / 10 // Send pings to the server with this period
maxMessageSize = 6 * 1024 // Maximum message size allowed from the server
)
// Client encapsulates WebSocket connection, subscription, and message handling.
type Client struct {
eventEmitter *EventEmitter
ws *websocket.Conn
config Config
logger *logrus.Logger
subscribedTopics map[string]struct{}
isConnecting bool
mu sync.Mutex
writeChan chan []byte
closeChan chan struct{}
closeOnce *sync.Once
ctx context.Context
cancel context.CancelFunc
done chan struct{}
}
// NewClient initializes a new Client instance.
func NewClient(config Config) *Client {
logger := config.Logger
if logger == nil {
logger = logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.DebugLevel)
}
ctx, cancel := context.WithCancel(context.Background())
client := &Client{
eventEmitter: NewEventEmitter(),
config: config,
logger: logger,
subscribedTopics: make(map[string]struct{}),
writeChan: make(chan []byte, 256),
closeChan: make(chan struct{}),
closeOnce: new(sync.Once),
ctx: ctx,
cancel: cancel,
done: make(chan struct{}),
}
// Register event listeners
client.eventEmitter.On("priv/acks.ack", client.onAck)
client.eventEmitter.On("*.response", client.onResponse)
client.eventEmitter.On("main.welcome", client.onWelcome)
return client
}
// closeWebSocket safely closes the WebSocket connection once.
func (c *Client) closeWebSocket() {
c.mu.Lock()
if c.ws != nil {
_ = c.ws.Close()
c.ws = nil
}
c.mu.Unlock()
}
// Connect establishes a connection to the WebSocket server.
func (c *Client) Connect() {
c.mu.Lock()
c.isConnecting = false
c.mu.Unlock()
go c.connectLoop()
}
func (c *Client) Disconnect() error {
c.closeOnce.Do(func() {
c.logger.Infof("Disconnecting client")
close(c.done) // Signal all goroutines to exit
close(c.writeChan)
c.closeWebSocket()
})
return nil
}
// Publish publishes a message to a specified topic.
func (c *Client) Publish(topic string, payload interface{}, opts ...PublishOption) (*WaitFor, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.ws == nil {
c.logger.Errorf("Attempted to publish without an active WebSocket connection.")
return nil, fmt.Errorf("WebSocket connection is not established")
}
if opts == nil {
opts = make([]PublishOption, 0)
}
// Set default options
options := PublishOptions{
ID: getRandomID(rand.Reader),
MessageType: "broadcast",
Compress: false,
}
// Apply provided options
for _, opt := range opts {
opt(&options)
}
message := map[string]interface{}{
"type": "publish",
"data": map[string]interface{}{
"topic": topic,
"messageType": options.MessageType,
"compress": options.Compress,
"payload": payload,
"id": options.ID,
},
}
messageBytes, err := json.Marshal(message)
if err != nil {
c.logger.Errorf("Failed to marshal message: %v", err)
return nil, err
}
c.logger.Debugf("Publishing message to topic %s: %v", topic, payload)
err = c.sendMessage(messageBytes)
if err != nil {
c.logger.Errorf("Failed to send message: %v", err)
return nil, err
}
return &WaitFor{
client: c,
id: options.ID,
}, nil
}
// Send sends a message directly to the server.
func (c *Client) Send(payload interface{}, opts ...SendOption) (*WaitFor, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.ws == nil {
c.logger.Errorf("Attempted to send without an active WebSocket connection.")
return nil, fmt.Errorf("WebSocket connection is not established")
}
// Set default options
options := SendOptions{
ID: getRandomID(rand.Reader),
MessageType: "", // Can be empty
Compress: false, // Default compress is false
}
// Apply provided options
for _, opt := range opts {
opt(&options)
}
data := map[string]interface{}{
"messageType": options.MessageType,
"compress": options.Compress,
"payload": payload,
"id": options.ID,
}
// Remove messageType from data if it's empty
if options.MessageType == "" {
delete(data, "messageType")
}
message := map[string]interface{}{
"type": "message",
"data": data,
}
messageBytes, err := json.Marshal(message)
if err != nil {
c.logger.Errorf("Failed to marshal message: %v", err)
return nil, err
}
c.logger.Debugf("Outgoing message: %v", string(messageBytes))
err = c.sendMessage(messageBytes)
if err != nil {
c.logger.Errorf("Failed to send message: %v", err)
return nil, err
}
return &WaitFor{
client: c,
id: options.ID,
}, nil
}
// SubscribeRemoteTopic subscribes to a remote topic to receive messages.
func (c *Client) SubscribeRemoteTopic(topic string) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.ws == nil {
c.logger.Errorf("Attempted to subscribe to %s without an active WebSocket connection.", topic)
return fmt.Errorf("WebSocket connection is not established")
}
// Check if already subscribed
if _, exists := c.subscribedTopics[topic]; exists {
c.logger.Warnf("Already subscribed to topic: %s", topic)
return nil
}
c.subscribedTopics[topic] = struct{}{}
message := map[string]interface{}{
"type": "subscribe",
"data": map[string]interface{}{
"topic": topic,
},
}
messageBytes, err := json.Marshal(message)
if err != nil {
c.logger.Errorf("Failed to marshal subscribe message: %v", err)
return err
}
c.logger.Infof("Subscribing to topic: %s", topic)
err = c.sendMessage(messageBytes)
if err != nil {
c.logger.Errorf("Failed to send subscribe message: %v", err)
return err
}
return nil
}
// UnsubscribeRemoteTopic unsubscribes from a previously subscribed topic.
func (c *Client) UnsubscribeRemoteTopic(topic string) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.ws == nil {
c.logger.Errorf("Attempted to unsubscribe from %s without an active WebSocket connection.", topic)
return fmt.Errorf("WebSocket connection is not established")
}
if _, exists := c.subscribedTopics[topic]; !exists {
c.logger.Warnf("Not subscribed to topic: %s", topic)
return nil
}
delete(c.subscribedTopics, topic)
message := map[string]interface{}{
"type": "unsubscribe",
"data": map[string]interface{}{
"topic": topic,
},
}
messageBytes, err := json.Marshal(message)
if err != nil {
c.logger.Errorf("Failed to marshal unsubscribe message: %v", err)
return err
}
c.logger.Infof("Unsubscribing from topic: %s", topic)
err = c.sendMessage(messageBytes)
if err != nil {
c.logger.Errorf("Failed to send unsubscribe message: %v", err)
return err
}
return nil
}
// sendMessage queues a message to be sent to the WebSocket connection.
func (c *Client) sendMessage(message []byte) error {
select {
case c.writeChan <- message:
return nil
case <-time.After(5 * time.Second):
return fmt.Errorf("timeout sending message")
}
}
// onAck handles acknowledgment messages received from the server.
func (c *Client) onAck(args ...interface{}) {
if len(args) < 1 {
c.logger.Errorf("onAck called with insufficient arguments")
return
}
message, ok := args[0].(IncomingMessage)
if !ok {
c.logger.Errorf("onAck received invalid message format")
return
}
data, ok := message["data"].(map[string]interface{})
if !ok {
c.logger.Errorf("onAck message data is invalid")
return
}
ackID, ok := data["data"]
if !ok {
c.logger.Errorf("onAck data missing 'data' field")
return
}
c.logger.Debugf("Received ack: %v", data)
c.eventEmitter.Emit(fmt.Sprintf("ack.%v", ackID), data)
}
// onResponse handles response messages received from other subscribers or backend services.
func (c *Client) onResponse(args ...interface{}) {
if len(args) < 1 {
c.logger.Errorf("onResponse called with insufficient arguments")
return
}
message, ok := args[0].(IncomingMessage)
if !ok {
c.logger.Errorf("onResponse received invalid message format")
return
}
topic, _ := message["topic"].(string)
if strings.HasPrefix(topic, "priv/") {
c.logger.Debugf("Received response for topic %s: %v", topic, message["data"])
data, ok := message["data"].(map[string]interface{})
if !ok {
c.logger.Errorf("onResponse message data is invalid")
return
}
payload, ok := data["payload"].(map[string]interface{})
if !ok {
c.logger.Errorf("onResponse payload is invalid")
return
}
id := payload["id"]
c.eventEmitter.Emit(fmt.Sprintf("response.%v", id), payload)
}
}
// onWelcome handles 'welcome' messages to indicate that the session has started.
func (c *Client) onWelcome(args ...interface{}) {
if len(args) < 1 {
c.logger.Errorf("onWelcome called with insufficient arguments")
return
}
message, _ := args[0].(IncomingMessage)
data, _ := message["data"].(map[string]interface{})
connection, _ := data["connection"].(map[string]interface{})
c.logger.Infof("Session started, connection details: %v", connection)
c.eventEmitter.Emit("session.started", ConnectionInfo(connection))
}
// onMessage handles incoming WebSocket messages.
func (c *Client) onMessage(message []byte) {
var messageData map[string]interface{}
err := json.Unmarshal(message, &messageData)
if err != nil {
c.handleError(fmt.Errorf("failed to unmarshal message: %v", err))
return
}
topic, ok := messageData["topic"].(string)
if !ok {
c.handleError(fmt.Errorf("message missing 'topic' field: %v", messageData))
return
}
messageType, ok := messageData["messageType"].(string)
if !ok {
c.handleError(fmt.Errorf("message missing 'messageType' field: %v", messageData))
return
}
data := messageData["data"]
messageEvent := IncomingMessage{
"topic": topic,
"messageType": messageType,
"data": data,
"compression": false,
}
c.logger.Debugf("Incoming message: %v", string(message))
if messageType != "" {
c.eventEmitter.Emit(fmt.Sprintf("%s.%s", topic, messageType), messageEvent, c.reply(messageEvent))
}
}
func (c *Client) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.closeWebSocket()
}()
for {
select {
case message, ok := <-c.writeChan:
if !ok {
// The write channel was closed.
return
}
c.mu.Lock()
ws := c.ws
c.mu.Unlock()
if ws == nil {
return
}
_ = ws.SetWriteDeadline(time.Now().Add(writeWait))
err := ws.WriteMessage(websocket.TextMessage, message)
if err != nil {
c.handleError(err)
return
}
case <-ticker.C:
c.mu.Lock()
ws := c.ws
c.mu.Unlock()
if ws == nil {
return
}
_ = ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.PingMessage, nil); err != nil {
c.handleError(err)
return
}
case <-c.done:
ticker.Stop()
return
}
}
}
func (c *Client) readPump() {
defer func() {
c.closeWebSocket()
c.handleClose()
}()
c.ws.SetReadLimit(maxMessageSize)
_ = c.ws.SetReadDeadline(time.Now().Add(pongWait))
c.ws.SetPongHandler(func(string) error {
_ = c.ws.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
for {
select {
case <-c.done:
// Client is shutting down.
return
default:
// Continue reading messages
}
_, message, err := c.ws.ReadMessage()
if err != nil {
select {
case <-c.done:
return
default:
c.handleError(err)
return
}
}
if message != nil {
c.onMessage(message)
} else {
continue
}
}
}
func (c *Client) connectLoop() {
c.mu.Lock()
if c.isConnecting {
c.mu.Unlock()
return
}
c.isConnecting = true
c.mu.Unlock()
backoff := 1 * time.Second
maxBackoff := 60 * time.Second
for {
c.logger.Debugf("connectLoop tick...")
select {
case <-c.done:
c.logger.Debugf("Connect loop exiting due to client disconnection")
return
default:
// Continue with connection attempts
}
wsURL, err := c.config.WebSocketOptions.URLProvider()
if err != nil {
c.logger.Warnf("Error obtaining WebSocket URL: %v", err)
time.Sleep(backoff)
continue
}
u, err := url.Parse(wsURL)
if err != nil {
c.logger.Warnf("Invalid WebSocket URL: %v", err)
time.Sleep(backoff)
continue
}
// Create a net.Dialer with timeouts
netDialer := &net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}
// Create a websocket.Dialer that uses net.Dialer
dialer := websocket.Dialer{
NetDialContext: netDialer.DialContext,
}
// Attempt to connect with a context that can be canceled
ctx, cancel := context.WithCancel(context.Background())
go func() {
select {
case <-c.done:
cancel()
case <-ctx.Done():
// Do nothing
}
}()
conn, _, err := dialer.DialContext(ctx, u.String(), nil)
cancel() // Ensure the context is canceled to free resources
if err != nil {
select {
case <-c.done:
c.logger.Debugf("Dial canceled due to client disconnection")
return
default:
// Continue
}
c.handleError(err)
c.logger.Warnf("Retrying connection in %v after failure: %v", backoff, err)
time.Sleep(backoff)
if backoff < maxBackoff {
backoff *= 2
} else {
backoff = maxBackoff
}
continue
}
c.mu.Lock()
c.ws = conn
c.closeOnce = new(sync.Once) // Reset closeOnce for the new connection
c.logger.Infof("Connected to WebSocket URL: %.70s", wsURL)
c.isConnecting = false
c.mu.Unlock()
// Start writePump and readPump
go c.writePump()
go c.readPump()
return
}
}
// handleError handles WebSocket errors by logging and emitting an 'error' event.
func (c *Client) handleError(err error) {
c.logger.Errorf("WebSocket error: %v", err)
c.eventEmitter.Emit("error", err)
}
func (c *Client) handleClose() {
c.mu.Lock()
c.ws = nil
c.mu.Unlock()
select {
case <-c.done:
return
default:
// Continue
}
c.logger.Warnf("WebSocket closed unexpectedly, attempting to reconnect.")
c.eventEmitter.Emit("close")
go c.connectLoop()
}
// reply creates a reply function for the given client and message.
func (c *Client) reply(message map[string]interface{}) ReplyFunc {
return func(data interface{}, status string, opts ...ReplyOption) error {
if status == "" {
status = "ok"
}
dataMap, ok := message["data"].(map[string]interface{})
if !ok {
return fmt.Errorf("data field is missing or not a map in the message")
}
clientMap, ok := dataMap["client"].(map[string]interface{})
if !ok {
return fmt.Errorf("client field is missing or not a map in the message data")
}
connectionID, ok := clientMap["connectionId"]
if !ok {
return fmt.Errorf("connectionId is missing in the client data")
}
originalID := dataMap["id"]
// Set default options
options := ReplyOptions{
Compress: false,
}
// Apply provided options
for _, opt := range opts {
opt(&options)
}
payload := map[string]interface{}{
"data": data,
"status": status,
"id": originalID,
}
_, err := c.Publish(fmt.Sprintf("priv/%v", connectionID), payload,
WithPublishMessageType("response"),
WithPublishCompress(options.Compress),
)
return err
}
}
// On registers a listener for a specific event.
func (c *Client) On(event string, listener ListenerFunc) int {
return c.eventEmitter.On(event, listener)
}
// Off removes a listener for a specific event using the listener ID.
func (c *Client) Off(event string, id int) {
c.eventEmitter.Off(event, id)
}
// Once registers a listener for a specific event that will be called at most once.
func (c *Client) Once(event string, listener ListenerFunc) int {
return c.eventEmitter.Once(event, listener)
}