-
Notifications
You must be signed in to change notification settings - Fork 3
/
http-push-streamer.go
363 lines (337 loc) · 9.65 KB
/
http-push-streamer.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) 2021 6 River Systems
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package actions
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"strconv"
"sync"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog"
pubsubv1 "google.golang.org/api/pubsub/v1"
"go.6river.tech/mmmbbb/ent"
"go.6river.tech/mmmbbb/logging"
)
type HttpPushStreamer struct {
conn *httpPushStreamConn
ms *MessageStreamer
}
func NewHttpPusher(
subscriptionName string,
subscriptionID uuid.UUID,
endpoint string,
httpClient *http.Client,
entClient *ent.Client,
) *HttpPushStreamer {
conn := newHttpPushConn(subscriptionName, subscriptionID, endpoint, httpClient)
streamer := &MessageStreamer{
Client: entClient,
Logger: conn.logger,
SubscriptionID: &conn.subscriptionID,
SubscriptionName: conn.subscriptionName,
// we want automatic renew for http push delivery
AutomaticNack: false,
}
return &HttpPushStreamer{conn, streamer}
}
func (p *HttpPushStreamer) Go(ctx context.Context) error {
return p.ms.Go(ctx, p.conn)
}
// TODO: this is only exposed for acceptance testing, which is bad, refactor
// tests to not need this
func (p *HttpPushStreamer) CurrentFlowControl() FlowControl {
return FlowControl{
MaxMessages: p.conn.maxMessages,
MaxBytes: p.conn.maxBytes,
}
}
func (p *HttpPushStreamer) LogContexter(c zerolog.Context) zerolog.Context {
return c.
Str("subscriptionName", p.conn.subscriptionName).
Stringer("subscriptionID", p.conn.subscriptionID)
// TODO: include topic info in log context
}
type httpPushStreamConn struct {
logger *logging.Logger
mu sync.Mutex
subscriptionName string // needed to construct the delivery messages
subscriptionID uuid.UUID // for logging
endpoint string
client *http.Client
wg sync.WaitGroup
fastAckQueue chan uuid.UUID
slowAckQueue chan uuid.UUID
nackQueue chan uuid.UUID
maxBytes int
maxMessages int
failing bool
lastFail time.Time
}
var _ StreamConnection = &httpPushStreamConn{}
func newHttpPushConn(
subscriptionName string,
subscriptionID uuid.UUID,
endpoint string,
client *http.Client,
) *httpPushStreamConn {
if client == nil {
client = http.DefaultClient
}
if endpoint == "" {
panic("empty endpoint")
}
return &httpPushStreamConn{
logger: logging.GetLoggerWith("actions/http-streamer", func(c zerolog.Context) zerolog.Context {
return c.
Str("subscriptionName", subscriptionName).
Stringer("subscriptionID", subscriptionID).
Str("endpoint", endpoint)
}),
subscriptionName: subscriptionName,
subscriptionID: subscriptionID,
endpoint: endpoint,
client: client,
fastAckQueue: make(chan uuid.UUID, 10),
slowAckQueue: make(chan uuid.UUID, 10),
nackQueue: make(chan uuid.UUID, 10),
maxBytes: 10_000_000,
maxMessages: 1,
}
}
func (c *httpPushStreamConn) Close() error {
// Wait for any in-progress sends to complete. Having canceled the context
// should cause them to do so quickly.
c.wg.Wait()
return nil
}
func drainIds(dest *[]uuid.UUID, src <-chan uuid.UUID) {
for {
select {
case id := <-src:
*dest = append(*dest, id)
default:
return
}
}
}
func (c *httpPushStreamConn) Receive(ctx context.Context) (*MessageStreamRequest, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case id := <-c.fastAckQueue:
// fast ack, increase message limit
c.mu.Lock()
defer c.mu.Unlock()
ret := &MessageStreamRequest{
Ack: []uuid.UUID{id},
}
drainIds(&ret.Ack, c.fastAckQueue)
if c.maxMessages < 1000 {
mm := c.maxMessages + len(ret.Ack)
if mm > 1000 {
mm = 1000
}
c.maxMessages = mm
ret.FlowControl = &FlowControl{
MaxMessages: c.maxMessages,
MaxBytes: c.maxBytes,
}
}
return ret, nil
case id := <-c.slowAckQueue:
// fast ack, decrease message limit
c.mu.Lock()
defer c.mu.Unlock()
ret := &MessageStreamRequest{
Ack: []uuid.UUID{id},
}
drainIds(&ret.Ack, c.slowAckQueue)
if c.maxMessages > 1 {
mm := c.maxMessages - len(ret.Ack)
if mm < 1 {
mm = 1
}
c.maxMessages = mm
ret.FlowControl = &FlowControl{
MaxMessages: c.maxMessages,
MaxBytes: c.maxBytes,
}
}
return ret, nil
case id := <-c.nackQueue:
// nack, major decrease in message limit
c.mu.Lock()
defer c.mu.Unlock()
ret := &MessageStreamRequest{
Nack: []uuid.UUID{id},
}
drainIds(&ret.Nack, c.nackQueue)
if c.maxMessages > 1 {
mm := c.maxMessages - 10*len(ret.Nack)
if mm < 1 {
mm = 1
}
c.maxMessages = mm
ret.FlowControl = &FlowControl{
MaxMessages: c.maxMessages,
MaxBytes: c.maxBytes,
}
}
return ret, nil
}
}
func (c *httpPushStreamConn) nowFailing(newValue bool) (isChanged bool) {
c.mu.Lock()
isChanged = c.failing != newValue || time.Since(c.lastFail) > time.Minute
c.failing = newValue
if isChanged && newValue {
c.lastFail = time.Now()
}
c.mu.Unlock()
return
}
type PushMessage = pubsubv1.PubsubMessage
// PushRequest represents the type of the POST payload for a push subscription
// operating in Wrapped mode.
//
// Surprisingly, google doesn't export a type for this.
//
// See: https://cloud.google.com/pubsub/docs/push#receive_push
type PushRequest struct {
Message PushMessage `json:"message"`
Subscription string `json:"subscription"`
DeliveryAttempt int `json:"deliveryAttempt"`
}
func (c *httpPushStreamConn) Send(ctx context.Context, del *SubscriptionMessageDelivery) error {
// payload must be base64 encoded since the base API supports binary payloads
payload64 := base64.StdEncoding.EncodeToString(del.Payload)
bodyObject := PushRequest{
Message: PushMessage{
Attributes: del.Attributes,
Data: payload64,
MessageId: del.MessageID.String(),
// OrderingKey set below
// NOTE: format matters!?
PublishTime: del.PublishedAt.Format(time.RFC3339Nano),
},
Subscription: c.subscriptionName,
DeliveryAttempt: del.NumAttempts,
}
if del.OrderKey != nil {
bodyObject.Message.OrderingKey = *del.OrderKey
}
body, err := json.Marshal(&bodyObject)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
c.wg.Add(1)
go func() {
defer c.wg.Done()
start := time.Now()
resp, err := c.client.Do(req)
dur := time.Since(start)
q := c.slowAckQueue
if dur < time.Second {
q = c.fastAckQueue
}
var outcome, httpStatus string // for metrics
if err != nil {
// nack, don't fail the sending
var evt *zerolog.Event
if c.nowFailing(true) {
evt = c.logger.Warn()
} else {
evt = c.logger.Trace()
}
evt.Err(err).Msg("Failed to contact push endpoint")
outcome, httpStatus = "error", "xxx"
q = c.nackQueue
} else {
httpStatus = strconv.Itoa(resp.StatusCode)
defer resp.Body.Close()
respBody, respBodyErr := io.ReadAll(resp.Body)
// TODO: if we fail to read the response, should we treat it as a NACK
// even if the status code was success?
switch resp.StatusCode {
case http.StatusProcessing, http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent:
// all good, leave q as ack queue
// TODO: emulate google's insistence that no-content has no content
c.nowFailing(false)
outcome = "success"
var evt *zerolog.Event
var msg string
if respBodyErr != nil {
evt = c.logger.Error().
Err(respBodyErr)
msg = ("failed to read push subscription's response on ACK")
} else {
evt = c.logger.Trace().
Str("response", string(respBody))
msg = ("successful push")
}
evt.
Int("code", resp.StatusCode).
Str("pushEndpoint", c.endpoint).
Str("subscriptionName", c.subscriptionName).
Msg(msg)
default:
var evt *zerolog.Event
failingChanged := c.nowFailing(true)
if respBodyErr != nil {
evt = c.logger.Error()
} else if failingChanged {
evt = c.logger.Warn()
} else {
evt = c.logger.Trace()
}
evt = evt.
Int("code", resp.StatusCode).
Str("pushEndpoint", c.endpoint).
Str("subscriptionName", c.subscriptionName)
if respBodyErr != nil {
evt.
Err(respBodyErr).
Msg("failed to read response during HTTP error (nack) from push endpoint")
} else {
evt.
Str("response", string(respBody)).
Msg("HTTP error (nack) from push endpoint")
}
outcome = "failure"
q = c.nackQueue
}
}
httpPushDuration.WithLabelValues(outcome, httpStatus).Observe(dur.Seconds())
select {
case q <- del.ID:
case <-ctx.Done():
}
}()
return nil
}