-
Notifications
You must be signed in to change notification settings - Fork 835
/
output_list.go
174 lines (151 loc) · 4.47 KB
/
output_list.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
package redis
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-redis/redis/v7"
ibatch "github.com/benthosdev/benthos/v4/internal/batch"
"github.com/benthosdev/benthos/v4/internal/batch/policy"
"github.com/benthosdev/benthos/v4/internal/bloblang/field"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/internal/component/output/batcher"
"github.com/benthosdev/benthos/v4/internal/component/output/processors"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/impl/redis/old"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/message"
)
func init() {
err := bundle.AllOutputs.Add(processors.WrapConstructor(newRedisListOutput), docs.ComponentSpec{
Name: "redis_list",
Summary: `
Pushes messages onto the end of a Redis list (which is created if it doesn't
already exist) using the RPUSH command.`,
Description: output.Description(true, true, `
The field `+"`key`"+` supports
[interpolation functions](/docs/configuration/interpolation#bloblang-queries), allowing
you to create a unique key for each message.`),
Config: docs.FieldComponent().WithChildren(old.ConfigDocs()...).WithChildren(
docs.FieldString(
"key", "The key for each message, function interpolations can be optionally used to create a unique key per message.",
"benthos_list", "${!meta(\"kafka_key\")}", "${!json(\"doc.id\")}", "${!count(\"msgs\")}",
).IsInterpolated(),
docs.FieldInt("max_in_flight", "The maximum number of messages to have in flight at a given time. Increase this to improve throughput."),
policy.FieldSpec(),
).ChildDefaultAndTypesFromStruct(output.NewRedisListConfig()),
Categories: []string{
"Services",
},
})
if err != nil {
panic(err)
}
}
func newRedisListOutput(conf output.Config, mgr bundle.NewManagement) (output.Streamed, error) {
w, err := newRedisListWriter(conf.RedisList, mgr)
if err != nil {
return nil, err
}
a, err := output.NewAsyncWriter("redis_list", conf.RedisList.MaxInFlight, w, mgr)
if err != nil {
return nil, err
}
return batcher.NewFromConfig(conf.RedisList.Batching, a, mgr)
}
type redisListWriter struct {
log log.Modular
conf output.RedisListConfig
keyStr *field.Expression
client redis.UniversalClient
connMut sync.RWMutex
}
func newRedisListWriter(conf output.RedisListConfig, mgr bundle.NewManagement) (*redisListWriter, error) {
r := &redisListWriter{
log: mgr.Logger(),
conf: conf,
}
var err error
if r.keyStr, err = mgr.BloblEnvironment().NewField(conf.Key); err != nil {
return nil, fmt.Errorf("failed to parse key expression: %v", err)
}
if _, err := clientFromConfig(conf.Config); err != nil {
return nil, err
}
return r, nil
}
func (r *redisListWriter) ConnectWithContext(ctx context.Context) error {
r.connMut.Lock()
defer r.connMut.Unlock()
client, err := clientFromConfig(r.conf.Config)
if err != nil {
return err
}
if _, err = client.Ping().Result(); err != nil {
return err
}
r.client = client
return nil
}
func (r *redisListWriter) WriteWithContext(ctx context.Context, msg *message.Batch) error {
r.connMut.RLock()
client := r.client
r.connMut.RUnlock()
if client == nil {
return component.ErrNotConnected
}
if msg.Len() == 1 {
key := r.keyStr.String(0, msg)
if err := client.RPush(key, msg.Get(0).Get()).Err(); err != nil {
_ = r.disconnect()
r.log.Errorf("Error from redis: %v\n", err)
return component.ErrNotConnected
}
return nil
}
pipe := client.Pipeline()
_ = msg.Iter(func(i int, p *message.Part) error {
key := r.keyStr.String(0, msg)
_ = pipe.RPush(key, p.Get())
return nil
})
cmders, err := pipe.Exec()
if err != nil {
_ = r.disconnect()
r.log.Errorf("Error from redis: %v\n", err)
return component.ErrNotConnected
}
var batchErr *ibatch.Error
for i, res := range cmders {
if res.Err() != nil {
if batchErr == nil {
batchErr = ibatch.NewError(msg, res.Err())
}
batchErr.Failed(i, res.Err())
}
}
if batchErr != nil {
return batchErr
}
return nil
}
func (r *redisListWriter) 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 *redisListWriter) CloseAsync() {
go func() {
_ = r.disconnect()
}()
}
func (r *redisListWriter) WaitForClose(timeout time.Duration) error {
return nil
}