forked from uber-go/kafka-client
/
types.go
201 lines (172 loc) · 6.52 KB
/
types.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
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package consumer
import (
"errors"
"github.com/Gillani0/kafka-client/internal/util"
"github.com/Gillani0/kafka-client/kafka"
"github.com/Shopify/sarama"
"github.com/bsm/sarama-cluster"
"github.com/golang/protobuf/proto"
"go.uber.org/zap/zapcore"
)
type (
// SaramaConsumer is an interface for external consumer library (sarama)
SaramaConsumer interface {
Close() error
Errors() <-chan error
Notifications() <-chan *cluster.Notification
Partitions() <-chan cluster.PartitionConsumer
CommitOffsets() error
Messages() <-chan *sarama.ConsumerMessage
HighWaterMarks() map[string]map[int32]int64
MarkOffset(msg *sarama.ConsumerMessage, metadata string)
MarkPartitionOffset(topic string, partition int32, offset int64, metadata string)
ResetPartitionOffset(topic string, partition int32, offset int64, metadata string)
}
// saramaConsumer is an internal version of SaramaConsumer that implements a close method that can be safely called
// multiple times.
saramaConsumer struct {
SaramaConsumer
lifecycle *util.RunLifecycle
}
// saramaProducer is an internal version of SaramaConsumer that implements a close method that can be safely called
// multiple times.
saramaProducer struct {
sarama.AsyncProducer
lifecycle *util.RunLifecycle
}
// saramaClient is an internal version of sarama Client that implements a close method that can be safely called
// multiple times
saramaClient struct {
sarama.Client
lifecycle *util.RunLifecycle
}
// Constructors wraps multiple Sarama Constructors, which can be used for tests.
Constructors struct {
NewSaramaProducer func(sarama.Client) (sarama.AsyncProducer, error)
NewSaramaConsumer func([]string, string, []string, *cluster.Config) (SaramaConsumer, error)
NewSaramaClient func([]string, *sarama.Config) (sarama.Client, error)
}
// Topic is an internal wrapper around kafka.ConsumerTopic
Topic struct {
kafka.ConsumerTopic
DLQMetadataDecoder
PartitionConsumerFactory
ConsumerGroupSuffix string
}
// DLQMetadataDecoder decodes a byte array into DLQMetadata.
DLQMetadataDecoder func([]byte) (*DLQMetadata, error)
)
// NoopDLQMetadataDecoder does no decoding and returns a default DLQMetadata object.
func NoopDLQMetadataDecoder(b []byte) (*DLQMetadata, error) {
return newDLQMetadata(), nil
}
// ProtobufDLQMetadataDecoder uses proto.Unmarshal to decode protobuf encoded binary into the DLQMetadata object.
func ProtobufDLQMetadataDecoder(b []byte) (*DLQMetadata, error) {
dlqMetadata := newDLQMetadata()
if b == nil {
return nil, errors.New("expected to decode non-nil byte array to DLQ metadata")
}
if err := proto.Unmarshal(b, dlqMetadata); err != nil {
return nil, err
}
return dlqMetadata, nil
}
// MarshalLogObject implements zapcore.ObjectMarshaler for structured logging.
func (t Topic) MarshalLogObject(e zapcore.ObjectEncoder) error {
t.ConsumerTopic.MarshalLogObject(e)
return nil
}
// NewSaramaConsumer returns a new SaramaConsumer that has a Close method that can be called multiple times.
func NewSaramaConsumer(brokers []string, groupID string, topics []string, config *cluster.Config) (SaramaConsumer, error) {
c, err := cluster.NewConsumer(brokers, groupID, topics, config)
if err != nil {
return nil, err
}
return newSaramaConsumer(c)
}
func newSaramaConsumer(c SaramaConsumer) (SaramaConsumer, error) {
sc := saramaConsumer{
SaramaConsumer: c,
lifecycle: util.NewRunLifecycle("sarama-consumer"),
}
sc.lifecycle.Start(func() error { return nil }) // must start lifecycle so stop will stop
return &sc, nil
}
// Close overwrites the underlying SaramaConsumer Close method with one that can be safely called multiple times.
//
// This close will always return nil error.
func (p *saramaConsumer) Close() error {
p.lifecycle.Stop(func() {
p.SaramaConsumer.Close()
})
return nil
}
// NewSaramaProducer returns a new AsyncProducer that has Close method that can be called multiple times.
func NewSaramaProducer(client sarama.Client) (sarama.AsyncProducer, error) {
p, err := sarama.NewAsyncProducerFromClient(client)
if err != nil {
return nil, err
}
return newSaramaProducer(p)
}
func newSaramaProducer(p sarama.AsyncProducer) (sarama.AsyncProducer, error) {
sp := saramaProducer{
AsyncProducer: p,
lifecycle: util.NewRunLifecycle("sarama-producer"),
}
sp.lifecycle.Start(func() error { return nil }) // must start lifecycle so stop will stop
return &sp, nil
}
// Close overwrites the underlying Sarama Close method with one that can be safely called multiple times.
//
// This close will always return nil error.
func (p *saramaProducer) Close() error {
p.lifecycle.Stop(func() {
p.AsyncProducer.Close()
})
return nil
}
// NewSaramaClient returns an internal sarama Client, which can be safely closed multiple times.
func NewSaramaClient(brokers []string, config *sarama.Config) (sarama.Client, error) {
sc, err := sarama.NewClient(brokers, config)
if err != nil {
return nil, err
}
return newSaramaClient(sc)
}
func newSaramaClient(client sarama.Client) (sarama.Client, error) {
c := &saramaClient{
Client: client,
lifecycle: util.NewRunLifecycle("sarama-client"),
}
c.lifecycle.Start(func() error { return nil }) // must start lifecycle so stop will stop
return c, nil
}
// Close overwrites the underlying Sarama Close method with one that can be safely called multiple times.
//
// This close will always return nil error.
func (c *saramaClient) Close() error {
c.lifecycle.Stop(func() {
c.Client.Close()
})
return nil
}