-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sse_event_stream.go
192 lines (159 loc) · 4.18 KB
/
sse_event_stream.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
package http_ns
import (
"errors"
"sync"
"sync/atomic"
"time"
"github.com/inoxlang/inox/internal/core"
)
const (
STOPPING_SUB_WAIT_TIME = time.Millisecond
)
type multiSubscriptionSSEStream struct {
lock sync.Mutex
id string
eventlog eventLog
events chan *ServerSentEvent
stopGoroutine chan struct{}
lastEventHandledAck chan struct{}
stopping atomic.Bool
subscriptions map[*Subscription]struct{}
subCount int
}
func newStream(id string, buffSize int) *multiSubscriptionSSEStream {
return &multiSubscriptionSSEStream{
id: id,
subscriptions: map[*Subscription]struct{}{},
eventlog: make(eventLog, 0),
events: make(chan *ServerSentEvent, buffSize),
stopGoroutine: make(chan struct{}, 1),
lastEventHandledAck: make(chan struct{}, 1),
}
}
func (s *multiSubscriptionSSEStream) startGoroutine() {
go func(s *multiSubscriptionSSEStream) {
defer func() {
close(s.lastEventHandledAck)
close(s.stopGoroutine)
close(s.events)
}()
for {
select {
case event := <-s.events:
s.sendEventToSubscriptions(event)
if s.stopping.Load() && len(s.events) == 0 {
s.lastEventHandledAck <- struct{}{}
}
case <-s.stopGoroutine:
return
}
}
}(s)
}
func (s *multiSubscriptionSSEStream) sendEventToSubscriptions(event *ServerSentEvent) {
s.lock.Lock()
defer s.lock.Unlock()
s.sendEventToSubscriptionsNoLock(event)
}
func (s *multiSubscriptionSSEStream) sendEventToSubscriptionsNoLock(event *ServerSentEvent) {
for sub := range s.subscriptions {
if sub.IsStopped() {
delete(s.subscriptions, sub)
s.subCount--
continue
}
sub.events <- event //TODO: prevent blocking
}
}
// Stop stops the stream, it does not wait for events to be sent.
func (s *multiSubscriptionSSEStream) Stop() {
if s.stopping.CompareAndSwap(false, true) {
s.stopGoroutine <- struct{}{}
s.removeAllSubscriptions()
}
}
// GracefulStop stops the stream, it waits for events to be sent.
func (s *multiSubscriptionSSEStream) GracefulStop(ctx *core.Context, timeout time.Duration) {
if s.stopping.CompareAndSwap(false, true) {
defer func() {
s.removeAllSubscriptions()
s.stopGoroutine <- struct{}{}
}()
start := time.Now()
select {
case <-ctx.Done():
return
case <-time.After(timeout - time.Since(start)):
return
case <-s.lastEventHandledAck:
break
}
s.lock.Lock()
defer s.lock.Unlock()
// stop asynchronously the subscriptions
for sub := range s.subscriptions {
sub.StopAsync()
}
//wait for the subscriptions to be stopped
wait_loop:
for {
for sub := range s.subscriptions {
if sub.IsStopped() {
continue
}
time.Sleep(STOPPING_SUB_WAIT_TIME)
if time.Since(start) > timeout || ctx.IsDone() {
return
}
continue wait_loop
}
return
}
}
}
func (s *multiSubscriptionSSEStream) QueuedEventCount() int {
return len(s.events)
}
func (s *multiSubscriptionSSEStream) PublishAsync(e *ServerSentEvent) {
if s.stopping.Load() {
panic(errors.New("failed to publish SSE: SSE stream is stopping"))
}
s.events <- e
}
// addSubscription creates & add a new subscription to a stream
func (s *multiSubscriptionSSEStream) addSubscription(requestContext *core.Context, lastEventId int, url core.URL) *Subscription {
s.lock.Lock()
defer s.lock.Unlock()
sub := &Subscription{
lastEventId: lastEventId,
events: make(chan *ServerSentEvent, DEFAULT_SUBSCRIPTION_CHAN_SIZE),
url: url,
}
s.subscriptions[sub] = struct{}{}
return sub
}
func (s *multiSubscriptionSSEStream) removeSubscription(sub *Subscription) (newCount int) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.subscriptions[sub]; ok {
s.subCount--
delete(s.subscriptions, sub)
}
return s.subCount
}
func (str *multiSubscriptionSSEStream) removeAllSubscriptions() {
str.lock.Lock()
defer str.lock.Unlock()
for sub := range str.subscriptions {
if !sub.IsStopped() {
sub.StopAsync()
}
}
str.subCount = 0
str.subscriptions = map[*Subscription]struct{}{}
}
func (str *multiSubscriptionSSEStream) SubscriptionCount() int {
str.lock.Lock()
defer str.lock.Unlock()
return str.subCount
}