Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add functionality pause and resume for consumer #89

Merged
merged 9 commits into from
Jan 15, 2024

Conversation

Abdulsametileri
Copy link
Member

@Abdulsametileri Abdulsametileri commented Jan 13, 2024

Based on this issue and some internal projects, I implemented a pause/resume consuming mechanism.

Before implementation, I researched segmentio/kafka-go issues for this functionality. I came across this issue. achille-roussel answer is really clear. If I stop calling FetchMessage, the consumer pauses. If I invoke FetchMessage, it resumes. Consumer group is still Stable because when I called kafka.NewConsumer, segmentio creates a goroutine and starts heartbeat to the broker under the hood!

First, I defined two methods, Resume and Pause on the Consumer interface. So our clients can invoke this easily. You can refer to examples/with-pause-resume-consumer/main.go. It basically

consumer, _ := kafka.NewConsumer(consumerCfg)
defer consumer.Stop()

consumer.Consume()
fmt.Println("Consumer started...!")

go func() {
  time.Sleep(10 * time.Second)
  consumer.Pause()
  
  time.Sleep(10 * time.Second)
  consumer.Resume()
  
  time.Sleep(10 * time.Second)
  consumer.Pause()
  
  time.Sleep(10 * time.Second)
  consumer.Resume()
  
  time.Sleep(10 * time.Second)
  consumer.Pause()
}()

I also added related documentation within the example.

Copy link

codecov bot commented Jan 13, 2024

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (4f33fc4) 35.62% compared to head (9565659) 37.07%.

Files Patch % Lines
consumer_base.go 54.54% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##               v2      #89      +/-   ##
==========================================
+ Coverage   35.62%   37.07%   +1.45%     
==========================================
  Files          19       19              
  Lines         685      712      +27     
==========================================
+ Hits          244      264      +20     
- Misses        434      441       +7     
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

consumer_base.go Outdated Show resolved Hide resolved
Copy link
Member

@mhmtszr mhmtszr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@Abdulsametileri Abdulsametileri merged commit d0a5b26 into v2 Jan 15, 2024
4 checks passed
@Abdulsametileri Abdulsametileri deleted the feature/resume-pause-consumer branch January 15, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants