/
option.go
55 lines (46 loc) · 1.17 KB
/
option.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
package kafka
import (
"time"
"github.com/Shopify/sarama"
"github.com/mantzas/patron/errors"
)
// OptionFunc definition for configuring the consumer in a functional way.
type OptionFunc func(*consumer) error
// Version option for setting the Kafka version.
func Version(version string) OptionFunc {
return func(c *consumer) error {
if version == "" {
return errors.New("versions has to be provided")
}
v, err := sarama.ParseKafkaVersion(version)
if err != nil {
return errors.Wrap(err, "invalid kafka version provided")
}
c.cfg.Version = v
return nil
}
}
// Buffer option for adjusting the incoming messages buffer.
func Buffer(buf int) OptionFunc {
return func(c *consumer) error {
if buf < 0 {
return errors.New("buffer must greater or equal than 0")
}
c.buffer = buf
return nil
}
}
// Start option for adjusting the start point in the topic.
func Start(start Offset) OptionFunc {
return func(c *consumer) error {
c.start = start
return nil
}
}
// Timeout option for adjusting the timeout of the connection.
func Timeout(timeout time.Duration) OptionFunc {
return func(c *consumer) error {
c.cfg.Net.DialTimeout = timeout
return nil
}
}