-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcast.go
More file actions
119 lines (101 loc) · 2.87 KB
/
broadcast.go
File metadata and controls
119 lines (101 loc) · 2.87 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
package spanner
import (
"sync"
"k8s.io/klog/v2"
)
// watchEvent is the internal representation of a storage mutation,
// published through the broadcaster to all active watchers.
type watchEvent struct {
key string
value []byte
prevValue []byte
rev int64
isCreated bool
isDeleted bool
isProgress bool
}
// subscription is a channel-based subscription to broadcast events.
type subscription struct {
ch chan watchEvent
id uint64
closed bool
}
// Broadcaster fans out storage mutation events to all subscribed watchers.
// The write path (Create/Delete/GuaranteedUpdate) calls Publish() after each
// successful Spanner commit. This gives ~microsecond notification latency
// for in-process consumers (the cacher), avoiding the need for Change Streams
// on the hot path.
type Broadcaster struct {
mu sync.RWMutex
subscribers map[uint64]*subscription
nextID uint64
// highWaterMark tracks the highest revision seen, used for
// progress/bookmark events.
highWaterMark int64
}
// NewBroadcaster creates a new Broadcaster.
func NewBroadcaster() *Broadcaster {
return &Broadcaster{
subscribers: make(map[uint64]*subscription),
}
}
// Publish sends an event to all subscribers. Non-blocking: if a subscriber's
// channel is full, the subscription is closed so the watcher detects the gap
// and the cacher can re-list (same behavior as etcd closing a slow watch).
func (b *Broadcaster) Publish(e watchEvent) {
b.mu.Lock()
defer b.mu.Unlock()
if e.rev > b.highWaterMark {
b.highWaterMark = e.rev
}
klog.V(4).Infof("broadcaster.Publish: key=%q rev=%d isCreated=%v isDeleted=%v isProgress=%v subscribers=%d",
e.key, e.rev, e.isCreated, e.isDeleted, e.isProgress, len(b.subscribers))
for id, sub := range b.subscribers {
if sub.closed {
continue
}
select {
case sub.ch <- e:
default:
// Subscriber can't keep up — close it so the watcher
// exits cleanly and the cacher re-establishes the watch.
sub.closed = true
close(sub.ch)
delete(b.subscribers, id)
}
}
}
// Subscribe creates a new subscription. The returned channel receives all
// events published after the subscription is created. bufSize controls the
// channel buffer depth.
func (b *Broadcaster) Subscribe(bufSize int) *subscription {
b.mu.Lock()
defer b.mu.Unlock()
if bufSize < 64 {
bufSize = 64
}
sub := &subscription{
ch: make(chan watchEvent, bufSize),
id: b.nextID,
}
b.nextID++
b.subscribers[sub.id] = sub
return sub
}
// Unsubscribe removes a subscription and closes its channel.
func (b *Broadcaster) Unsubscribe(sub *subscription) {
b.mu.Lock()
defer b.mu.Unlock()
if sub.closed {
return
}
sub.closed = true
close(sub.ch)
delete(b.subscribers, sub.id)
}
// HighWaterMark returns the highest revision seen by the broadcaster.
func (b *Broadcaster) HighWaterMark() int64 {
b.mu.RLock()
defer b.mu.RUnlock()
return b.highWaterMark
}