-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
228 lines (198 loc) · 6.18 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package feedapi
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io"
"net/http"
"strconv"
"strings"
)
// Client struct is a generic-based client-side implementation of the EventFetcher interface.
type Client struct {
httpClient *http.Client
requestProcessor func(r *http.Request) error
logger logrus.FieldLogger
url string
partitionCount int
}
var _ EventFetcher = &Client{}
var ErrRediscoveryNeeded = errors.New("token changed, client needs to re-start consumption process and call Discover again; the number of partitions could potentially be changed")
// NewClient is a constructor for the Client. The partitionCount parameter says how many partitions to expect
// in the V1 protocol; if you do not wish to support the V1 protocol you may pass `NoV1Support`
func NewClient(url string, partitionCount int) Client {
return Client{
httpClient: http.DefaultClient,
requestProcessor: func(r *http.Request) error {
return nil
},
logger: logrus.StandardLogger(),
url: url,
partitionCount: partitionCount,
}
}
// WithHttpClient is a Client method for providing custom HTTP client.
func (c Client) WithHttpClient(httpClient *http.Client) (r Client) {
r = c
r.httpClient = httpClient
return
}
func (c Client) WithRequestProcessor(requestProcessor func(r *http.Request) error) (r Client) {
r = c
r.requestProcessor = requestProcessor
return
}
// WithLogger is a Client method for providing custom logger.
func (c Client) WithLogger(logger logrus.FieldLogger) (r Client) {
r = c
r.logger = logger
return
}
type Partition struct {
Id int `json:"id,string"`
Closed bool `json:"closed,omitempty"`
StartsAfterPartition int `json:"startsAfterPartition,omitempty"`
CursorFromPartitions []int `json:"cursorFromPartitions,omitempty"`
}
const V1Token = "_v1" // FeedInfo.Token = V1Token indicates to use v1 protocol
const NoV1Support = 0
type FeedInfo struct {
Token string `json:"token"`
Partitions []Partition `json:"partitions"`
ExactlyOnce bool `json:"exactlyOnce"`
}
func (c Client) Discover(ctx context.Context) (FeedInfo, error) {
req, err := http.NewRequest(http.MethodGet, c.url, nil)
if err != nil {
return FeedInfo{}, err
}
req = req.WithContext(ctx)
if err = c.requestProcessor(req); err != nil {
return FeedInfo{}, err
}
res, err := c.httpClient.Do(req)
if err != nil {
return FeedInfo{}, err
}
defer func(body io.ReadCloser) {
_ = body.Close()
}(res.Body)
// Just always read it into a string, because we want to print it if something is wrong..
responseBody, err := io.ReadAll(res.Body)
if err != nil {
return FeedInfo{}, err
}
if res.StatusCode == http.StatusBadRequest && c.partitionCount != NoV1Support {
// We treat this as a sign that we are on ZeroEventHub v1 because we did not pass the &n parameter;
// fallback to special version 1 compatability mode. This is only done if
// partitionCount has been set up when making the client.
info := FeedInfo{
Token: V1Token,
}
for i := 0; i != c.partitionCount; i++ {
info.Partitions = append(info.Partitions, Partition{
Id: i,
})
}
return info, nil
}
if res.StatusCode != http.StatusOK {
if len(responseBody) > 1000 {
responseBody = responseBody[:1000]
}
return FeedInfo{}, errors.Errorf("Unexpected status code: %d. Response body: %s", res.StatusCode, responseBody)
}
var info FeedInfo
err = json.Unmarshal(responseBody, &info)
if err != nil {
return FeedInfo{}, errors.Errorf("Failed to unmarshal FeedAPI response, error=%s, response=%s", err, responseBody)
}
return info, nil
}
// FetchEvents is a client-side implementation that queries the server and properly deserializes received data.
func (c Client) FetchEvents(ctx context.Context, token string, partitionID int, cursor string, r EventReceiver, options Options) error {
if token == V1Token {
return c.FetchEventsV1(ctx, partitionID, cursor, r, options)
}
type checkpointOrEvent struct {
Cursor string `json:"cursor"`
// OR, this is set:
Data json.RawMessage `json:"data"`
}
req, err := http.NewRequest(http.MethodGet, c.url+"/events", nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
q := req.URL.Query()
q.Add("token", token)
q.Add("partition", strconv.Itoa(partitionID))
q.Add("cursor", cursor)
if options.PageSizeHint != DefaultPageSize {
q.Add("pagesizehint", fmt.Sprintf("%d", options.PageSizeHint))
}
if options.EventTypes != nil {
q.Add("event-types", strings.Join(options.EventTypes, ";"))
}
req.URL.RawQuery = q.Encode()
if err := c.requestProcessor(req); err != nil {
return err
}
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer func(body io.ReadCloser) {
_, _ = io.Copy(io.Discard, body)
_ = body.Close()
}(res.Body)
if res.StatusCode == http.StatusConflict {
c.logger.WithField("event", "feedapi.token.conflict").Info()
return ErrRediscoveryNeeded
} else if res.StatusCode/100 != 2 {
log := c.logger.WithFields(logrus.Fields{
"responseCode": strconv.Itoa(res.StatusCode),
"requestUrl": req.URL.String(),
}).WithContext(ctx)
if all, err := io.ReadAll(res.Body); err != nil {
log.WithField("event", "feedapi.res_body_read_error").WithError(err).Error()
return err
} else {
if string(all) == "\n" || string(all) == "" {
err = errors.Errorf("response code %d, empty response body", res.StatusCode)
} else {
err = errors.Errorf("response code %d, response body: %s", res.StatusCode, string(all))
}
log.WithField("event", "feedapi.unexpected_response_body").WithError(err).Error()
return err
}
}
scanner := bufio.NewScanner(res.Body)
for scanner.Scan() {
line := bytes.TrimSpace(scanner.Bytes())
if len(line) == 0 {
continue
}
// we only partially parse at this point, as "data" is json.RawMessage
var parsedLine checkpointOrEvent
if err := json.Unmarshal(line, &parsedLine); err != nil {
return err
}
if parsedLine.Cursor != "" {
// checkpoint
if err := r.Checkpoint(parsedLine.Cursor); err != nil {
return err
}
} else {
// event
if err := r.Event(parsedLine.Data); err != nil {
return err
}
}
}
return nil
}