-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathreceiver.go
189 lines (159 loc) · 4.17 KB
/
receiver.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package kafka_sarama
import (
"context"
"io"
"sync"
"github.com/Shopify/sarama"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/protocol"
)
type msgErr struct {
msg binding.Message
err error
}
// Receiver which implements sarama.ConsumerGroupHandler
// After the first invocation of Receiver.Receive(), the sarama.ConsumerGroup is created and started.
type Receiver struct {
once sync.Once
incoming chan msgErr
}
// NewReceiver creates a Receiver which implements sarama.ConsumerGroupHandler
// The sarama.ConsumerGroup must be started invoking. If you need a Receiver which also manage the ConsumerGroup, use NewConsumer
// After the first invocation of Receiver.Receive(), the sarama.ConsumerGroup is created and started.
func NewReceiver() *Receiver {
return &Receiver{
incoming: make(chan msgErr),
}
}
func (r *Receiver) Setup(sarama.ConsumerGroupSession) error {
return nil
}
func (r *Receiver) Cleanup(sarama.ConsumerGroupSession) error {
return nil
}
func (r *Receiver) Close(context.Context) error {
r.once.Do(func() {
close(r.incoming)
})
return nil
}
func (r *Receiver) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for message := range claim.Messages() {
msg := message
m := NewMessageFromConsumerMessage(msg)
r.incoming <- msgErr{
msg: binding.WithFinish(m, func(err error) {
if protocol.IsACK(err) {
session.MarkMessage(msg, "")
}
}),
}
}
return nil
}
func (r *Receiver) Receive(ctx context.Context) (binding.Message, error) {
select {
case <-ctx.Done():
return nil, io.EOF
case msgErr, ok := <-r.incoming:
if !ok {
return nil, io.EOF
}
return msgErr.msg, msgErr.err
}
}
var _ protocol.Receiver = (*Receiver)(nil)
var _ protocol.Closer = (*Receiver)(nil)
type Consumer struct {
Receiver
client sarama.Client
ownClient bool
topic string
groupId string
cgMtx sync.Mutex
}
func NewConsumer(brokers []string, saramaConfig *sarama.Config, groupId string, topic string) (*Consumer, error) {
client, err := sarama.NewClient(brokers, saramaConfig)
if err != nil {
return nil, err
}
consumer := NewConsumerFromClient(client, groupId, topic)
consumer.ownClient = true
return consumer, nil
}
func NewConsumerFromClient(client sarama.Client, groupId string, topic string) *Consumer {
return &Consumer{
Receiver: Receiver{
incoming: make(chan msgErr),
},
client: client,
topic: topic,
groupId: groupId,
ownClient: false,
}
}
func (c *Consumer) OpenInbound(ctx context.Context) error {
c.cgMtx.Lock()
defer c.cgMtx.Unlock()
cg, err := sarama.NewConsumerGroupFromClient(c.groupId, c.client)
if err != nil {
return err
}
errCh := make(chan error)
go c.startConsumerGroupLoop(cg, ctx, errCh)
select {
case <-ctx.Done():
return cg.Close()
case err = <-errCh:
// We still need to close this thing
err2 := cg.Close()
if err == nil {
err = err2
}
// Somebody else closed the client, so no problem here
if err == sarama.ErrClosedClient || err == sarama.ErrClosedConsumerGroup {
return nil
}
return err
}
}
func (c *Consumer) startConsumerGroupLoop(cg sarama.ConsumerGroup, ctx context.Context, errs chan<- error) {
defer c.Receiver.Close(ctx)
// Need to be wrapped in a for loop
// https://godoc.org/github.com/Shopify/sarama#ConsumerGroup
for {
err := cg.Consume(context.Background(), []string{c.topic}, c)
select {
// If context is closed, then consumer group session was closed by the user
case <-ctx.Done():
if err != nil {
errs <- err
}
return
// Something else happened
default:
if err == nil {
continue
} else if err == sarama.ErrClosedClient || err == sarama.ErrClosedConsumerGroup {
// Consumer group closed correctly, we can close that loop
return
} else {
// Another error happened (eg a disconnection to the cluster)
// We need to loop again
errs <- err
}
}
}
}
func (c *Consumer) Close(ctx context.Context) error {
if c.ownClient {
return c.client.Close()
}
return nil
}
var _ protocol.Opener = (*Consumer)(nil)
var _ protocol.Closer = (*Consumer)(nil)