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 preBatch feature #83

Merged
merged 12 commits into from
Dec 18, 2023
Merged

feat: add preBatch feature #83

merged 12 commits into from
Dec 18, 2023

Conversation

Abdulsametileri
Copy link
Member

@Abdulsametileri Abdulsametileri commented Dec 16, 2023

In order to prevent race conditions for fat types of events (concurrency is higher than 1), we enable a feature called preBatch. Please take a look at the standalone examples of it.

In retry/exception, there is no need to integrate preBatch for it. We used chunkMessages to handle exceptional cases. chunkMessages is processed after the preBatch method, so it is unnecessary to take action for it.

@@ -11,7 +11,8 @@ import (
type batchConsumer struct {
*base

consumeFn func([]*Message) error
consumeFn BatchConsumeFn
Copy link
Member Author

Choose a reason for hiding this comment

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

We already defined func([]*Message) error alias for this.

b.wg.Add(1)
go b.startConsume()

b.wg.Add(b.concurrency)
Copy link
Member Author

Choose a reason for hiding this comment

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

For more readability, I extract wg.Add within setupConcurrentWorkers method here

@@ -125,6 +128,11 @@ func chunkMessages(allMessages *[]*Message, chunkSize int) [][]*Message {
func (b *batchConsumer) consume(allMessages *[]*Message, commitMessages *[]kafka.Message) {
chunks := chunkMessages(allMessages, b.messageGroupLimit)

if b.preBatchFn != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

Just to let you know, I only changed chunks, not all messages. Because allMessages is used for commitMessages. If I mutate, I have some issues of offset/lag, etc.

@@ -126,6 +126,7 @@ func (c *base) startConsume() {
for {
select {
case <-c.quit:
close(c.incomingMessageStream)
Copy link
Member Author

Choose a reason for hiding this comment

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

Before, we had closed this channel in Stop() method, but this is not the correct way to manage channels.

If a goroutine writes to a channel, It should be that goroutine's responsibility to close the channel, too.

Copy link

codecov bot commented Dec 16, 2023

Codecov Report

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

Comparison is base (9f103dd) 35.53% compared to head (405f871) 37.05%.

Files Patch % Lines
batch_consumer.go 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##               v2      #83      +/-   ##
==========================================
+ Coverage   35.53%   37.05%   +1.51%     
==========================================
  Files          19       19              
  Lines         695      699       +4     
==========================================
+ Hits          247      259      +12     
+ Misses        441      433       -8     
  Partials        7        7              

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

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 cab3410 into v2 Dec 18, 2023
4 checks passed
@Abdulsametileri Abdulsametileri deleted the feature/pre-batch branch January 13, 2024 19:35
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.

3 participants