-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproducer.go
183 lines (147 loc) · 3.47 KB
/
producer.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
package ami
import (
"fmt"
"sync"
"time"
"github.com/go-redis/redis/v7"
"github.com/imdario/mergo"
"github.com/ssgreg/repeat"
)
// NewProducer creates new producer client for Ami
//
// Note, that you MUST set in ClusterOptions ReadTimeout and WriteTimeout to at
// least 30-60 seconds, if you set big PipeBufferSize (50000 in examples) and
// if you do producing big messages.
// Reason: one full buffer is send in one big-sized query to Redis.
// So this big query may don't be completed in time and will be retransmitted,
// may be forewer retransmitted.
// This is especially strictly for Producer, because it send big queries with
// big messages. And not so strictly for Consumer, because it send big queries
// with not so big messages (only ids of ACKed messages).
func NewProducer(opt ProducerOptions, ropt *redis.ClusterOptions) (*Producer, error) {
if err := mergo.Merge(&opt, defaultProducerOptions); err != nil {
return nil, err
}
client, err := newClient(clientOptions{
name: opt.Name,
ropt: ropt,
shardsCount: opt.ShardsCount,
})
if err != nil {
return nil, err
}
c := make(chan string, opt.PendingBufferSize)
pr := &Producer{
c: c,
cl: client,
notif: opt.ErrorNotifier,
opt: opt,
wg: &sync.WaitGroup{},
}
pr.wg.Add(1)
go pr.produce()
return pr, nil
}
// Close queue client.
//
// Function locks until all produced messages will be sent to Redis.
// If PendingBufferSize has huge value - Close can wait long time.
func (p *Producer) Close() {
close(p.c)
p.wg.Wait()
}
// Send message.
//
// Message not sended immediately, but pushed to send buffer and sended to Redis
// in other goroutine.
func (p *Producer) Send(m string) {
p.c <- m
}
func (p *Producer) produce() {
shard := 0
buf := make([]string, p.opt.PipeBufferSize)
idx := 0
started := time.Now()
tick := time.NewTicker(p.opt.PipePeriod)
for {
var doStop bool
select {
case m, more := <-p.c:
if !more {
doStop = true
} else {
buf[idx] = m
idx++
}
case <-tick.C:
}
if doStop {
p.sendWithLock(shard, buf[0:idx])
break
}
var doSend bool
switch {
case idx == int(p.opt.PipeBufferSize):
doSend = true
case time.Since(started) >= p.opt.PipePeriod && len(p.c) == 0:
// Don't send by time if there are more messages in channel
// Prefer to collect them in batch to speedup producing
doSend = true
default:
doSend = false
}
if !doSend {
continue
}
p.sendWithLock(shard, buf[0:idx])
idx = 0
started = time.Now()
shard++
if shard == int(p.opt.ShardsCount) {
shard = 0
}
}
p.wg.Done()
}
func (p *Producer) sendWithLock(shard int, buf []string) {
if len(buf) == 0 {
return
}
args := make([]redis.XAddArgs, len(buf))
stream := fmt.Sprintf("qu{%d}_%s", shard, p.opt.Name)
for i, m := range buf {
args[i] = redis.XAddArgs{
ID: "*",
Stream: stream,
Values: map[string]interface{}{"m": m},
}
}
p.wg.Add(1)
go func() {
p.send(args)
p.wg.Done()
}()
}
func (p *Producer) send(args []redis.XAddArgs) {
err := repeat.Repeat(
repeat.Fn(func() error {
pipe := p.cl.rDB.TxPipeline()
for _, m := range args {
pipe.XAdd(&m)
}
_, err := pipe.Exec()
if err != nil {
if p.notif != nil {
p.notif.AmiError(err)
}
return repeat.HintTemporary(err)
}
return nil
}),
repeat.StopOnSuccess(),
repeat.WithDelay(repeat.FullJitterBackoff(500*time.Millisecond).Set()),
)
if err != nil && p.notif != nil {
p.notif.AmiError(err)
}
}