Skip to content

Fixed some functional issues and improved code readability #3

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions internal/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/IBM/sarama"
"github.com/violetpay-org/queue-streamer/common"
"sync"
"time"

"github.com/IBM/sarama"
"github.com/violetpay-org/queue-streamer/common"
)

var transactionalId int32 = 0
Expand Down Expand Up @@ -234,7 +235,9 @@ func (consumer *StreamConsumer) HandleTxnError(producer sarama.AsyncProducer, me

func (consumer *StreamConsumer) handleTxnError(producer sarama.AsyncProducer, message *sarama.ConsumerMessage, session sarama.ConsumerGroupSession, err error, defaulthandler func() error) {
fmt.Printf("Message consumer: unable to process transaction: %+v", err)
for {
retryCount := 0
maxRetries := 30
for retryCount < maxRetries {
Copy link
Member

Choose a reason for hiding this comment

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

A transaction must be canceled if some error occurs, for exactly-once. So this is good for infinite loop, but this doesn't look good for business policy. In my opinion, it would be better to delay processing (time.Sleep) or log if cross the attempt threshold in a loop. (actually my suggestion is another business feature)

Copy link
Member

Choose a reason for hiding this comment

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

If a transaction error occurs, it will one day be processed if the producer is okay, so no worry to infinite loop. If there is a problem with the producer, it is prevented by handling producer.TxnStatus()& sarama.ProducerTxnFlagFatalError

if producer.TxnStatus()&sarama.ProducerTxnFlagFatalError != 0 {
// fatal error. need to recreate producer.
fmt.Println("Message consumer: producer is in a fatal state, need to recreate it")
Expand All @@ -258,4 +261,7 @@ func (consumer *StreamConsumer) handleTxnError(producer sarama.AsyncProducer, me
return
}
}
if retryCount == maxRetries {
fmt.Println("Error: failed to commit transaction after", maxRetries, "retries")
}
}
7 changes: 4 additions & 3 deletions internal/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package internal_test
import (
"context"
"errors"
"testing"
"time"

"github.com/IBM/sarama"
"github.com/stretchr/testify/assert"
"github.com/violetpay-org/queue-streamer/common"
"github.com/violetpay-org/queue-streamer/internal"
"testing"
"time"
)

var cbrokers = []string{"localhost:9093"}
Expand Down Expand Up @@ -136,6 +137,7 @@ func TestStreamConsumer_ConsumeClaim(t *testing.T) {
msg.DataChan = make(chan *sarama.ConsumerMessage, 1)

go func() {
defer close(msg.DataChan)
Copy link
Member

Choose a reason for hiding this comment

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

Closing the channel is used as a signal that the channel is no longer in use. It is not related to memory leaks. So, 'close' should be called when the data transfer ends, rather than when the goroutine ends (defer).

Copy link
Member

Choose a reason for hiding this comment

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

In this case deleted the code line (close) below and used the defer instead.

time.Sleep(1 * time.Second)
msg.DataChan <- &sarama.ConsumerMessage{
Topic: "test",
Expand All @@ -145,7 +147,6 @@ func TestStreamConsumer_ConsumeClaim(t *testing.T) {
Offset: 0,
}
time.Sleep(1 * time.Second)
close(msg.DataChan)
}()

assert.Equal(t, 0, len(consumer.ProducerPool().Producers()))
Expand Down
4 changes: 2 additions & 2 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func Copy(source interface{}, destin interface{}) {
y := reflect.New(starX.Type())
starY := y.Elem()
starY.Set(starX)
reflect.ValueOf(destin).Elem().Set(y.Elem())
dest.Elem().Set(y.Elem())
} else {
reflect.ValueOf(destin).Elem().Set(x)
dest.Elem().Set(x)
}
}
16 changes: 7 additions & 9 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package internal_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/violetpay-org/queue-streamer/internal"
"testing"
)

type TestStruct struct {
Name string
Age int
}

func TestCopy(t *testing.T) {
t.Parallel()

t.Run("Copy pointer", func(t *testing.T) {
type TestStruct struct {
Name string
Age int
}

source := &TestStruct{Name: "John", Age: 25}
var destin TestStruct
Expand All @@ -25,10 +27,6 @@ func TestCopy(t *testing.T) {
})

t.Run("Copy value", func(t *testing.T) {
type TestStruct struct {
Name string
Age int
}

source := TestStruct{Name: "John", Age: 25}
var destin TestStruct
Expand Down