-
Notifications
You must be signed in to change notification settings - Fork 217
/
transport.go
687 lines (609 loc) · 18.2 KB
/
transport.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package http
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"go.uber.org/zap"
"github.com/cloudevents/sdk-go/pkg/cloudevents"
cecontext "github.com/cloudevents/sdk-go/pkg/cloudevents/context"
"github.com/cloudevents/sdk-go/pkg/cloudevents/observability"
"github.com/cloudevents/sdk-go/pkg/cloudevents/transport"
)
// Transport adheres to transport.Transport.
var _ transport.Transport = (*Transport)(nil)
const (
// DefaultShutdownTimeout defines the default timeout given to the http.Server when calling Shutdown.
DefaultShutdownTimeout = time.Minute * 1
// TransportName is the name of this transport.
TransportName = "HTTP"
)
// Transport acts as both a http client and a http handler.
type Transport struct {
// The encoding used to select the codec for outbound events.
Encoding Encoding
// DefaultEncodingSelectionFn allows for other encoding selection strategies to be injected.
DefaultEncodingSelectionFn EncodingSelector
// ShutdownTimeout defines the timeout given to the http.Server when calling Shutdown.
// If nil, DefaultShutdownTimeout is used.
ShutdownTimeout *time.Duration
// Sending
// Client is the http client that will be used to send requests.
// If nil, the Transport will create a one.
Client *http.Client
// Req is the base http request that is used for http.Do.
// Only .Method, .URL, .Close, and .Header is considered.
// If not set, Req.Method defaults to POST.
// Req.URL or context.WithTarget(url) are required for sending.
Req *http.Request
// Receiving
// Receiver is invoked target for incoming events.
Receiver transport.Receiver
// Converter is invoked if the incoming transport receives an undecodable
// message.
Converter transport.Converter
// Port is the port to bind the receiver to. Defaults to 8080.
Port *int
// Path is the path to bind the receiver to. Defaults to "/".
Path string
// Handler is the handler the http Server will use. Use this to reuse the
// http server. If nil, the Transport will create a one.
Handler *http.ServeMux
// LongPollClient is the http client that will be used to long poll.
// If nil and LongPollReq is set, the Transport will create a one.
LongPollClient *http.Client
// LongPollReq is the base http request that is used for long poll.
// Only .Method, .URL, .Close, and .Header is considered.
// If not set, LongPollReq.Method defaults to GET.
// LongPollReq.URL or context.WithLongPollTarget(url) are required to long
// poll on StartReceiver.
LongPollReq *http.Request
listener net.Listener
server *http.Server
handlerRegistered bool
codec transport.Codec
// Create Mutex
crMu sync.Mutex
// Receive Mutex
reMu sync.Mutex
middleware []Middleware
}
func New(opts ...Option) (*Transport, error) {
t := &Transport{
Req: &http.Request{
Method: http.MethodPost,
},
}
if err := t.applyOptions(opts...); err != nil {
return nil, err
}
return t, nil
}
func (t *Transport) applyOptions(opts ...Option) error {
for _, fn := range opts {
if err := fn(t); err != nil {
return err
}
}
return nil
}
func (t *Transport) loadCodec(ctx context.Context) bool {
if t.codec == nil {
t.crMu.Lock()
if t.DefaultEncodingSelectionFn != nil && t.Encoding != Default {
logger := cecontext.LoggerFrom(ctx)
logger.Warn("transport has a DefaultEncodingSelectionFn set but Encoding is not Default. DefaultEncodingSelectionFn will be ignored.")
t.codec = &Codec{
Encoding: t.Encoding,
}
} else {
t.codec = &Codec{
Encoding: t.Encoding,
DefaultEncodingSelectionFn: t.DefaultEncodingSelectionFn,
}
}
t.crMu.Unlock()
}
return true
}
func copyHeaders(from, to http.Header) {
if from == nil || to == nil {
return
}
for header, values := range from {
for _, value := range values {
to.Add(header, value)
}
}
}
// Ensure to is a non-nil map before copying
func copyHeadersEnsure(from http.Header, to *http.Header) {
if len(from) > 0 {
if *to == nil {
*to = http.Header{}
}
copyHeaders(from, *to)
}
}
// Send implements Transport.Send
func (t *Transport) Send(ctx context.Context, event cloudevents.Event) (context.Context, *cloudevents.Event, error) {
ctx, r := observability.NewReporter(ctx, reportSend)
rctx, resp, err := t.obsSend(ctx, event)
if err != nil {
r.Error()
} else {
r.OK()
}
return rctx, resp, err
}
func (t *Transport) obsSend(ctx context.Context, event cloudevents.Event) (context.Context, *cloudevents.Event, error) {
if t.Client == nil {
t.crMu.Lock()
t.Client = &http.Client{}
t.crMu.Unlock()
}
req := http.Request{
Header: HeaderFrom(ctx),
}
if t.Req != nil {
req.Method = t.Req.Method
req.URL = t.Req.URL
req.Close = t.Req.Close
req.Host = t.Req.Host
copyHeadersEnsure(t.Req.Header, &req.Header)
}
// Override the default request with target from context.
if target := cecontext.TargetFrom(ctx); target != nil {
req.URL = target
}
if ok := t.loadCodec(ctx); !ok {
return WithTransportContext(ctx, NewTransportContextFromResponse(nil)), nil, fmt.Errorf("unknown encoding set on transport: %d", t.Encoding)
}
msg, err := t.codec.Encode(ctx, event)
if err != nil {
return WithTransportContext(ctx, NewTransportContextFromResponse(nil)), nil, err
}
if m, ok := msg.(*Message); ok {
m.ToRequest(&req)
return httpDo(ctx, t.Client, &req, func(resp *http.Response, err error) (context.Context, *cloudevents.Event, error) {
rctx := WithTransportContext(ctx, NewTransportContextFromResponse(resp))
if err != nil {
return rctx, nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
respEvent, err := t.MessageToEvent(ctx, &Message{
Header: resp.Header,
Body: body,
})
if err != nil {
isErr := true
handled := false
if txerr, ok := err.(*transport.ErrTransportMessageConversion); ok {
if !txerr.IsFatal() {
isErr = false
}
if txerr.Handled() {
handled = true
}
}
if isErr {
return rctx, nil, err
}
if handled {
return rctx, nil, nil
}
}
if accepted(resp) {
return rctx, respEvent, nil
}
return rctx, respEvent, fmt.Errorf("error sending cloudevent: %s", resp.Status)
})
}
return WithTransportContext(ctx, NewTransportContextFromResponse(nil)), nil, fmt.Errorf("failed to encode Event into a Message")
}
func (t *Transport) MessageToEvent(ctx context.Context, msg *Message) (*cloudevents.Event, error) {
logger := cecontext.LoggerFrom(ctx)
var event *cloudevents.Event
var err error
if msg.CloudEventsVersion() != "" {
// This is likely a cloudevents encoded message, try to decode it.
if ok := t.loadCodec(ctx); !ok {
err = transport.NewErrTransportMessageConversion("http", fmt.Sprintf("unknown encoding set on transport: %d", t.Encoding), false, true)
logger.Error("failed to load codec", zap.Error(err))
} else {
event, err = t.codec.Decode(ctx, msg)
}
} else {
err = transport.NewErrTransportMessageConversion("http", "cloudevents version unknown", false, false)
}
// If codec returns and error, or could not load the correct codec, try
// with the converter if it is set.
if err != nil && t.HasConverter() {
event, err = t.Converter.Convert(ctx, msg, err)
}
// If err is still set, it means that there was no converter, or the
// converter failed to convert.
if err != nil {
logger.Debug("failed to decode message", zap.Error(err))
}
// If event and error are both nil, then there is nothing to do with this event, it was handled.
if err == nil && event == nil {
logger.Debug("convert function returned (nil, nil)")
err = transport.NewErrTransportMessageConversion("http", "convert function handled request", true, false)
}
return event, err
}
// SetReceiver implements Transport.SetReceiver
func (t *Transport) SetReceiver(r transport.Receiver) {
t.Receiver = r
}
// SetConverter implements Transport.SetConverter
func (t *Transport) SetConverter(c transport.Converter) {
t.Converter = c
}
// HasConverter implements Transport.HasConverter
func (t *Transport) HasConverter() bool {
return t.Converter != nil
}
// StartReceiver implements Transport.StartReceiver
// NOTE: This is a blocking call.
func (t *Transport) StartReceiver(ctx context.Context) error {
t.reMu.Lock()
defer t.reMu.Unlock()
if t.LongPollReq != nil {
go func() { _ = t.longPollStart(ctx) }()
}
if t.Handler == nil {
t.Handler = http.NewServeMux()
}
if !t.handlerRegistered {
// handler.Handle might panic if the user tries to use the same path as the sdk.
t.Handler.Handle(t.GetPath(), t)
t.handlerRegistered = true
}
addr, err := t.listen()
if err != nil {
return err
}
t.server = &http.Server{
Addr: addr.String(),
Handler: attachMiddleware(t.Handler, t.middleware),
}
// Shutdown
defer func() {
t.server.Close()
t.server = nil
}()
errChan := make(chan error, 1)
go func() {
errChan <- t.server.Serve(t.listener)
}()
// wait for the server to return or ctx.Done().
select {
case <-ctx.Done():
// Try a gracefully shutdown.
timeout := DefaultShutdownTimeout
if t.ShutdownTimeout != nil {
timeout = *t.ShutdownTimeout
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := t.server.Shutdown(ctx)
<-errChan // Wait for server goroutine to exit
return err
case err := <-errChan:
return err
}
}
func (t *Transport) longPollStart(ctx context.Context) error {
logger := cecontext.LoggerFrom(ctx)
logger.Info("starting long poll receiver")
if t.LongPollClient == nil {
t.crMu.Lock()
t.LongPollClient = &http.Client{}
t.crMu.Unlock()
}
req := &http.Request{
// TODO: decide if it is ok to use HeaderFrom context here.
Header: HeaderFrom(ctx),
}
if t.LongPollReq != nil {
req.Method = t.LongPollReq.Method
req.URL = t.LongPollReq.URL
req.Close = t.LongPollReq.Close
copyHeaders(t.LongPollReq.Header, req.Header)
}
// Override the default request with target from context.
if target := LongPollTargetFrom(ctx); target != nil {
req.URL = target
}
if req.URL == nil {
return errors.New("no long poll target found")
}
req = req.WithContext(ctx)
msgCh := make(chan Message)
defer close(msgCh)
isClosed := false
go func(ch chan<- Message) {
for {
if isClosed {
return
}
if resp, err := t.LongPollClient.Do(req); err != nil {
logger.Errorw("long poll request returned error", err)
uErr := err.(*url.Error)
if uErr.Temporary() || uErr.Timeout() {
continue
}
// TODO: if the transport is throwing errors, we might want to try again. Maybe with a back-off sleep.
// But this error also might be that there was a done on the context.
} else if resp.StatusCode == http.StatusNotModified {
// Keep polling.
continue
} else if resp.StatusCode == http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
if err := resp.Body.Close(); err != nil {
logger.Warnw("error closing long poll response body", zap.Error(err))
}
msg := Message{
Header: resp.Header,
Body: body,
}
msgCh <- msg
} else {
// TODO: not sure what to do with upstream errors yet.
logger.Errorw("unhandled long poll response", zap.Any("resp", resp))
}
}
}(msgCh)
// Attach the long poll request context to the context.
ctx = WithTransportContext(ctx, TransportContext{
URI: req.URL.RequestURI(),
Host: req.URL.Host,
Method: req.Method,
})
for {
select {
case <-ctx.Done():
isClosed = true
return nil
case msg := <-msgCh:
logger.Debug("got a message", zap.Any("msg", msg))
if event, err := t.MessageToEvent(ctx, &msg); err != nil {
logger.Errorw("could not convert http message to event", zap.Error(err))
} else {
logger.Debugw("got an event", zap.Any("event", event))
// TODO: deliver event.
if _, err := t.invokeReceiver(ctx, *event); err != nil {
logger.Errorw("could not invoke receiver event", zap.Error(err))
}
}
}
}
}
// attachMiddleware attaches the HTTP middleware to the specified handler.
func attachMiddleware(h http.Handler, middleware []Middleware) http.Handler {
for _, m := range middleware {
h = m(h)
}
return h
}
type eventError struct {
ctx context.Context
event *cloudevents.Event
err error
}
func httpDo(ctx context.Context, client *http.Client, req *http.Request, fn func(*http.Response, error) (context.Context, *cloudevents.Event, error)) (context.Context, *cloudevents.Event, error) {
// Run the HTTP request in a goroutine and pass the response to fn.
c := make(chan eventError, 1)
req = req.WithContext(ctx)
go func() {
rctx, event, err := fn(client.Do(req))
c <- eventError{ctx: rctx, event: event, err: err}
}()
select {
case <-ctx.Done():
return ctx, nil, ctx.Err()
case ee := <-c:
return ee.ctx, ee.event, ee.err
}
}
// accepted is a helper method to understand if the response from the target
// accepted the CloudEvent.
func accepted(resp *http.Response) bool {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return true
}
return false
}
func (t *Transport) invokeReceiver(ctx context.Context, event cloudevents.Event) (*Response, error) {
ctx, r := observability.NewReporter(ctx, reportReceive)
resp, err := t.obsInvokeReceiver(ctx, event)
if err != nil {
r.Error()
} else {
r.OK()
}
return resp, err
}
func (t *Transport) obsInvokeReceiver(ctx context.Context, event cloudevents.Event) (*Response, error) {
logger := cecontext.LoggerFrom(ctx)
if t.Receiver != nil {
// Note: http does not use eventResp.Reason
eventResp := cloudevents.EventResponse{}
resp := Response{}
err := t.Receiver.Receive(ctx, event, &eventResp)
if err != nil {
logger.Warnw("got an error from receiver fn", zap.Error(err))
resp.StatusCode = http.StatusInternalServerError
return &resp, err
}
if eventResp.Event != nil {
if t.loadCodec(ctx) {
if m, err := t.codec.Encode(ctx, *eventResp.Event); err != nil {
logger.Errorw("failed to encode response from receiver fn", zap.Error(err))
} else if msg, ok := m.(*Message); ok {
resp.Message = *msg
}
} else {
logger.Error("failed to load codec")
resp.StatusCode = http.StatusInternalServerError
return &resp, err
}
// Look for a transport response context
var trx *TransportResponseContext
if ptrTrx, ok := eventResp.Context.(*TransportResponseContext); ok {
// found a *TransportResponseContext, use it.
trx = ptrTrx
} else if realTrx, ok := eventResp.Context.(TransportResponseContext); ok {
// found a TransportResponseContext, make it a pointer.
trx = &realTrx
}
// If we found a TransportResponseContext, use it.
if trx != nil && trx.Header != nil && len(trx.Header) > 0 {
copyHeadersEnsure(trx.Header, &resp.Message.Header)
}
}
if eventResp.Status != 0 {
resp.StatusCode = eventResp.Status
} else {
resp.StatusCode = http.StatusAccepted // default is 202 - Accepted
}
return &resp, err
}
return nil, nil
}
// ServeHTTP implements http.Handler
func (t *Transport) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx, r := observability.NewReporter(req.Context(), reportServeHTTP)
// Add the transport context to ctx.
ctx = WithTransportContext(ctx, NewTransportContext(req))
logger := cecontext.LoggerFrom(ctx)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
logger.Errorw("failed to handle request", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error":"Invalid request"}`))
r.Error()
return
}
event, err := t.MessageToEvent(ctx, &Message{
Header: req.Header,
Body: body,
})
if err != nil {
isFatal := true
handled := false
if txerr, ok := err.(*transport.ErrTransportMessageConversion); ok {
isFatal = txerr.IsFatal()
handled = txerr.Handled()
}
if isFatal {
logger.Errorw("failed to convert http message to event", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(fmt.Sprintf(`{"error":%q}`, err.Error())))
r.Error()
return
}
// if handled, do not pass to receiver.
if handled {
w.WriteHeader(http.StatusNoContent)
r.OK()
return
}
}
if event == nil {
logger.Error("failed to get non-nil event from MessageToEvent")
w.WriteHeader(http.StatusBadRequest)
r.Error()
return
}
resp, err := t.invokeReceiver(ctx, *event)
if err != nil {
logger.Warnw("error returned from invokeReceiver", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(fmt.Sprintf(`{"error":%q}`, err.Error())))
r.Error()
return
}
if resp != nil {
if t.Req != nil {
copyHeaders(t.Req.Header, w.Header())
}
if len(resp.Message.Header) > 0 {
copyHeaders(resp.Message.Header, w.Header())
}
status := http.StatusAccepted
if resp.StatusCode >= 200 && resp.StatusCode < 600 {
status = resp.StatusCode
}
w.Header().Add("Content-Length", strconv.Itoa(len(resp.Message.Body)))
w.WriteHeader(status)
if len(resp.Message.Body) > 0 {
if _, err := w.Write(resp.Message.Body); err != nil {
r.Error()
return
}
}
r.OK()
return
}
w.WriteHeader(http.StatusNoContent)
r.OK()
}
// GetPort returns the listening port.
// Returns -1 if there is a listening error.
// Note this will call net.Listen() if the listener is not already started.
func (t *Transport) GetPort() int {
// Ensure we have a listener and therefore a port.
if _, err := t.listen(); err == nil || t.Port != nil {
return *t.Port
}
return -1
}
func (t *Transport) setPort(port int) {
if t.Port == nil {
t.Port = new(int)
}
*t.Port = port
}
// listen if not already listening, update t.Port
func (t *Transport) listen() (net.Addr, error) {
if t.listener == nil {
port := 8080
if t.Port != nil {
port = *t.Port
if port < 0 || port > 65535 {
return nil, fmt.Errorf("invalid port %d", port)
}
}
var err error
if t.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil {
return nil, err
}
}
addr := t.listener.Addr()
if tcpAddr, ok := addr.(*net.TCPAddr); ok {
t.setPort(tcpAddr.Port)
}
return addr, nil
}
// GetPath returns the path the transport is hosted on. If the path is '/',
// the transport will handle requests on any URI. To discover the true path
// a request was received on, inspect the context from Receive(cxt, ...) with
// TransportContextFrom(ctx).
func (t *Transport) GetPath() string {
path := strings.TrimSpace(t.Path)
if len(path) > 0 {
return path
}
return "/" // default
}