/
client.go
135 lines (110 loc) · 2.83 KB
/
client.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
package consumer
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/Shopify/sarama"
)
const (
KafkaGo = "kafka-go"
Sarama = "sarama"
)
type Client interface {
Topics() ([]string, error)
Consume(ctx context.Context, topics []string, ready chan bool) error
Close() error
}
func NewClient(
kafkaClient string,
brokerURLs string,
group string,
ver string,
saramaLog bool,
saramaAssignor string,
saramaOldest bool) (Client, error) {
switch kafkaClient {
case Sarama:
return NewSaramaClient(
brokerURLs, group, ver, saramaLog, saramaAssignor, saramaOldest,
)
case KafkaGo:
return nil, fmt.Errorf(
"not yet supported, waiting for: %s",
"https://github.com/segmentio/kafka-go/issues/131",
)
default:
return nil, fmt.Errorf("kafkaClient not supported: %v\n", kafkaClient)
}
}
func NewSaramaClient(
brokerURLs string,
group string,
ver string,
saramaLog bool,
saramaAssignor string,
saramaOldest bool) (Client, error) {
if saramaLog {
sarama.Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
}
version, err := sarama.ParseKafkaVersion(ver)
if err != nil {
return nil, fmt.Errorf("Error parsing Kafka version: %v\n", err)
}
c := sarama.NewConfig()
c.Version = version
switch saramaAssignor {
case "sticky":
c.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategySticky
case "roundrobin":
c.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
case "range":
c.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRange
default:
return nil, fmt.Errorf(
"Unknown group partition saramaAssignor: %s", saramaAssignor)
}
if saramaOldest {
c.Consumer.Offsets.Initial = sarama.OffsetOldest
}
// disable auto commits of offsets
// https://github.com/Shopify/sarama/issues/1570#issuecomment-574908417
c.Consumer.Offsets.AutoCommit.Enable = false
brokers := strings.Split(brokerURLs, ",")
consumerGroup, err := sarama.NewConsumerGroup(brokers, group, c)
if err != nil {
return nil, fmt.Errorf("Error creating consumer group: %v\n", err)
}
cluster, err := sarama.NewConsumer(brokers, c)
if err != nil {
return nil, fmt.Errorf("Error creating consumer: %v\n", err)
}
return &saramaClient{
cluster: cluster,
consumerGroup: consumerGroup,
consumer: NewSaramaConsumer(),
}, nil
}
type saramaClient struct {
cluster sarama.Consumer
consumerGroup sarama.ConsumerGroup
consumer saramaConsumer
}
func (c *saramaClient) Topics() ([]string, error) {
return c.cluster.Topics()
}
func (c *saramaClient) Consume(
ctx context.Context, topics []string, ready chan bool) error {
c.consumer.ready = ready
return c.consumerGroup.Consume(ctx, topics, c.consumer)
}
func (c *saramaClient) Close() error {
if err := c.consumerGroup.Close(); err != nil {
return err
}
if err := c.cluster.Close(); err != nil {
return err
}
return nil
}