-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasyncProducer.go
72 lines (64 loc) · 1.68 KB
/
asyncProducer.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
package kago
import (
"github.com/Shopify/sarama"
"log"
)
type AsyncProducer struct {
producer sarama.AsyncProducer
Id int
ProducerGroupId string
}
// one asyncProducer without retry
func InitManualRetryAsyncProducer(addr []string, conf *Config) (*AsyncProducer, error) {
conf.Config.Producer.Retry.Max = 0
aSyncProducer := &AsyncProducer{
Id: 0,
ProducerGroupId: "",
}
var err error
aSyncProducer.producer, err = sarama.NewAsyncProducer(addr, &conf.Config.Config)
if err != nil {
log.Print(err)
return nil, err
}
return aSyncProducer, nil
}
// some(config.AsyncProducerAmount) asyncProducer without retry
func InitManualRetryAsyncProducerGroup(addr []string, conf *Config, groupId string) ([]*AsyncProducer, error) {
conf.Config.Producer.Retry.Max = 0
producerAmount := conf.AsyncProducerAmount
if producerAmount < 1 {
producerAmount = 1
}
var err2 error
var producerSli []*AsyncProducer
for i := 0; i < producerAmount; i++ {
aSyncProducer := &AsyncProducer{
Id: i,
ProducerGroupId: groupId,
}
var err error
aSyncProducer.producer, err = sarama.NewAsyncProducer(addr, &conf.Config.Config)
if err != nil {
err2 = err
log.Print(err)
} else {
producerSli = append(producerSli, aSyncProducer)
}
}
return producerSli, err2
}
//send message
func (asp *AsyncProducer) Send() chan<- *ProducerMessage {
return asp.producer.Input()
}
func (asp *AsyncProducer) Successes() <-chan *ProducerMessage {
return asp.producer.Successes()
}
func (asp *AsyncProducer) Errors() <-chan *ProducerError {
return asp.producer.Errors()
}
func (asp *AsyncProducer) Close() (err error) {
err = asp.producer.Close()
return
}