@@ -14,12 +14,10 @@ limitations under the License.
14
14
package kafka
15
15
16
16
import (
17
- "context"
18
17
"errors"
19
18
"fmt"
20
19
"strconv"
21
20
"sync"
22
- "sync/atomic"
23
21
"time"
24
22
25
23
"github.com/IBM/sarama"
@@ -29,15 +27,8 @@ import (
29
27
)
30
28
31
29
type consumer struct {
32
- k * Kafka
33
- ready chan bool
34
- running chan struct {}
35
- stopped atomic.Bool
36
- once sync.Once
37
- mutex sync.Mutex
38
- skipConsume bool
39
- consumeCtx context.Context
40
- consumeCancel context.CancelFunc
30
+ k * Kafka
31
+ mutex sync.Mutex
41
32
}
42
33
43
34
func (consumer * consumer ) ConsumeClaim (session sarama.ConsumerGroupSession , claim sarama.ConsumerGroupClaim ) error {
@@ -233,27 +224,9 @@ func (consumer *consumer) Cleanup(sarama.ConsumerGroupSession) error {
233
224
}
234
225
235
226
func (consumer * consumer ) Setup (sarama.ConsumerGroupSession ) error {
236
- consumer .once .Do (func () {
237
- close (consumer .ready )
238
- })
239
-
240
227
return nil
241
228
}
242
229
243
- // AddTopicHandler adds a handler and configuration for a topic
244
- func (k * Kafka ) AddTopicHandler (topic string , handlerConfig SubscriptionHandlerConfig ) {
245
- k .subscribeLock .Lock ()
246
- k .subscribeTopics [topic ] = handlerConfig
247
- k .subscribeLock .Unlock ()
248
- }
249
-
250
- // RemoveTopicHandler removes a topic handler
251
- func (k * Kafka ) RemoveTopicHandler (topic string ) {
252
- k .subscribeLock .Lock ()
253
- delete (k .subscribeTopics , topic )
254
- k .subscribeLock .Unlock ()
255
- }
256
-
257
230
// checkBulkSubscribe checks if a bulk handler and config are correctly registered for provided topic
258
231
func (k * Kafka ) checkBulkSubscribe (topic string ) bool {
259
232
if bulkHandlerConfig , ok := k .subscribeTopics [topic ]; ok &&
@@ -275,120 +248,3 @@ func (k *Kafka) GetTopicHandlerConfig(topic string) (SubscriptionHandlerConfig,
275
248
return SubscriptionHandlerConfig {},
276
249
fmt .Errorf ("any handler for messages of topic %s not found" , topic )
277
250
}
278
-
279
- // Subscribe to topic in the Kafka cluster, in a background goroutine
280
- func (k * Kafka ) Subscribe (ctx context.Context ) error {
281
- if k .consumerGroup == "" {
282
- return errors .New ("kafka: consumerGroup must be set to subscribe" )
283
- }
284
-
285
- k .subscribeLock .Lock ()
286
- defer k .subscribeLock .Unlock ()
287
-
288
- topics := k .subscribeTopics .TopicList ()
289
- if len (topics ) == 0 {
290
- // Nothing to subscribe to
291
- return nil
292
- }
293
- k .consumer .skipConsume = true
294
-
295
- ctxCreateFn := func () {
296
- consumeCtx , cancel := context .WithCancel (context .Background ())
297
-
298
- k .consumer .consumeCtx = consumeCtx
299
- k .consumer .consumeCancel = cancel
300
-
301
- k .consumer .skipConsume = false
302
- }
303
-
304
- if k .cg == nil {
305
- cg , err := sarama .NewConsumerGroup (k .brokers , k .consumerGroup , k .config )
306
- if err != nil {
307
- return err
308
- }
309
-
310
- k .cg = cg
311
-
312
- ready := make (chan bool )
313
- k .consumer = consumer {
314
- k : k ,
315
- ready : ready ,
316
- running : make (chan struct {}),
317
- }
318
-
319
- ctxCreateFn ()
320
-
321
- go func () {
322
- k .logger .Debugf ("Subscribed and listening to topics: %s" , topics )
323
-
324
- for {
325
- // If the context was cancelled, as is the case when handling SIGINT and SIGTERM below, then this pops
326
- // us out of the consume loop
327
- if ctx .Err () != nil {
328
- k .logger .Info ("Consume context cancelled" )
329
- break
330
- }
331
-
332
- k .logger .Debugf ("Starting loop to consume." )
333
-
334
- if k .consumer .skipConsume {
335
- continue
336
- }
337
-
338
- topics = k .subscribeTopics .TopicList ()
339
-
340
- // Consume the requested topics
341
- bo := backoff .WithContext (backoff .NewConstantBackOff (k .consumeRetryInterval ), ctx )
342
- innerErr := retry .NotifyRecover (func () error {
343
- if ctxErr := ctx .Err (); ctxErr != nil {
344
- return backoff .Permanent (ctxErr )
345
- }
346
- return k .cg .Consume (k .consumer .consumeCtx , topics , & (k .consumer ))
347
- }, bo , func (err error , t time.Duration ) {
348
- k .logger .Errorf ("Error consuming %v. Retrying...: %v" , topics , err )
349
- }, func () {
350
- k .logger .Infof ("Recovered consuming %v" , topics )
351
- })
352
- if innerErr != nil && ! errors .Is (innerErr , context .Canceled ) {
353
- k .logger .Errorf ("Permanent error consuming %v: %v" , topics , innerErr )
354
- }
355
- }
356
-
357
- k .logger .Debugf ("Closing ConsumerGroup for topics: %v" , topics )
358
- err := k .cg .Close ()
359
- if err != nil {
360
- k .logger .Errorf ("Error closing consumer group: %v" , err )
361
- }
362
-
363
- // Ensure running channel is only closed once.
364
- if k .consumer .stopped .CompareAndSwap (false , true ) {
365
- close (k .consumer .running )
366
- }
367
- }()
368
-
369
- <- ready
370
- } else {
371
- // The consumer group is already created and consuming topics. This means a new subscription is being added
372
- k .consumer .consumeCancel ()
373
- ctxCreateFn ()
374
- }
375
-
376
- return nil
377
- }
378
-
379
- // Close down consumer group resources, refresh once.
380
- func (k * Kafka ) closeSubscriptionResources () {
381
- if k .cg != nil {
382
- err := k .cg .Close ()
383
- if err != nil {
384
- k .logger .Errorf ("Error closing consumer group: %v" , err )
385
- }
386
-
387
- k .consumer .once .Do (func () {
388
- // Wait for shutdown to be complete
389
- <- k .consumer .running
390
- close (k .consumer .ready )
391
- k .consumer .once = sync.Once {}
392
- })
393
- }
394
- }
0 commit comments