-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
188 lines (154 loc) · 5.52 KB
/
Copy pathclient.go
File metadata and controls
188 lines (154 loc) · 5.52 KB
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
package cloudeventprovider
import (
"context"
"errors"
"fmt"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/client"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/protocol"
)
type CloudEventProviderClient struct {
context context.Context
protocol ProtocolType
conn cloudEventConnection
connectionClient client.Client
connectionType ConnectionType
alive bool
}
var ErrInvalidConfig = errors.New("invalid config (type of Config.Settings does not match Config.Protocol)")
// newClient ignores topic parameter for http connections
func newClient(conf Config, connectionType ConnectionType, topic string) (*CloudEventProviderClient, error) {
ctx := context.Background()
if conf.Settings == nil {
return nil, errors.New("Config.Settings is nil")
}
var connection interface{}
var err error
if conf.Protocol == ProtocolTypeHttp {
httpConfig, ok := conf.Settings.(HttpConfig)
if !ok {
return nil, ErrInvalidConfig
}
switch connectionType {
case ConnectionTypePub, ConnectionTypeReq:
connection, err = cloudevents.NewHTTP(cloudevents.WithTarget(httpConfig.Url))
case ConnectionTypeSub, ConnectionTypeRep:
connection, err = cloudevents.NewHTTP(cloudevents.WithPort(httpConfig.Port), cloudevents.WithPath("/"+httpConfig.Path))
default:
return nil, fmt.Errorf("unknown connectionType: %s. Could not create HttpConnection", connectionType)
}
} else {
connection, err = newCloudEventConnection(conf, connectionType, topic)
}
if err != nil {
return nil, err
}
connectionClient, err := cloudevents.NewClient(connection)
if err != nil {
return nil, err
}
// if http cloudEventConnection = nil
cloudEventConnection, _ := connection.(cloudEventConnection)
return &CloudEventProviderClient{
context: ctx,
protocol: conf.Protocol,
conn: cloudEventConnection,
connectionClient: connectionClient,
connectionType: connectionType,
alive: true,
}, nil
}
func (c *CloudEventProviderClient) Close() error {
//TODO: what about closing http?
if c.protocol == ProtocolTypeHttp {
return nil
}
if err := c.conn.Close(c.context); err != nil {
return err
}
c.alive = false
return nil
}
func (c *CloudEventProviderClient) Alive() bool {
return c.alive
}
// Pub publishes the given event.Event
// DEPRECATED: Use PubCtx
func (c *CloudEventProviderClient) Pub(event event.Event) error {
return c.PubCtx(context.TODO(), event)
}
// PubCtx publishes the given event.Event
func (c *CloudEventProviderClient) PubCtx(ctx context.Context, event event.Event) error {
if c.connectionType != ConnectionTypePub {
return fmt.Errorf("pub is not supported for connectionType %s", c.connectionType)
}
result := c.connectionClient.Send(ctx, event)
if err := getResultError(result); err != nil {
return err
}
return nil
}
// Sub Subscribes the client and calls the given fn on receive
// DEPRECATED: Use SubCtx
func (c *CloudEventProviderClient) Sub(fn func(event event.Event)) error {
return c.SubCtx(context.TODO(), fn)
}
// SubCtx Subscribes the client and calls the given fn on receive
func (c *CloudEventProviderClient) SubCtx(ctx context.Context, fn func(event event.Event)) error {
if c.connectionType != ConnectionTypeSub {
return fmt.Errorf("sub is not supported for connectionType %s", c.connectionType)
}
return c.connectionClient.StartReceiver(ctx, fn)
}
// Request sends the given event.Event as a request and returns the response
// DEPRECATED: Use RequestCtx
func (c *CloudEventProviderClient) Request(event event.Event, timeOut time.Duration) (*event.Event, error) {
ctx, cancelFn := context.WithTimeout(context.TODO(), timeOut)
defer cancelFn()
return c.RequestCtx(ctx, event)
}
// RequestCtx sends the given event.Event as a request and returns the response
func (c *CloudEventProviderClient) RequestCtx(ctx context.Context, event event.Event) (*event.Event, error) {
if c.connectionType != ConnectionTypeReq {
return nil, fmt.Errorf("request is not supported for connectionType %s", c.connectionType)
}
response, result := c.connectionClient.Request(ctx, event)
if err := getResultError(result); err != nil {
return nil, err
}
return response, nil
}
// Reply method is blocking. Use it in a goroutine
// DEPRECATED: Use ReplyCtx
func (c *CloudEventProviderClient) Reply(responseFunc func(ctx context.Context, event event.Event) (*event.Event, error)) error {
return c.ReplyCtx(context.TODO(), responseFunc)
}
// ReplyCtx method is blocking. Use it in a goroutine
func (c *CloudEventProviderClient) ReplyCtx(ctx context.Context, responseFunc func(ctx context.Context, event event.Event) (*event.Event, error)) error {
if c.connectionType != ConnectionTypeRep {
return fmt.Errorf("reply is not supported for connectionType %s", c.connectionType)
}
switch c.protocol {
case ProtocolTypeHttp:
return c.connectionClient.StartReceiver(ctx, responseFunc)
case ProtocolTypeNats:
natsReplyConsumer, ok := c.conn.(natsReplyConsumerInterface)
if !ok {
return fmt.Errorf("reply is not supported for connectionType %s", c.connectionType)
}
return natsReplyConsumer.Reply(ctx, responseFunc)
default:
return fmt.Errorf("reply is not supported for protocol %s", c.protocol)
}
}
func getResultError(result protocol.Result) error {
if cloudevents.IsUndelivered(result) {
return fmt.Errorf("failed to send event: %w", result)
} else if cloudevents.IsNACK(result) {
return fmt.Errorf("failed to publish event, event not ack: %w", result)
} else {
return nil
}
}