@@ -33,10 +33,7 @@ type Adapter interface {
33
33
// Config wraps configuration for consumer
34
34
type Config struct {
35
35
ConsumerGroup string
36
- ErrHandler func (* error )
37
36
KafkaBrokers []string
38
- MsgHandler func (* sarama.ConsumerMessage , * Consumer )
39
- NtfnHandler func (* cluster.Notification )
40
37
// Allow overwriting default sarama-config
41
38
SaramaConfig * cluster.Config
42
39
Topics []string
@@ -49,11 +46,9 @@ type Consumer struct {
49
46
isLoggingEnabled bool
50
47
}
51
48
52
- // To facilitate testing. This var gets overwritten by custon
53
- // init function.
54
- // We don't pass the init function as argument or
55
- // via dependency-injection because the purpose of
56
- // this library is to highly abstract the kafka configs
49
+ // To facilitate testing. This var gets overwritten by custom init function.
50
+ // We don't pass the init function as argument or via dependency-injection
51
+ // because the purpose of this library is to abstract the kafka configs.
57
52
var initFunc func ([]string , string , []string , * cluster.Config ) (* cluster.Consumer , error )
58
53
59
54
func init () {
@@ -75,7 +70,6 @@ func New(initConfig *Config) (*Consumer, error) {
75
70
config .Consumer .Offsets .Initial = sarama .OffsetNewest
76
71
config .Consumer .MaxProcessingTime = 10 * time .Second
77
72
config .Consumer .Return .Errors = true
78
- config .Group .Return .Notifications = true
79
73
}
80
74
81
75
consumer , err := initFunc (initConfig .KafkaBrokers , initConfig .ConsumerGroup , initConfig .Topics , config )
@@ -91,16 +85,12 @@ func New(initConfig *Config) (*Consumer, error) {
91
85
isLoggingEnabled : false ,
92
86
}
93
87
94
- // Don't run these functions when mocking consumer,
95
- // where initial consumer is nil.
88
+ // Don't run this function when mocking consumer, where
89
+ // initial consumer is nil.
96
90
// This initialization is controlled by mock consumer.
97
91
if consumer != nil {
98
92
proxyConsumer .handleKeyInterrupt ()
99
- proxyConsumer .handleErrors (initConfig .ErrHandler )
100
- proxyConsumer .handleMessages (initConfig .MsgHandler )
101
- proxyConsumer .handleNotifications (initConfig .NtfnHandler )
102
93
}
103
- // log.Println("Consumer waiting for messages.")
104
94
return & proxyConsumer , nil
105
95
}
106
96
@@ -130,51 +120,20 @@ func (c *Consumer) handleKeyInterrupt() {
130
120
// Elegant exit
131
121
go func () {
132
122
<- sigChan
133
- log .Println ("Keyboard-Interrupt signal received. " )
123
+ log .Println ("Keyboard-Interrupt signal received, cleaning up before closing " )
134
124
closeError := <- c .Close ()
135
125
log .Println (closeError )
136
126
}()
137
127
}
138
128
139
- func (c * Consumer ) handleErrors (errHandler func (* error )) {
140
- consumer := c .SaramaConsumerGroup ()
141
- go func () {
142
- for err := range consumer .Errors () {
143
- if c .isLoggingEnabled {
144
- log .Fatalln ("Failed to read messages from topic:" , err )
145
- }
146
- if errHandler != nil {
147
- errHandler (& err )
148
- }
149
- }
150
- }()
129
+ // Errors returns the error-channel for Consumer.
130
+ func (c * Consumer ) Errors () <- chan error {
131
+ return c .consumer .Errors ()
151
132
}
152
133
153
- func (c * Consumer ) handleMessages (msgHandler func (* sarama.ConsumerMessage , * Consumer )) {
154
- consumer := c .SaramaConsumerGroup ()
155
- go func () {
156
- for message := range consumer .Messages () {
157
- if c .isLoggingEnabled {
158
- log .Printf ("Topic: %s\t Partition: %v\t Offset: %v\n " , message .Topic , message .Partition , message .Offset )
159
- }
160
- msgHandler (message , c )
161
- }
162
- }()
163
- }
164
-
165
- // Consumer-Rebalancing notifications
166
- func (c * Consumer ) handleNotifications (ntfnHandler func (* cluster.Notification )) {
167
- consumer := c .SaramaConsumerGroup ()
168
- go func () {
169
- for ntf := range consumer .Notifications () {
170
- if c .isLoggingEnabled {
171
- log .Printf ("Rebalanced: %+v\n " , ntf )
172
- }
173
- if ntfnHandler != nil {
174
- ntfnHandler (ntf )
175
- }
176
- }
177
- }()
134
+ // Messages returns the messages-channel for Consumer.
135
+ func (c * Consumer ) Messages () <- chan * sarama.ConsumerMessage {
136
+ return c .consumer .Messages ()
178
137
}
179
138
180
139
// Close attempts to close the consumer,
0 commit comments