-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscription.go
More file actions
584 lines (512 loc) · 14.1 KB
/
subscription.go
File metadata and controls
584 lines (512 loc) · 14.1 KB
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
package wsstat
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
)
const (
subscriptionIDPrefix = "subscription-"
)
// Subscription captures a long-lived stream registered through Subscribe.
// Counters are updated atomically by the WSStat instance.
type Subscription struct {
ID string
cancel context.CancelFunc
done <-chan struct{}
messages *uint64
bytes *uint64
updates <-chan SubscriptionMessage
}
// subscriptionDecoder converts an incoming frame into an arbitrary representation.
type subscriptionDecoder func(messageType int, data []byte) (any, error)
// subscriptionMatcher returns true when the provided frame should be delivered to the
// subscription. The decoded value is only non-nil if a Decoder is configured.
type subscriptionMatcher func(messageType int, data []byte, decoded any) bool
// SubscriptionOptions configures how WSStat establishes and demultiplexes a subscription.
type SubscriptionOptions struct {
// ID can be provided to preassign a human-readable identifier. If empty, WSStat
// allocates an incremental identifier.
ID string
// MessageType and Payload describe the initial frame sent to initiate the subscription.
MessageType int
Payload []byte
// decoder transforms inbound frames into a convenient representation before dispatching
// to subscription consumers. If nil, frames are passed through as raw bytes.
decoder subscriptionDecoder
// matcher determines whether a given frame belongs to this subscription. When nil,
// the dispatcher falls back to internal matching heuristics (such as explicit IDs).
matcher subscriptionMatcher
// Buffer controls the per-subscription delivery queue length. Zero implies the default.
Buffer int
}
// SubscriptionStats snapshots per-subscription metrics for reporting through Result.
type SubscriptionStats struct {
FirstEvent time.Duration
LastEvent time.Duration
MessageCount uint64
ByteCount uint64
MeanInterArrival time.Duration
Error error
}
// SubscriptionMessage represents a single frame delivered to a subscription consumer.
type SubscriptionMessage struct {
MessageType int
Data []byte
Decoded any
Received time.Time
Err error
Size int
}
// subscriptionState tracks the state of a subscription.
type subscriptionState struct {
id string
ctx context.Context
cancel context.CancelFunc
done chan struct{}
mu sync.Mutex
closed bool
matcher subscriptionMatcher
decoder subscriptionDecoder
options SubscriptionOptions
buffer chan SubscriptionMessage
stats subscriptionMetrics
messages *uint64
bytes *uint64
}
// subscriptionMetrics tracks the metrics of a subscription.
type subscriptionMetrics struct {
firstEvent time.Time
lastEvent time.Time
messageCount uint64
byteCount uint64
totalInterArrival time.Duration
lastArrival time.Time
finalErr error
}
// Cancel stops the subscription and prevents further deliveries.
func (s *Subscription) Cancel() {
if s == nil || s.cancel == nil {
return
}
s.cancel()
}
// Unsubscribe is an alias for Cancel and preserves semantic clarity for callers.
func (s *Subscription) Unsubscribe() {
s.Cancel()
}
// Done returns a channel that closes once the subscription is fully torn down.
func (s *Subscription) Done() <-chan struct{} {
if s == nil {
return nil
}
return s.done
}
// Updates exposes the buffered stream of subscription messages.
func (s *Subscription) Updates() <-chan SubscriptionMessage {
if s == nil {
return nil
}
return s.updates
}
// MessageCount reports the total number of messages delivered to the subscription.
func (s *Subscription) MessageCount() uint64 {
if s == nil || s.messages == nil {
return 0
}
return atomic.LoadUint64(s.messages)
}
// ByteCount reports the aggregate payload size delivered to the subscription.
func (s *Subscription) ByteCount() uint64 {
if s == nil || s.bytes == nil {
return 0
}
return atomic.LoadUint64(s.bytes)
}
// Subscribe registers a long-lived listener using the supplied options and context.
// The returned Subscription can be used to consume streamed frames until cancellation.
func (ws *WSStat) Subscribe(ctx context.Context, opts SubscriptionOptions) (*Subscription, error) {
if ws.conn.Load() == nil {
return nil, errors.New("websocket connection is not established")
}
if opts.MessageType == 0 {
opts.MessageType = websocket.TextMessage
}
bufferSize := opts.Buffer
if bufferSize <= 0 {
bufferSize = ws.defaultSubscriptionBuffer
}
parentCtx := ws.ctx
if parentCtx == nil {
parentCtx = context.Background()
}
combinedCtx, cancel := context.WithCancel(parentCtx)
done := make(chan struct{})
state := &subscriptionState{
options: opts,
matcher: opts.matcher,
decoder: opts.decoder,
buffer: make(chan SubscriptionMessage, bufferSize),
done: done,
cancel: cancel,
ctx: combinedCtx,
}
if opts.ID != "" {
state.id = opts.ID
} else {
state.id = ws.newSubscriptionID()
}
// Ensure caller-provided context cancels the subscription if terminated early.
if ctx != nil {
go func(c context.Context, cancel func()) {
select {
case <-c.Done():
cancel()
case <-done:
}
}(ctx, cancel)
}
sub := ws.registerSubscription(state)
go ws.watchSubscription(state)
// Send initial subscription request if payload is provided.
if len(opts.Payload) > 0 {
ws.WriteMessage(opts.MessageType, opts.Payload)
}
return sub, nil
}
// newSubscriptionID generates a new subscription ID.
func (ws *WSStat) newSubscriptionID() string {
val := atomic.AddUint64(&ws.nextSubscriptionID, 1)
return fmt.Sprintf("%s%d", subscriptionIDPrefix, val)
}
// SubscribeOnce registers a subscription and waits for the first delivered message before
// canceling the subscription. The returned message is a snapshot of the first delivery.
func (ws *WSStat) SubscribeOnce(
ctx context.Context,
opts SubscriptionOptions,
) (SubscriptionMessage, error) {
sub, err := ws.Subscribe(ctx, opts)
if err != nil {
return SubscriptionMessage{}, err
}
updates := sub.Updates()
done := sub.Done()
var ctxDone <-chan struct{}
if ctx != nil {
ctxDone = ctx.Done()
}
for {
select {
case msg, ok := <-updates:
if !ok {
select {
case <-done:
default:
}
return SubscriptionMessage{},
errors.New("subscription closed before delivering a message")
}
if msg.Err != nil {
sub.Cancel()
<-done
return SubscriptionMessage{}, msg.Err
}
sub.Cancel()
<-done
return msg, nil
case <-done:
return SubscriptionMessage{},
errors.New("subscription closed before delivering a message")
case <-ctxDone:
sub.Cancel()
<-done
if ctx.Err() != nil {
return SubscriptionMessage{}, ctx.Err()
}
return SubscriptionMessage{}, context.Canceled
}
}
}
// trackSubscriptionEvent tracks the first and last events of a subscription.
func (ws *WSStat) trackSubscriptionEvent(ts time.Time) {
if ts.IsZero() {
return
}
ws.subscriptionMu.Lock()
defer ws.subscriptionMu.Unlock()
if ws.subscriptionFirstEvent.IsZero() || ts.Before(ws.subscriptionFirstEvent) {
ws.subscriptionFirstEvent = ts
}
if ts.After(ws.subscriptionLastEvent) {
ws.subscriptionLastEvent = ts
}
}
// deliverSubscriptionMessage delivers a message to a subscription.
func (ws *WSStat) deliverSubscriptionMessage(
state *subscriptionState,
message SubscriptionMessage,
) {
if state == nil {
return
}
now := message.Received
dropped := false
state.mu.Lock()
if state.closed {
state.mu.Unlock()
return
}
if message.Err == nil {
if state.stats.firstEvent.IsZero() {
state.stats.firstEvent = now
}
if !state.stats.lastArrival.IsZero() {
state.stats.totalInterArrival += now.Sub(state.stats.lastArrival)
}
state.stats.lastArrival = now
state.stats.lastEvent = now
state.stats.messageCount++
state.stats.byteCount += uint64(message.Size)
} else {
if state.stats.firstEvent.IsZero() {
state.stats.firstEvent = now
}
state.stats.lastEvent = now
}
if state.buffer != nil {
select {
case state.buffer <- message:
default:
dropped = true
}
}
state.mu.Unlock()
if state.messages != nil {
atomic.AddUint64(state.messages, 1)
}
if state.bytes != nil {
atomic.AddUint64(state.bytes, uint64(message.Size))
}
ws.trackSubscriptionEvent(now)
if dropped {
ws.log.Warn().Str("subscription", state.id).
Msg("subscription buffer full; dropping message")
}
}
// dispatchIncoming dispatches an incoming frame to the appropriate subscription.
func (ws *WSStat) dispatchIncoming(read *wsRead) bool {
states := ws.activeSubscriptions()
if len(states) == 0 {
return false
}
receivedAt := time.Now()
if read.err != nil {
for _, state := range states {
envelope := SubscriptionMessage{
MessageType: read.messageType,
Received: receivedAt,
Err: read.err,
}
ws.deliverSubscriptionMessage(state, envelope)
ws.finalizeSubscription(state, read.err)
}
return true
}
deliverAll := len(states) == 1
claimed := false
for _, state := range states {
if state == nil {
continue
}
if err := state.ctx.Err(); err != nil {
ws.finalizeSubscription(state, err)
continue
}
decoded := any(nil)
var decodeErr error
if state.decoder != nil {
decoded, decodeErr = state.decoder(read.messageType, read.data)
}
match := false
if state.matcher != nil {
match = state.matcher(read.messageType, read.data, decoded)
} else if deliverAll {
match = true
}
if !match && decodeErr == nil {
continue
}
payload := append([]byte(nil), read.data...)
envelope := SubscriptionMessage{
MessageType: read.messageType,
Data: payload,
Decoded: decoded,
Received: receivedAt,
Err: decodeErr,
Size: len(payload),
}
ws.deliverSubscriptionMessage(state, envelope)
claimed = true
}
return claimed
}
// activeSubscriptions returns a snapshot of the active subscriptions.
func (ws *WSStat) activeSubscriptions() []*subscriptionState {
ws.subscriptionMu.RLock()
defer ws.subscriptionMu.RUnlock()
if len(ws.subscriptions) == 0 {
return nil
}
states := make([]*subscriptionState, 0, len(ws.subscriptions))
for _, state := range ws.subscriptions {
states = append(states, state)
}
return states
}
// watchSubscription watches a subscription for its completion or cancellation.
func (ws *WSStat) watchSubscription(state *subscriptionState) {
if state == nil {
return
}
<-state.ctx.Done()
err := state.ctx.Err()
if errors.Is(err, context.Canceled) {
err = nil
}
ws.finalizeSubscription(state, err)
}
// registerSubscription registers a subscription.
func (ws *WSStat) registerSubscription(state *subscriptionState) *Subscription {
ws.subscriptionMu.Lock()
defer ws.subscriptionMu.Unlock()
if ws.subscriptions == nil {
ws.subscriptions = make(map[string]*subscriptionState)
}
ws.subscriptions[state.id] = state
if ws.subscriptionArchive == nil {
ws.subscriptionArchive = make(map[string]SubscriptionStats)
}
delete(ws.subscriptionArchive, state.id)
if state.messages == nil {
state.messages = new(uint64)
}
if state.bytes == nil {
state.bytes = new(uint64)
}
sub := &Subscription{
ID: state.id,
cancel: state.cancel,
done: state.done,
messages: state.messages,
bytes: state.bytes,
updates: state.buffer,
}
return sub
}
// finalizeSubscription finalizes a subscription upon its completion or cancellation.
func (ws *WSStat) finalizeSubscription(state *subscriptionState, finalErr error) {
if state == nil {
return
}
state.mu.Lock()
if state.closed {
state.mu.Unlock()
return
}
state.closed = true
if finalErr != nil {
state.stats.finalErr = finalErr
}
metricsCopy := state.stats
var msgCount, byteCount uint64
if state.messages != nil {
msgCount = atomic.LoadUint64(state.messages)
}
if state.bytes != nil {
byteCount = atomic.LoadUint64(state.bytes)
}
buffer := state.buffer
done := state.done
state.mu.Unlock()
if buffer != nil {
close(buffer)
}
if state.cancel != nil {
state.cancel()
}
subStats := SubscriptionStats{
FirstEvent: ws.durationSinceDial(metricsCopy.firstEvent),
LastEvent: ws.durationSinceDial(metricsCopy.lastEvent),
MessageCount: msgCount,
ByteCount: byteCount,
MeanInterArrival: 0,
Error: metricsCopy.finalErr,
}
if metricsCopy.messageCount > 1 {
subStats.MeanInterArrival =
metricsCopy.totalInterArrival / time.Duration(metricsCopy.messageCount-1)
}
ws.removeSubscription(state.id, &subStats)
if done != nil {
close(done)
}
}
// snapshotSubscriptionStats snapshots the subscription stats.
func (ws *WSStat) snapshotSubscriptionStats() (
stats map[string]SubscriptionStats,
firstEvent time.Time,
lastEvent time.Time,
) {
ws.subscriptionMu.RLock()
defer ws.subscriptionMu.RUnlock()
size := len(ws.subscriptions) + len(ws.subscriptionArchive)
if size == 0 {
return nil, time.Time{}, time.Time{}
}
snap := make(map[string]SubscriptionStats, size)
for id, state := range ws.subscriptions {
state.mu.Lock()
s := state.stats
var msgCount, byteCount uint64
if state.messages != nil {
msgCount = atomic.LoadUint64(state.messages)
}
if state.bytes != nil {
byteCount = atomic.LoadUint64(state.bytes)
}
stats := SubscriptionStats{
FirstEvent: ws.durationSinceDial(s.firstEvent),
LastEvent: ws.durationSinceDial(s.lastEvent),
MessageCount: msgCount,
ByteCount: byteCount,
MeanInterArrival: 0,
Error: s.finalErr,
}
if s.messageCount > 1 {
stats.MeanInterArrival = s.totalInterArrival / time.Duration(s.messageCount-1)
}
state.mu.Unlock()
snap[id] = stats
}
for id, archived := range ws.subscriptionArchive {
snap[id] = archived
}
return snap, ws.subscriptionFirstEvent, ws.subscriptionLastEvent
}
// removeSubscription removes a subscription by ID.
func (ws *WSStat) removeSubscription(id string, stats *SubscriptionStats) {
ws.subscriptionMu.Lock()
defer ws.subscriptionMu.Unlock()
if ws.subscriptions != nil {
delete(ws.subscriptions, id)
}
if stats != nil {
if ws.subscriptionArchive == nil {
ws.subscriptionArchive = make(map[string]SubscriptionStats)
}
ws.subscriptionArchive[id] = *stats
}
}