-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
213 lines (199 loc) · 4.36 KB
/
client.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
package sse
import (
"bufio"
"bytes"
"context"
"errors"
"io"
"net"
"net/http"
"sync"
"time"
)
var ErrStreamClosed = errors.New("sse: stream closed")
// Dial creates a server-sent event (SSE) stream
func Dial(log logger, url string) (*Stream, error) {
return DialWith(http.DefaultClient, log, url)
}
// DialWith creates a server-sent event (SSE) stream with a custom HTTP client.
func DialWith(client *http.Client, log logger, url string) (*Stream, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "text/event-stream")
req.Close = true
res, err := client.Do(req)
if err != nil {
return nil, err
}
stream := &Stream{
log: log,
res: res,
eventCh: make(chan *Event),
errorCh: make(chan error),
closeCh: make(chan struct{}),
}
go stream.loop()
return stream, nil
}
type Stream struct {
log logger
res *http.Response
eventCh chan *Event
errorCh chan error
closeCh chan struct{}
once onceError
closed error
}
type onceError struct {
once sync.Once
err error
}
func (e *onceError) Do(fn func() error) (err error) {
e.once.Do(func() { e.err = fn() })
return e.err
}
func (s *Stream) loop() {
sc := bufio.NewScanner(s.res.Body)
sc.Split(scanLines)
event := &Event{}
data := [][]byte{}
// Return the error
defer func() {
if sc.Err() != nil {
s.errorCh <- sc.Err()
}
}()
// Scan line by line
for sc.Scan() {
line := sc.Bytes()
if len(line) == 0 {
event.Data = bytes.Join(data, []byte{'\n'})
// Don't let pending events block the client from closing
select {
case s.eventCh <- event:
case <-s.closeCh:
break
}
event = &Event{}
data = [][]byte{}
}
key, value := parseLine(line)
switch string(key) {
case "event":
event.Type = string(value)
case "data":
data = append(data, value)
case "id":
event.ID = string(value)
case "retry":
// TODO
default:
// ignored
}
}
}
// Next blocks until recieving the next event from the stream. If the stream is
// closed, it will return an ErrStreamClosed error.
func (s *Stream) Next(ctx context.Context) (*Event, error) {
if s.closed != nil {
return nil, s.closed
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case evt := <-s.eventCh:
if s.closed != nil {
return nil, s.closed
}
return evt, nil
case err := <-s.errorCh:
if s.closed != nil {
return nil, s.closed
}
return nil, err
case <-ticker.C:
s.log.Debug("sse: client waiting for next event")
}
}
}
func (s *Stream) Close() error {
return s.once.Do(s.close)
}
func (s *Stream) close() (err error) {
err = errors.Join(err, s.res.Body.Close())
close(s.closeCh)
// Drain event channel
if e := <-s.errorCh; e != nil {
// Closed errors are expected since we closed the body
if !isExpectedCloseError(e) {
err = errors.Join(err, e)
}
}
close(s.errorCh)
close(s.eventCh)
s.closed = ErrStreamClosed
return err
}
// isExpectedCloseError returns true if the close error is expected
func isExpectedCloseError(err error) bool {
if errors.Is(err, net.ErrClosed) {
return true
}
// Unfortunately this error is not exported
// https://github.com/golang/go/blob/f4274e64aac99aaa9af05988f2f8c36c47554889/src/net/http/transport.go#L2636
if err.Error() == "http: read on closed response body" {
return true
}
return false
}
func parseLine(line []byte) (key []byte, value []byte) {
key = line
colon := bytes.IndexByte(line, ':')
if colon == -1 {
return nil, nil
}
// Handle comments
if colon == 0 {
key = []byte("comment")
value = line[colon+1:]
return key, value
}
// Parse into key-value
key = line[:colon]
value = line[colon+1:]
if value[0] == ' ' {
value = value[1:]
}
return key, value
}
// Scan each line of data
func scanLines(data []byte, eof bool) (advance int, token []byte, err error) {
if i := bytes.IndexAny(data, "\r\n"); i >= 0 {
if data[i] == '\r' {
if i == len(data)-1 {
if eof {
// final line
return len(data), data[:len(data)-1], io.EOF
}
return 0, nil, nil // LF may follow, request more data
}
if data[i+1] == '\n' {
return i + 2, data[:i], nil
}
return i + 1, data[:i], nil
}
// data[i] == '\n'
return i + 1, data[:i], nil
}
if eof {
// final line
return len(data), data, io.EOF
}
// request more data
return 0, nil, nil
}