/
gcp_pubsub.go
218 lines (194 loc) · 5.77 KB
/
gcp_pubsub.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
package writer
import (
"context"
"fmt"
"sync"
"time"
"cloud.google.com/go/pubsub"
"github.com/Jeffail/benthos/v3/internal/batch"
"github.com/Jeffail/benthos/v3/internal/bloblang/field"
"github.com/Jeffail/benthos/v3/internal/component/output"
"github.com/Jeffail/benthos/v3/internal/interop"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
// GCPPubSubConfig contains configuration fields for the output GCPPubSub type.
type GCPPubSubConfig struct {
ProjectID string `json:"project" yaml:"project"`
TopicID string `json:"topic" yaml:"topic"`
MaxInFlight int `json:"max_in_flight" yaml:"max_in_flight"`
PublishTimeout string `json:"publish_timeout" yaml:"publish_timeout"`
Metadata output.Metadata `json:"metadata" yaml:"metadata"`
}
// NewGCPPubSubConfig creates a new Config with default values.
func NewGCPPubSubConfig() GCPPubSubConfig {
return GCPPubSubConfig{
ProjectID: "",
TopicID: "",
MaxInFlight: 1,
PublishTimeout: "60s",
Metadata: output.NewMetadata(),
}
}
//------------------------------------------------------------------------------
// GCPPubSub is a benthos writer.Type implementation that writes messages to a
// GCP Pub/Sub topic.
type GCPPubSub struct {
conf GCPPubSubConfig
client *pubsub.Client
publishTimeout time.Duration
metaFilter *output.MetadataFilter
topicID *field.Expression
topics map[string]*pubsub.Topic
topicMut sync.Mutex
log log.Modular
stats metrics.Type
}
// NewGCPPubSub creates a new GCP Cloud Pub/Sub writer.Type.
//
// Deprecated: use the V2 API instead.
func NewGCPPubSub(
conf GCPPubSubConfig,
log log.Modular,
stats metrics.Type,
) (*GCPPubSub, error) {
return NewGCPPubSubV2(conf, types.NoopMgr(), log, stats)
}
// NewGCPPubSubV2 creates a new GCP Cloud Pub/Sub writer.Type.
func NewGCPPubSubV2(
conf GCPPubSubConfig,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
) (*GCPPubSub, error) {
client, err := pubsub.NewClient(context.Background(), conf.ProjectID)
if err != nil {
return nil, err
}
topic, err := interop.NewBloblangField(mgr, conf.TopicID)
if err != nil {
return nil, fmt.Errorf("failed to parse topic expression: %v", err)
}
pubTimeout, err := time.ParseDuration(conf.PublishTimeout)
if err != nil {
return nil, fmt.Errorf("failed to parse publish timeout duration: %w", err)
}
metaFilter, err := conf.Metadata.Filter()
if err != nil {
return nil, fmt.Errorf("failed to construct metadata filter: %w", err)
}
return &GCPPubSub{
conf: conf,
log: log,
metaFilter: metaFilter,
client: client,
publishTimeout: pubTimeout,
stats: stats,
topicID: topic,
}, nil
}
// ConnectWithContext attempts to establish a connection to the target GCP
// Pub/Sub topic.
func (c *GCPPubSub) ConnectWithContext(ctx context.Context) error {
c.topicMut.Lock()
defer c.topicMut.Unlock()
if c.topics != nil {
return nil
}
c.topics = map[string]*pubsub.Topic{}
c.log.Infof("Sending GCP Cloud Pub/Sub messages to project '%v' and topic '%v'\n", c.conf.ProjectID, c.conf.TopicID)
return nil
}
func (c *GCPPubSub) getTopic(ctx context.Context, t string) (*pubsub.Topic, error) {
c.topicMut.Lock()
defer c.topicMut.Unlock()
if c.topics == nil {
return nil, types.ErrNotConnected
}
if t, exists := c.topics[t]; exists {
return t, nil
}
topic := c.client.Topic(t)
exists, err := topic.Exists(ctx)
if err != nil {
return nil, fmt.Errorf("failed to validate topic '%v': %v", t, err)
}
if !exists {
return nil, fmt.Errorf("topic '%v' does not exist", t)
}
topic.PublishSettings.Timeout = c.publishTimeout
c.topics[t] = topic
return topic, nil
}
// Connect attempts to establish a connection to the target GCP Pub/Sub topic.
func (c *GCPPubSub) Connect() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
return c.ConnectWithContext(ctx)
}
// WriteWithContext attempts to write message contents to a target topic.
func (c *GCPPubSub) WriteWithContext(ctx context.Context, msg types.Message) error {
topics := make([]*pubsub.Topic, msg.Len())
if err := msg.Iter(func(i int, _ types.Part) error {
var tErr error
topics[i], tErr = c.getTopic(ctx, c.topicID.String(i, msg))
return tErr
}); err != nil {
return err
}
results := make([]*pubsub.PublishResult, msg.Len())
msg.Iter(func(i int, part types.Part) error {
topic := topics[i]
attr := map[string]string{}
c.metaFilter.Iter(part.Metadata(), func(k, v string) error {
attr[k] = v
return nil
})
gmsg := &pubsub.Message{
Data: part.Get(),
}
if len(attr) > 0 {
gmsg.Attributes = attr
}
results[i] = topic.Publish(ctx, gmsg)
return nil
})
var batchErr *batch.Error
for i, r := range results {
if _, err := r.Get(ctx); err != nil {
if batchErr == nil {
batchErr = batch.NewError(msg, err)
}
batchErr.Failed(i, err)
}
}
if batchErr != nil {
return batchErr
}
return nil
}
// Write attempts to write message contents to a target topic.
func (c *GCPPubSub) Write(msg types.Message) error {
return c.WriteWithContext(context.Background(), msg)
}
// CloseAsync begins cleaning up resources used by this reader asynchronously.
func (c *GCPPubSub) CloseAsync() {
go func() {
c.topicMut.Lock()
defer c.topicMut.Unlock()
if c.topics != nil {
for _, t := range c.topics {
t.Stop()
}
c.topics = nil
}
}()
}
// WaitForClose will block until either the reader is closed or a specified
// timeout occurs.
func (c *GCPPubSub) WaitForClose(time.Duration) error {
return nil
}
//------------------------------------------------------------------------------