forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
317 lines (260 loc) · 8.9 KB
/
subscribe.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
package mercure
import (
"encoding/json"
"errors"
"net/http"
"time"
"go.uber.org/zap"
)
type responseController struct {
http.ResponseController
rw http.ResponseWriter
// disconnectionTime is the JWT expiration date minus hub.dispatchTimeout, or time.Now() plus hub.writeTimeout minus hub.dispatchTimeout
disconnectionTime time.Time
// writeDeadline is the JWT expiration date or time.Now() + hub.writeTimeout
writeDeadline time.Time
hub *Hub
subscriber *Subscriber
}
func (rc *responseController) setDispatchWriteDeadline() bool {
if rc.hub.dispatchTimeout == 0 {
return true
}
deadline := time.Now().Add(rc.hub.dispatchTimeout)
if deadline.After(rc.writeDeadline) {
return true
}
if err := rc.SetWriteDeadline(deadline); err != nil {
if c := rc.hub.logger.Check(zap.ErrorLevel, "Unable to set dispatch write deadline"); c != nil {
c.Write(zap.Object("subscriber", rc.subscriber), zap.Error(err))
}
return false
}
return true
}
func (rc *responseController) setDefaultWriteDeadline() bool {
if err := rc.SetWriteDeadline(rc.writeDeadline); err != nil {
if errors.Is(err, http.ErrNotSupported) {
panic(err)
}
if c := rc.hub.logger.Check(zap.InfoLevel, "Error while setting default write deadline"); c != nil {
c.Write(zap.Object("subscriber", rc.subscriber), zap.Error(err))
}
return false
}
return true
}
func (rc *responseController) flush() bool {
if err := rc.Flush(); err != nil {
if errors.Is(err, http.ErrNotSupported) {
panic(err)
}
if c := rc.hub.logger.Check(zap.InfoLevel, "Unable to flush"); c != nil {
c.Write(zap.Object("subscriber", rc.subscriber), zap.Error(err))
}
return false
}
return true
}
func (h *Hub) newResponseController(w http.ResponseWriter, s *Subscriber) *responseController {
wd := h.getWriteDeadline(s)
return &responseController{*http.NewResponseController(w), w, wd.Add(-h.dispatchTimeout), wd, h, s} // nolint:bodyclose
}
func (h *Hub) getWriteDeadline(s *Subscriber) (deadline time.Time) {
if h.writeTimeout != 0 {
deadline = time.Now().Add(h.writeTimeout)
}
if s.Claims != nil && s.Claims.ExpiresAt != nil && (deadline == time.Time{} || s.Claims.ExpiresAt.Time.Before(deadline)) {
deadline = s.Claims.ExpiresAt.Time
}
return
}
// SubscribeHandler creates a keep alive connection and sends the events to the subscribers.
//
//nolint:funlen,gocognit
func (h *Hub) SubscribeHandler(w http.ResponseWriter, r *http.Request) {
s, rc := h.registerSubscriber(w, r)
if s == nil {
return
}
defer h.shutdown(s)
rc.setDefaultWriteDeadline()
var (
heartbeatTimer *time.Timer
heartbeatTimerC <-chan time.Time
disconnectionTimerC <-chan time.Time
)
if h.heartbeat != 0 {
heartbeatTimer = time.NewTimer(h.heartbeat)
defer heartbeatTimer.Stop()
heartbeatTimerC = heartbeatTimer.C
}
if h.writeTimeout != 0 {
disconnectionTimer := time.NewTimer(time.Until(rc.disconnectionTime))
defer disconnectionTimer.Stop()
disconnectionTimerC = disconnectionTimer.C
}
for {
select {
case <-r.Context().Done():
if c := h.logger.Check(zap.DebugLevel, "Connection closed by the client"); c != nil {
c.Write(zap.Object("subscriber", s))
}
return
case <-heartbeatTimerC:
// Send a SSE comment as a heartbeat, to prevent issues with some proxies and old browsers
if !h.write(rc, ":\n") {
return
}
heartbeatTimer.Reset(h.heartbeat)
case <-disconnectionTimerC:
// Cleanly close the HTTP connection before the write deadline to prevent client-side errors
return
case update, ok := <-s.Receive():
if !ok || !h.write(rc, newSerializedUpdate(update).event) {
return
}
if heartbeatTimer != nil {
if !heartbeatTimer.Stop() {
<-heartbeatTimer.C
}
heartbeatTimer.Reset(h.heartbeat)
}
if c := h.logger.Check(zap.InfoLevel, "Update sent"); c != nil {
c.Write(zap.Object("subscriber", s), zap.Object("update", update))
}
}
}
}
// registerSubscriber initializes the connection.
func (h *Hub) registerSubscriber(w http.ResponseWriter, r *http.Request) (*Subscriber, *responseController) {
s := NewSubscriber(retrieveLastEventID(r, h.opt, h.logger), h.logger)
s.Debug = h.debug
s.RemoteAddr = r.RemoteAddr
var privateTopics []string
var claims *claims
if h.subscriberJWT != nil {
var err error
claims, err = authorize(r, h.subscriberJWT, nil, h.cookieName)
if claims != nil {
s.Claims = claims
privateTopics = claims.Mercure.Subscribe
}
if err != nil || (claims == nil && !h.anonymous) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
if c := h.logger.Check(zap.InfoLevel, "Subscriber unauthorized"); c != nil {
c.Write(zap.Object("subscriber", s), zap.Error(err))
}
return nil, nil
}
}
topics := r.URL.Query()["topic"]
if len(topics) == 0 {
http.Error(w, "Missing \"topic\" parameter.", http.StatusBadRequest)
return nil, nil
}
s.SetTopics(topics, privateTopics)
h.dispatchSubscriptionUpdate(s, true)
if err := h.transport.AddSubscriber(s); err != nil {
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
h.dispatchSubscriptionUpdate(s, false)
if c := h.logger.Check(zap.ErrorLevel, "Unable to add subscriber"); c != nil {
c.Write(zap.Object("subscriber", s), zap.Error(err))
}
return nil, nil
}
h.sendHeaders(w, s)
rc := h.newResponseController(w, s)
rc.flush()
if c := h.logger.Check(zap.InfoLevel, "New subscriber"); c != nil {
fields := []LogField{zap.Object("subscriber", s)}
if claims != nil && h.logger.Level() == zap.DebugLevel {
fields = append(fields, zap.Reflect("payload", claims.Mercure.Payload))
}
c.Write(fields...)
}
h.metrics.SubscriberConnected(s)
return s, rc
}
// sendHeaders sends correct HTTP headers to create a keep-alive connection.
func (h *Hub) sendHeaders(w http.ResponseWriter, s *Subscriber) {
// Keep alive, useful only for HTTP 1 clients https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive
w.Header().Set("Connection", "keep-alive")
// Server-sent events https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Sending_events_from_the_server
w.Header().Set("Content-Type", "text/event-stream")
// Disable cache, even for old browsers and proxies
w.Header().Set("Cache-Control", "private, no-cache, no-store, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expire", "0")
// NGINX support https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/#x-accel-buffering
w.Header().Set("X-Accel-Buffering", "no")
if s.RequestLastEventID != "" {
w.Header().Set("Last-Event-ID", <-s.responseLastEventID)
}
// Write a comment in the body
// Go currently doesn't provide a better way to flush the headers
w.Write([]byte{':', '\n'})
}
// retrieveLastEventID extracts the Last-Event-ID from the corresponding HTTP header with a fallback on the query parameter.
func retrieveLastEventID(r *http.Request, opt *opt, logger Logger) string {
if id := r.Header.Get("Last-Event-ID"); id != "" {
return id
}
query := r.URL.Query()
if id := query.Get("lastEventID"); id != "" {
return id
}
if legacyEventIDValues, present := query["Last-Event-ID"]; present {
if opt.isBackwardCompatiblyEnabledWith(7) {
logger.Info("Deprecated: the 'Last-Event-ID' query parameter is deprecated since the version 8 of the protocol, use 'lastEventID' instead.")
if len(legacyEventIDValues) != 0 {
return legacyEventIDValues[0]
}
} else {
logger.Info("Unsupported: the 'Last-Event-ID' query parameter is not supported anymore, use 'lastEventID' instead or enable backward compatibility with version 7 of the protocol.")
}
}
return ""
}
// Write sends the given string to the client.
// It returns false if the subscriber has been disconnected (e.g. timeout).
func (h *Hub) write(rc *responseController, data string) bool {
if !rc.setDispatchWriteDeadline() {
return false
}
if _, err := rc.rw.Write([]byte(data)); err != nil {
if c := h.logger.Check(zap.DebugLevel, "Error writing to client"); c != nil {
c.Write(zap.Object("subscriber", rc.subscriber), zap.Error(err))
}
return false
}
return rc.flush() && rc.setDefaultWriteDeadline()
}
func (h *Hub) shutdown(s *Subscriber) {
// Notify that the client is closing the connection
s.Disconnect()
h.transport.RemoveSubscriber(s)
h.dispatchSubscriptionUpdate(s, false)
if c := h.logger.Check(zap.InfoLevel, "Subscriber disconnected"); c != nil {
c.Write(zap.Object("subscriber", s))
}
h.metrics.SubscriberDisconnected(s)
}
func (h *Hub) dispatchSubscriptionUpdate(s *Subscriber, active bool) {
if !h.subscriptions {
return
}
for _, subscription := range s.getSubscriptions("", jsonldContext, active) {
json, err := json.MarshalIndent(subscription, "", " ")
if err != nil {
panic(err)
}
u := &Update{
Topics: []string{subscription.ID},
Private: true,
Debug: h.debug,
Event: Event{Data: string(json)},
}
h.transport.Dispatch(u)
}
}