-
Notifications
You must be signed in to change notification settings - Fork 15
/
simple_stream.go
183 lines (156 loc) · 4.69 KB
/
simple_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
package nakadi
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
)
// simpleStreamOpener implements the streamOpener interface.
type simpleStreamOpener struct {
client *Client
subscriptionID string
batchLimit uint
flushTimeout uint
maxUncommittedEvents uint
}
func (so *simpleStreamOpener) openStream() (streamer, error) {
req, err := http.NewRequest("GET", so.streamURL(so.subscriptionID), nil)
if err != nil {
return nil, errors.Wrap(err, "unable to create request")
}
if so.client.tokenProvider != nil {
token, err := so.client.tokenProvider()
if err != nil {
return nil, errors.Wrap(err, "unable to open stream")
}
req.Header.Set("Authorization", "Bearer "+token)
}
response, err := so.client.httpStreamClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "unable to create stream")
}
if response.StatusCode >= 400 {
buffer, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.Wrap(err, "unable to read response body")
}
return nil, decodeResponseToError(buffer, "unable to open stream")
}
s := &simpleStream{
nakadiStreamID: response.Header.Get("X-Nakadi-StreamId"),
buffer: bufio.NewReader(response.Body),
closer: response.Body,
readTimeout: 2 * nakadiHeartbeatInterval,
}
return s, nil
}
func (so *simpleStreamOpener) streamURL(id string) string {
queryParams := url.Values{}
if so.batchLimit > 0 {
queryParams.Add("batch_limit", strconv.FormatUint(uint64(so.batchLimit), 10))
}
if so.flushTimeout > 0 {
queryParams.Add("batch_flush_timeout", strconv.FormatUint(uint64(so.flushTimeout), 10))
}
if so.maxUncommittedEvents > 0 {
queryParams.Add("max_uncommitted_events", strconv.FormatUint(uint64(so.maxUncommittedEvents), 10))
}
return fmt.Sprintf("%s/subscriptions/%s/events?%s", so.client.nakadiURL, id, queryParams.Encode())
}
// simpleStream implements the streamer interface.
type simpleStream struct {
nakadiStreamID string
buffer *bufio.Reader
closer io.Closer
readTimeout time.Duration
}
func (s *simpleStream) nextEvents() (Cursor, []byte, error) {
if s.buffer == nil {
return Cursor{}, nil, errors.New("failed to read next batch: stream is closed")
}
fragment, isPrefix, err := s.readLineTimeout()
if err != nil {
return Cursor{}, nil, errors.Wrap(err, "failed to read next batch")
}
line := make([]byte, len(fragment))
copy(line, fragment)
for isPrefix {
var add []byte
add, isPrefix, err = s.readLineTimeout()
if err != nil {
return Cursor{}, nil, errors.Wrap(err, "failed to read next batch")
}
line = append(line, add...)
}
batch := struct {
Cursor Cursor `json:"cursor"`
Events *json.RawMessage `json:"events"`
}{}
err = json.Unmarshal(line, &batch)
if err != nil {
return Cursor{}, nil, errors.Wrap(err, "failed to unmarshal next batch")
}
batch.Cursor.NakadiStreamID = s.nakadiStreamID
if batch.Events == nil {
return batch.Cursor, nil, nil
}
return batch.Cursor, *batch.Events, nil
}
func (s *simpleStream) readLineTimeout() ([]byte, bool, error) {
timer := time.AfterFunc(s.readTimeout, func() { s.closer.Close() })
defer timer.Stop()
return s.buffer.ReadLine()
}
func (s *simpleStream) closeStream() error {
s.buffer = nil
return s.closer.Close()
}
// simpleCommitter implements the committer interface.
type simpleCommitter struct {
client *Client
subscriptionID string
}
func (s *simpleCommitter) commitCursor(cursor Cursor) error {
wrap := &struct {
Items []Cursor `json:"items"`
}{Items: []Cursor{cursor}}
data, err := json.Marshal(wrap)
if err != nil {
return errors.Wrap(err, "unable to unmarshal cursor")
}
req, err := http.NewRequest("POST", s.commitURL(s.subscriptionID), bytes.NewReader(data))
if err != nil {
return errors.Wrap(err, "unable to create request")
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("X-Nakadi-StreamId", cursor.NakadiStreamID)
if s.client.tokenProvider != nil {
token, err := s.client.tokenProvider()
if err != nil {
return errors.Wrap(err, "unable to commit cursor")
}
req.Header.Set("Authorization", "Bearer "+token)
}
response, err := s.client.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "unable to commit cursor")
}
defer response.Body.Close()
if response.StatusCode >= 400 {
buffer, err := io.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "unable to read response body")
}
return decodeResponseToError(buffer, "unable to commit cursor")
}
return nil
}
func (s *simpleCommitter) commitURL(id string) string {
return fmt.Sprintf("%s/subscriptions/%s/cursors", s.client.nakadiURL, id)
}