This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/
endpoint.go
89 lines (73 loc) · 2.02 KB
/
endpoint.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
package endpoint
import (
"context"
"errors"
"sync"
"github.com/jmalloc/ax"
opentracing "github.com/opentracing/opentracing-go"
)
// Endpoint is a named source and recipient of messages.
type Endpoint struct {
Name string
OutboundTransport OutboundTransport
InboundTransport InboundTransport
InboundPipeline InboundPipeline
OutboundPipeline OutboundPipeline
RetryPolicy RetryPolicy
SenderValidators []Validator
Tracer opentracing.Tracer
initOnce sync.Once
}
// NewSender returns an ax.Sender that can be used to send messages from this endpoint.
func (ep *Endpoint) NewSender(ctx context.Context) (ax.Sender, error) {
if err := ep.initialize(ctx); err != nil {
return nil, err
}
return SinkSender{
Sink: ep.OutboundPipeline,
Validators: ep.SenderValidators,
}, nil
}
// StartReceiving processes inbound messages until an error occurrs or ctx is canceled.
func (ep *Endpoint) StartReceiving(ctx context.Context) error {
if ep.InboundTransport == nil {
return errors.New("can not receive on send-only endpoint, there is no inbound transport")
}
if ep.InboundPipeline == nil {
return errors.New("can not receive on send-only endpoint, there is no inbound message pipeline")
}
if err := ep.initialize(ctx); err != nil {
return err
}
recv := &receiver{
Transport: ep.InboundTransport,
InboundPipeline: ep.InboundPipeline,
OutboundPipeline: ep.OutboundPipeline,
RetryPolicy: ep.RetryPolicy,
Tracer: ep.Tracer,
}
return recv.Run(ctx)
}
func (ep *Endpoint) initialize(ctx context.Context) (err error) {
ep.initOnce.Do(func() {
err = ep.OutboundTransport.Initialize(ctx, ep.Name)
if err != nil {
return
}
err = ep.OutboundPipeline.Initialize(ctx, ep)
if err != nil {
return
}
if ep.InboundTransport != nil && ep.InboundPipeline != nil {
err = ep.InboundTransport.Initialize(ctx, ep.Name)
if err != nil {
return
}
err = ep.InboundPipeline.Initialize(ctx, ep)
if err != nil {
return
}
}
})
return
}