-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
localsubscriber.go
136 lines (108 loc) · 3 KB
/
localsubscriber.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
package mercure
import (
"net/url"
"sync"
"sync/atomic"
"github.com/gofrs/uuid"
"go.uber.org/zap"
)
// LocalSubscriber represents a client subscribed to a list of topics on the current hub.
type LocalSubscriber struct {
Subscriber
disconnected int32
out chan *Update
outMutex sync.RWMutex
responseLastEventID chan string
ready int32
liveQueue []*Update
liveMutex sync.RWMutex
}
const outBufferLength = 1000
// NewLocalSubscriber creates a new subscriber.
func NewLocalSubscriber(lastEventID string, logger Logger, topicSelectorStore *TopicSelectorStore) *LocalSubscriber {
id := "urn:uuid:" + uuid.Must(uuid.NewV4()).String()
s := &LocalSubscriber{
Subscriber: *NewSubscriber(logger, topicSelectorStore),
responseLastEventID: make(chan string, 1),
out: make(chan *Update, outBufferLength),
}
s.ID = id
s.EscapedID = url.QueryEscape(id)
s.RequestLastEventID = lastEventID
return s
}
// Dispatch an update to the subscriber.
// Security checks must (topics matching) be done before calling Dispatch,
// for instance by calling Match.
func (s *LocalSubscriber) Dispatch(u *Update, fromHistory bool) bool {
if atomic.LoadInt32(&s.disconnected) > 0 {
return false
}
if !fromHistory && atomic.LoadInt32(&s.ready) < 1 {
s.liveMutex.Lock()
if s.ready < 1 {
s.liveQueue = append(s.liveQueue, u)
s.liveMutex.Unlock()
return true
}
s.liveMutex.Unlock()
}
s.outMutex.Lock()
if atomic.LoadInt32(&s.disconnected) > 0 {
s.outMutex.Unlock()
return false
}
select {
case s.out <- u:
s.outMutex.Unlock()
default:
s.handleFullChan()
return false
}
return true
}
// Ready flips the ready flag to true and flushes queued live updates returning number of events flushed.
func (s *LocalSubscriber) Ready() (n int) {
s.liveMutex.Lock()
s.outMutex.Lock()
for _, u := range s.liveQueue {
select {
case s.out <- u:
n++
default:
s.handleFullChan()
s.liveMutex.Unlock()
return n
}
}
atomic.StoreInt32(&s.ready, 1)
s.outMutex.Unlock()
s.liveMutex.Unlock()
return n
}
// Receive returns a chan when incoming updates are dispatched.
func (s *LocalSubscriber) Receive() <-chan *Update {
return s.out
}
// HistoryDispatched must be called when all messages coming from the history have been dispatched.
func (s *LocalSubscriber) HistoryDispatched(responseLastEventID string) {
s.responseLastEventID <- responseLastEventID
}
// Disconnect disconnects the subscriber.
func (s *LocalSubscriber) Disconnect() {
if atomic.LoadInt32(&s.disconnected) > 0 {
return
}
s.outMutex.Lock()
defer s.outMutex.Unlock()
atomic.StoreInt32(&s.disconnected, 1)
close(s.out)
}
// handleFullChan disconnects the subscriber when the out channel is full.
func (s *LocalSubscriber) handleFullChan() {
atomic.StoreInt32(&s.disconnected, 1)
s.outMutex.Unlock()
if c := s.logger.Check(zap.ErrorLevel, "subscriber unable to receive updates fast enough"); c != nil {
c.Write(zap.Object("subscriber", s))
}
}