/
output_pubsub.go
179 lines (152 loc) · 4.05 KB
/
output_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
package redis
import (
"context"
"fmt"
"sync"
"github.com/redis/go-redis/v9"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/public/service"
)
const (
psoFieldChannel = "channel"
psoFieldBatching = "batching"
)
func redisPubSubOutputConfig() *service.ConfigSpec {
return service.NewConfigSpec().
Stable().
Summary(`Publishes messages through the Redis PubSub model. It is not possible to guarantee that messages have been received.`).
Description(output.Description(true, true, `
This output will interpolate functions within the channel field, you can find a list of functions [here](/docs/configuration/interpolation#bloblang-queries).`)).
Categories("Services").
Fields(clientFields()...).
Fields(
service.NewInterpolatedStringField(psoFieldChannel).
Description("The channel to publish messages to."),
service.NewOutputMaxInFlightField(),
service.NewBatchPolicyField(psoFieldBatching),
)
}
func init() {
err := service.RegisterBatchOutput(
"redis_pubsub", redisPubSubOutputConfig(),
func(conf *service.ParsedConfig, mgr *service.Resources) (out service.BatchOutput, batchPol service.BatchPolicy, mif int, err error) {
if batchPol, err = conf.FieldBatchPolicy(psoFieldBatching); err != nil {
return
}
if mif, err = conf.FieldMaxInFlight(); err != nil {
return
}
out, err = newRedisPubSubWriter(conf, mgr)
return
})
if err != nil {
panic(err)
}
}
type redisPubSubWriter struct {
log *service.Logger
channelStr string
channel *service.InterpolatedString
clientCtor func() (redis.UniversalClient, error)
client redis.UniversalClient
connMut sync.RWMutex
}
func newRedisPubSubWriter(conf *service.ParsedConfig, mgr *service.Resources) (r *redisPubSubWriter, err error) {
r = &redisPubSubWriter{
log: mgr.Logger(),
clientCtor: func() (redis.UniversalClient, error) {
return getClient(conf)
},
}
if r.channelStr, err = conf.FieldString(psoFieldChannel); err != nil {
return
}
if r.channel, err = conf.FieldInterpolatedString(psoFieldChannel); err != nil {
return
}
if _, err := getClient(conf); err != nil {
return nil, err
}
return r, nil
}
func (r *redisPubSubWriter) Connect(ctx context.Context) error {
r.connMut.Lock()
defer r.connMut.Unlock()
client, err := r.clientCtor()
if err != nil {
return err
}
if _, err = client.Ping(ctx).Result(); err != nil {
return err
}
r.client = client
return nil
}
func (r *redisPubSubWriter) WriteBatch(ctx context.Context, batch service.MessageBatch) error {
r.connMut.RLock()
client := r.client
r.connMut.RUnlock()
if client == nil {
return service.ErrNotConnected
}
if len(batch) == 1 {
channel, err := r.channel.TryString(batch[0])
if err != nil {
return fmt.Errorf("channel interpolation error: %w", err)
}
mBytes, err := batch[0].AsBytes()
if err != nil {
return err
}
if err := client.Publish(ctx, channel, mBytes).Err(); err != nil {
_ = r.disconnect()
r.log.Errorf("Error from redis: %v\n", err)
return service.ErrNotConnected
}
return nil
}
pipe := client.Pipeline()
for i := 0; i < len(batch); i++ {
channel, err := batch.TryInterpolatedString(i, r.channel)
if err != nil {
return fmt.Errorf("channel interpolation error: %w", err)
}
mBytes, err := batch[i].AsBytes()
if err != nil {
return err
}
_ = pipe.Publish(ctx, channel, mBytes)
}
cmders, err := pipe.Exec(ctx)
if err != nil {
_ = r.disconnect()
r.log.Errorf("Error from redis: %v\n", err)
return service.ErrNotConnected
}
var batchErr *service.BatchError
for i, res := range cmders {
if res.Err() != nil {
if batchErr == nil {
batchErr = service.NewBatchError(batch, res.Err())
}
batchErr.Failed(i, res.Err())
}
}
if batchErr != nil {
return batchErr
}
return nil
}
func (r *redisPubSubWriter) disconnect() error {
r.connMut.Lock()
defer r.connMut.Unlock()
if r.client != nil {
err := r.client.Close()
r.client = nil
return err
}
return nil
}
func (r *redisPubSubWriter) Close(context.Context) error {
return r.disconnect()
}