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

producer: avoid sending duplicate data #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,23 @@ func (w *Producer) popTransaction(frameType int32, data []byte) {
}

func (w *Producer) transactionCleanup() {
// clean up transactions we can easily account for
for _, t := range w.transactions {
t.Error = ErrNotConnected
t.finish()
l:
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this label is necessary since it's not a nested for loop.

Copy link
Author

Choose a reason for hiding this comment

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

In for loop, there's a select in it, without label, break only jump out from select not for.

for {
select {
case data := <-w.responseChan:
Copy link
Member

Choose a reason for hiding this comment

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

I think we probably have the same race in w.errorChan, so we should flush that channel too.

Copy link
Author

Choose a reason for hiding this comment

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

I think you're right. The real error reason should pass to user, not just set error reason to ErrNotConnected. I will add this code.

w.popTransaction(FrameTypeResponse, data)
Copy link
Member

Choose a reason for hiding this comment

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

Should these be popped and finished with ErrNotConnected, like below?

Copy link
Author

Choose a reason for hiding this comment

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

I think the release code like below:

func (w *Producer) transactionCleanup() {
l:
	for {
		select {
		case data := <-w.responseChan:
			w.popTransaction(FrameTypeResponse, data)
		case data := <-w.errorChan:
			w.popTransaction(FrameTypeError, data)
		default:
			// clean up transactions we can easily account for
			for _, t := range w.transactions {
				t.Error = ErrNotConnected
				t.finish()
			}
			w.transactions = w.transactions[:0]
			break l
		}
	}
        ... ...
}

The transaction poped in chan w.responseChan branch means successfully sent, so transaction's error reason is empty and should not be set.
The transaction poped in chan w.errorChan branch means sent failed, should be send again, and transaction's error reason should be set to the real reason, so user can know what happened.

case data := <-w.errorChan:
w.popTransaction(FrameTypeError, data)
default:
// clean up transactions we can easily account for
for _, t := range w.transactions {
t.Error = ErrNotConnected
t.finish()
}
w.transactions = w.transactions[:0]
break l
}
}
w.transactions = w.transactions[:0]

// spin and free up any writes that might have raced
// with the cleanup process (blocked on writing
Expand Down