/
config.go
57 lines (48 loc) · 1.14 KB
/
config.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
package producer
import (
"github.com/Shopify/sarama"
"github.com/tryfix/log"
"github.com/tryfix/metrics"
)
type Config struct {
Id string
*sarama.Config
Pool struct {
NumOfWorkers int
}
BootstrapServers []string
RequiredAcks RequiredAcks
Partitioner Partitioner
Logger log.Logger
MetricsReporter metrics.Reporter
}
func NewConfig() *Config {
c := new(Config)
c.setDefaults()
return c
}
func (c *Config) validate() error {
if err := c.Config.Validate(); err != nil {
return err
}
return nil
}
func (c *Config) setDefaults() {
c.Config = sarama.NewConfig()
c.Producer.RequiredAcks = sarama.RequiredAcks(c.RequiredAcks)
c.Producer.Return.Errors = true
c.Producer.Return.Successes = true
c.Logger = log.NewNoopLogger()
//c.Config.Version = sarama.V2_3_0_0
c.MetricsReporter = metrics.NoopReporter()
c.Producer.Compression = sarama.CompressionSnappy
if c.Partitioner == Manual {
c.Producer.Partitioner = sarama.NewManualPartitioner
}
if c.Partitioner == HashBased {
c.Producer.Partitioner = sarama.NewHashPartitioner
}
if c.Partitioner == Random {
c.Producer.Partitioner = sarama.NewRandomPartitioner
}
}