-
Notifications
You must be signed in to change notification settings - Fork 1
/
notification2.go
442 lines (370 loc) · 10.9 KB
/
notification2.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
package notification2
import (
"bufio"
"bytes"
"crypto/tls"
"math"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/reubenmiller/go-c8y/pkg/logger"
"github.com/tidwall/gjson"
tomb "gopkg.in/tomb.v2"
)
var Logger logger.Logger
func init() {
Logger = logger.NewLogger("notifications2")
}
const (
// MaximumRetryInterval is the maximum interval (in seconds) between reconnection attempts
MaximumRetryInterval int64 = 300
// MinimumRetryInterval is the minimum interval (in seconds) between reconnection attempts
MinimumRetryInterval int64 = 5
// RetryBackoffFactor is the backoff factor applied to the retry interval for every unsuccessful reconnection attempt.
// i.e. the next retry interval is calculated as followins
// interval = MinimumRetryInterval
// interval = Min(MaximumRetryInterval, interval * RetryBackoffFactor)
RetryBackoffFactor float64 = 2
)
func SetLogger(log logger.Logger) {
if log == nil {
Logger = logger.NewDummyLogger("notification2")
} else {
Logger = log
}
}
type ConnectionOptions struct {
// Send pings to client with this interval. Must be less than pongWait.
PingInterval time.Duration
PongWait time.Duration
WriteWait time.Duration
Insecure bool
}
func (o *ConnectionOptions) GetWriteDuration() time.Duration {
if o.WriteWait == 0 {
return 60 * time.Second
}
return o.WriteWait
}
func (o *ConnectionOptions) GetPongDuration() time.Duration {
if o.PongWait == 0 {
return 120 * time.Second
}
return o.PongWait
}
func (o *ConnectionOptions) GetPingDuration() time.Duration {
if o.PingInterval == 0 {
return 60 * time.Second
}
return o.PingInterval
}
// Notification2Client is a client used for the notification2 interface
type Notification2Client struct {
mtx sync.RWMutex
host string
url *url.URL
tomb *tomb.Tomb
messages chan *Message
connected bool
dialer *websocket.Dialer
ws *websocket.Conn
Subscription Subscription
ConnectionOptions ConnectionOptions
hub *Hub
send chan []byte
}
type Subscription struct {
Consumer string `json:"consumer,omitempty"`
Token string `json:"token,omitempty"`
TokenRenewal func(string) (string, error)
}
type ClientSubscription struct {
Pattern string
Action string
Out chan<- Message
Disabled bool
}
// Message is the type delivered to subscribers.
type Message struct {
Identifier []byte `json:"identifier"`
Description []byte `json:"description"`
Action []byte `json:"action"`
Payload []byte `json:"data,omitempty"`
}
type ActionType string
var ActionTypeCreate ActionType = "CREATE"
var ActionTypeUpdate ActionType = "UPDATE"
var ActionTypeDelete ActionType = "DELETE"
func (m *Message) JSON() gjson.Result {
return gjson.ParseBytes(m.Payload)
}
func getEndpoint(host string, subscription Subscription) *url.URL {
fullHost := "wss://" + host
if index := strings.Index(host, "://"); index > -1 {
fullHost = "wss" + host[index:]
}
tempUrl, err := url.Parse(fullHost)
if err != nil {
Logger.Fatalf("Invalid url. %s", err)
}
c8yhost := tempUrl.ResolveReference(&url.URL{Path: "notification2/consumer/"})
c8yhost.RawQuery = "token=" + subscription.Token
if subscription.Consumer != "" {
c8yhost.RawQuery += "&consumer=" + subscription.Consumer
}
return c8yhost
}
// NewNotification2Client initializes a new notification2 client used to subscribe to realtime notifications from Cumulocity
func NewNotification2Client(host string, wsDialer *websocket.Dialer, subscription Subscription, options ConnectionOptions) *Notification2Client {
if wsDialer == nil {
// Default client ignores self signed certificates (to enable compatibility to the edge which uses self signed certs)
wsDialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: options.Insecure,
},
}
}
client := &Notification2Client{
host: host,
url: getEndpoint(host, subscription),
dialer: wsDialer,
messages: make(chan *Message, 100),
Subscription: subscription,
ConnectionOptions: options,
send: make(chan []byte),
hub: NewHub(),
}
go client.hub.Run()
go client.writeHandler()
return client
}
// Connect performs a handshake with the server and will repeatedly initiate a
// websocket connection until `Close` is called on the client.
func (c *Notification2Client) Connect() error {
if !c.IsConnected() {
err := c.connect()
if err != nil {
return err
}
}
return nil
}
func (c *Notification2Client) Endpoint() string {
// TODO: Support hiding of sensitive information (same as the client)
return c.url.String()
}
func (c *Notification2Client) URL() string {
return getEndpoint(c.url.Host, c.Subscription).String()
}
// IsConnected returns true if the websocket is connected
func (c *Notification2Client) IsConnected() bool {
c.mtx.RLock()
isConnected := c.connected
c.mtx.RUnlock()
return isConnected
}
// Close the connection
func (c *Notification2Client) Close() error {
if err := c.disconnect(); err != nil {
Logger.Warnf("Failed to disconnect. %s", err)
}
c.mtx.Lock()
defer c.mtx.Unlock()
if c.tomb != nil {
Logger.Debugf("Stopping worker")
c.tomb.Killf("Close")
c.tomb = nil
}
return nil
}
func (c *Notification2Client) disconnect() error {
// Change to disconnected state, as the server will not send a reply upon receiving the /meta/disconnect command
c.mtx.Lock()
defer c.mtx.Unlock()
c.connected = false
// TODO: Add option to unsubscribe on disconnect (e.g. no offline messages required?)
// Note: If you unsubscribe, then notifications will be ignored when the client is offline
// if c.ws != nil {
// if err := c.Unsubscribe(); err != nil {
// return err
// }
// }
return nil
}
func (c *Notification2Client) createWebsocket() (*websocket.Conn, error) {
if c.Subscription.TokenRenewal != nil {
token, err := c.Subscription.TokenRenewal(c.Subscription.Token)
if err != nil {
return nil, err
}
c.Subscription.Token = token
}
Logger.Debugf("Establishing connection to %s", c.Endpoint())
ws, _, err := c.dialer.Dial(c.URL(), nil)
if err != nil {
Logger.Warnf("Failed to establish connection. %s", err)
return ws, err
}
Logger.Debug("Established websocket connection")
return ws, nil
}
func (c *Notification2Client) reconnect() error {
c.Close()
connected := false
interval := MinimumRetryInterval
for !connected {
Logger.Warnf("Retrying in %ds", interval)
<-time.After(time.Duration(interval) * time.Second)
err := c.connect()
if err != nil {
Logger.Warnf("Failed to connect. %s", err)
interval = int64(math.Min(float64(MaximumRetryInterval), RetryBackoffFactor*float64(interval)))
continue
}
connected = true
}
Logger.Warn("Reestablished connection")
return nil
}
// StartWebsocket opens a websocket to cumulocity
func (c *Notification2Client) connect() error {
if c.dialer == nil {
panic("Missing dialer for realtime client")
}
ws, err := c.createWebsocket()
if err != nil {
return err
}
c.mtx.Lock()
defer c.mtx.Unlock()
c.ws = ws
if c.tomb == nil {
c.tomb = &tomb.Tomb{}
c.tomb.Go(c.worker)
}
c.connected = true
return nil
}
func parseMessage(raw []byte) *Message {
inHeader := true
message := &Message{}
scanner := bufio.NewScanner(bytes.NewReader(raw))
i := 0
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
inHeader = false
}
if inHeader {
if i == 0 {
message.Identifier = line
} else if i == 1 {
message.Description = line
} else if i == 2 {
message.Action = line
}
// Ignore unknown header indexes
} else {
message.Payload = line
}
i++
}
return message
}
func (c *Notification2Client) writeHandler() {
ticker := time.NewTicker(c.ConnectionOptions.GetPingDuration())
defer func() {
ticker.Stop()
close(c.send)
}()
for {
select {
case message, ok := <-c.send:
if !ok {
// The send channel has been closed
Logger.Info("Channel has been closed")
return
}
if c.ws != nil {
c.ws.SetWriteDeadline(time.Now().Add(c.ConnectionOptions.GetWriteDuration()))
if err := c.ws.WriteMessage(websocket.TextMessage, message); err != nil {
Logger.Warnf("Failed to send message. %s", err)
}
}
case <-ticker.C:
// Regularly check if the Websocket is alive by sending a PingMessage to the server
if c.ws != nil {
// A websocket ping should initiate a websocket pong response from the server
// If the pong is not received in the minimum time, then the connection will be reset
c.ws.SetWriteDeadline(time.Now().Add(c.ConnectionOptions.GetWriteDuration()))
if err := c.ws.WriteMessage(websocket.PingMessage, nil); err != nil {
Logger.Warnf("Failed to send ping message to server. %s", err)
// go c.reconnect()
return
}
Logger.Debug("Sent ping successfully")
}
}
}
}
func (c *Notification2Client) Register(pattern string, out chan<- Message) {
Logger.Debugf("Subscribing to %s", pattern)
c.hub.register <- &ClientSubscription{
Pattern: pattern,
Out: out,
Disabled: false,
}
}
func (c *Notification2Client) SendMessageAck(messageIdentifier []byte) error {
Logger.Debugf("Sending message ack: %s", messageIdentifier)
c.ws.SetWriteDeadline(time.Now().Add(c.ConnectionOptions.GetWriteDuration()))
return c.ws.WriteMessage(websocket.TextMessage, messageIdentifier)
}
func (c *Notification2Client) worker() error {
done := make(chan struct{})
c.ws.SetReadDeadline(time.Now().Add(c.ConnectionOptions.GetPongDuration()))
c.ws.SetPongHandler(func(string) error {
Logger.Debug("Received pong message")
c.ws.SetReadDeadline(time.Now().Add(c.ConnectionOptions.GetPongDuration()))
return nil
})
go func() {
defer close(done)
for {
messageType, rawMessage, err := c.ws.ReadMessage()
Logger.Debugf("Received message: type=%d, message=%s, err=%s", messageType, rawMessage, err)
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
Logger.Infof("error %v", err)
}
Logger.Warnf("err: %s", err)
go c.reconnect()
break
}
if messageType == websocket.TextMessage {
message := parseMessage(rawMessage)
c.hub.broadcast <- *message
Logger.Debugf("message id: %s", message.Identifier)
Logger.Debugf("message description: %s", message.Description)
Logger.Debugf("message action: %s", message.Action)
Logger.Debugf("message payload: %s", message.Payload)
}
}
}()
defer c.ws.Close()
<-c.tomb.Dying()
Logger.Info("Worker is shutting down")
return nil
}
// Unsubscribe unsubscribe to a given pattern
func (c *Notification2Client) Unsubscribe() error {
Logger.Info("unsubscribing")
c.ws.SetWriteDeadline(time.Now().Add(c.ConnectionOptions.GetWriteDuration()))
return c.ws.WriteMessage(websocket.TextMessage, []byte("unsubscribe_subscriber"))
}