forked from st3v/go-plugins
/
options.go
61 lines (51 loc) · 1.56 KB
/
options.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
package kafka
import (
"context"
"github.com/Shopify/sarama"
"github.com/micro/go-micro/broker"
)
var (
DefaultBrokerConfig = sarama.NewConfig()
DefaultClusterConfig = sarama.NewConfig()
)
type brokerConfigKey struct{}
type clusterConfigKey struct{}
func BrokerConfig(c *sarama.Config) broker.Option {
return setBrokerOption(brokerConfigKey{}, c)
}
func ClusterConfig(c *sarama.Config) broker.Option {
return setBrokerOption(clusterConfigKey{}, c)
}
type subscribeContextKey struct{}
// SubscribeContext set the context for broker.SubscribeOption
func SubscribeContext(ctx context.Context) broker.SubscribeOption {
return setSubscribeOption(subscribeContextKey{}, ctx)
}
// consumerGroupHandler is the implementation of sarama.ConsumerGroupHandler
type consumerGroupHandler struct {
handler broker.Handler
subopts broker.SubscribeOptions
kopts broker.Options
cg sarama.ConsumerGroup
sess sarama.ConsumerGroupSession
}
func (*consumerGroupHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
func (*consumerGroupHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
func (h *consumerGroupHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for msg := range claim.Messages() {
var m broker.Message
if err := h.kopts.Codec.Unmarshal(msg.Value, &m); err != nil {
continue
}
if err := h.handler(&publication{
m: &m,
t: msg.Topic,
km: msg,
cg: h.cg,
sess: sess,
}); err == nil && h.subopts.AutoAck {
sess.MarkMessage(msg, "")
}
}
return nil
}