-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
client_connection.go
277 lines (234 loc) · 7.12 KB
/
client_connection.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
package sse
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"
"github.com/tmaxmax/go-sse/internal/parser"
)
// EventCallback is a function that is used to receive events from a Connection.
type EventCallback func(Event)
// EventCallbackRemover is a function that removes an already registered callback
// from a connection. Calling it multiple times is a no-op.
type EventCallbackRemover func()
// Connection is a connection to an events stream. Created using the Client struct,
// a Connection processes the incoming events and calls the subscribed event callbacks.
// If the connection to the server temporarily fails, the connection will be reattempted.
// Retry values received from servers will be taken into account.
//
// Connections must not be copied after they are created.
type Connection struct { //nolint:govet // The current order aids readability.
mu sync.RWMutex
request *http.Request
callbacks map[string]map[int]EventCallback
callbacksAll map[int]EventCallback
lastEventID string
client Client
buf []byte
bufMaxSize int
callbackID int
isRetry bool
}
// SubscribeMessages subscribes the given callback to all events without type (without or with empty `event` field).
// Remove the callback by calling the returned function.
func (c *Connection) SubscribeMessages(cb EventCallback) EventCallbackRemover {
return c.SubscribeEvent("", cb)
}
// SubscribeEvent subscribes the given callback to all the events with the provided type
// (the `event` field has the value given here).
// Remove the callback by calling the returned function.
func (c *Connection) SubscribeEvent(typ string, cb EventCallback) EventCallbackRemover {
return c.addSubscriber(typ, cb)
}
// SubscribeToAll subscribes the given callback to all events, with or without type.
// Remove the callback by calling the returned function.
func (c *Connection) SubscribeToAll(cb EventCallback) EventCallbackRemover {
return c.addSubscriberToAll(cb)
}
func (c *Connection) addSubscriberToAll(cb EventCallback) EventCallbackRemover {
c.mu.Lock()
defer c.mu.Unlock()
id := c.callbackID
c.callbacksAll[id] = cb
c.callbackID++
return func() {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.callbacksAll, id)
}
}
func (c *Connection) addSubscriber(event string, cb EventCallback) EventCallbackRemover {
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.callbacks[event]; !ok {
c.callbacks[event] = map[int]EventCallback{}
}
id := c.callbackID
c.callbacks[event][id] = cb
c.callbackID++
return func() {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.callbacks[event], id)
if len(c.callbacks[event]) == 0 {
delete(c.callbacks, event)
}
}
}
// Buffer sets the underlying buffer to be used when scanning events.
// Use this if you need to read very large events (bigger than the default
// of 65K bytes).
//
// Read the documentation of bufio.Scanner.Buffer for more information.
func (c *Connection) Buffer(buf []byte, maxSize int) {
c.buf = buf
c.bufMaxSize = maxSize
}
// ConnectionError is the type that wraps all the connection errors that occur.
type ConnectionError struct {
// The request for which the connection failed.
Req *http.Request
// The reason the operation failed.
Err error
// The reason why the request failed.
Reason string
}
func (e *ConnectionError) Error() string {
return fmt.Sprintf("request failed: %s: %v", e.Reason, e.Err)
}
func (e *ConnectionError) Unwrap() error {
return e.Err
}
func (c *Connection) resetRequest() error {
if !c.isRetry {
c.isRetry = true
return nil
}
if err := resetRequestBody(c.request); err != nil {
return err
}
if c.lastEventID == "" {
c.request.Header.Del("Last-Event-ID")
} else {
c.request.Header.Set("Last-Event-ID", c.lastEventID)
}
return nil
}
func (c *Connection) dispatch(ev Event) {
c.mu.RLock()
defer c.mu.RUnlock()
cbs := c.callbacks[ev.Type]
cbCount := len(cbs) + len(c.callbacksAll)
if cbCount == 0 {
return
}
for _, cb := range c.callbacks[ev.Type] {
cb(ev)
}
for _, cb := range c.callbacksAll {
cb(ev)
}
}
func (c *Connection) read(r io.Reader, setRetry func(time.Duration)) error {
pf := func() *parser.Parser {
p := parser.New(r)
if c.buf != nil || c.bufMaxSize > 0 {
p.Buffer(c.buf, c.bufMaxSize)
}
return p
}
var readErr error
read(pf, c.lastEventID, func(r int64) { setRetry(time.Duration(r) * time.Millisecond) }, false)(func(e Event, err error) bool {
if err != nil {
readErr = err
return false
}
c.lastEventID = e.LastEventID
c.dispatch(e)
return true
})
return readErr
}
// Connect sends the request the connection was created with to the server
// and, if successful, it starts receiving events. The caller goroutine
// is blocked until the request's context is done or an error occurs.
//
// If the request's context is cancelled, Connect returns its error.
// Otherwise, if the maximum number or retries is made, the last error
// that occurred is returned. Connect never returns otherwise – either
// the context is cancelled, or it's done retrying.
//
// All errors returned other than the context errors will be wrapped
// inside a *ConnectionError.
func (c *Connection) Connect() error {
ctx := c.request.Context()
backoff := c.client.Backoff.new()
c.request.Header.Set("Accept", "text/event-stream")
c.request.Header.Set("Connection", "keep-alive")
c.request.Header.Set("Cache", "no-cache")
t := time.NewTimer(0)
defer t.Stop()
for {
select {
case <-t.C:
shouldRetry, err := c.doConnect(ctx, backoff.reset)
if !shouldRetry {
return err
}
next, shouldRetry := backoff.next()
if !shouldRetry {
return err
}
if c.client.OnRetry != nil {
c.client.OnRetry(err, next)
}
t.Reset(next)
case <-ctx.Done():
return ctx.Err()
}
}
}
func (c *Connection) doConnect(ctx context.Context, setRetry func(time.Duration)) (shouldRetry bool, err error) {
if err := c.resetRequest(); err != nil {
return false, &ConnectionError{Req: c.request, Reason: "request reset failed", Err: err}
}
res, err := c.client.HTTPClient.Do(c.request)
if err != nil {
concrete := err.(*url.Error) //nolint:errorlint // We know the concrete type here
if errors.Is(err, ctx.Err()) {
return false, concrete.Err
}
return true, &ConnectionError{Req: c.request, Reason: "connection to server failed", Err: concrete.Err}
}
defer res.Body.Close()
if err := c.client.ResponseValidator(res); err != nil {
return false, &ConnectionError{Req: c.request, Reason: "response validation failed", Err: err}
}
setRetry(0)
err = c.read(res.Body, setRetry)
if errors.Is(err, ctx.Err()) {
return false, err
}
return true, &ConnectionError{Req: c.request, Reason: "connection to server lost", Err: err}
}
// ErrNoGetBody is a sentinel error returned when the connection cannot be reattempted
// due to GetBody not existing on the original request.
var ErrNoGetBody = errors.New("the GetBody function doesn't exist on the request")
func resetRequestBody(r *http.Request) error {
if r.Body == nil || r.Body == http.NoBody {
return nil
}
if r.GetBody == nil {
return ErrNoGetBody
}
body, err := r.GetBody()
if err != nil {
return err
}
r.Body = body
return nil
}