Repository navigation
fix: SendAsync doesn't respect context and can't timeout during reconnection - #1422
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes issue #1332 where SendAsync doesn't respect context and can't timeout during producer reconnection. The root cause was that the producer's main event loop became blocked during reconnection, preventing it from processing new send requests and bypassing timeout checks.
- Separates reconnection logic into a dedicated goroutine to prevent blocking the main producer data loop
- Introduces a new
producerConnectingstate to manage message flow during reconnection - Ensures
SendAsyncrespects the provided context for cancellation and timeout
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| pulsar/producer.go | Updates SendAsync documentation to clarify context usage vs SendTimeout |
| pulsar/producer_partition.go | Refactors reconnection logic, adds producerConnecting state, and implements context handling |
| pulsar/producer_test.go | Adds comprehensive tests for timeout and context cancellation scenarios |
| pulsar/internal/blocking_queue.go | Adds Iterate method for efficient queue traversal |
| pulsar/internal/blocking_queue_test.go | Tests for the new Iterate functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Fixes #1332
Motivation
The root cause of issue #1332 is that the producer's main event loop becomes blocked during the reconnection process. This prevents it from processing new send requests from the
dataChan.Consequently, messages waiting in
dataChancannot be moved to the pending message queue, causing them to bypass theSendTimeoutcheck. Furthermore, thecontextpassed toSendAsyncis not respected when the loop is blocked.This behavior was introduced after PR #1029, which merged reconnection logic and data handling logic into a single event loop. The root cause of the issue in #1027 is that during producer reconnection, resending messages and sending new messages occur at the same time. This causes the messages to be sent out of order, which leads to the "received an ack larger than expected" error.
Modifications
This PR refactors the producer's reconnection logic to solve the blocking issue while also preventing the original message ordering problem.
The key changes are:
Separate Reconnection and Data Handling Logic: The reconnection process has been moved into its own dedicated goroutine (event loop). This ensures the main producer data loop remains unblocked and can continuously process send requests, even when a reconnection is in progress.
Introduce
producerConnectingState: A new state,producerConnecting, is introduced to manage the producer's behavior during reconnection. When in this state, new messages are added to the pending queue but are not immediately sent. This prevents new messages from being sent concurrently with resent messages from the previous connection, thus fixing the root cause of the out-of-order issue from Continuous reconnection failures lead to message send timeout #1027.Respect
SendAsyncContext: The ctx passed to SendAsync is now respected.New Reconnection Flow
The new logic creates a clear separation of concerns between the two event loops:
producerConnecting. Start reconnectingproducerReadyDocumentation Updates
The documentation for
SendAsynchas been updated to clarify the distinct roles of thecontextand theSendTimeoutconfiguration:Verifying this change
Added a new unit test,
TestSendAsyncCouldTimeoutWhileReconnecting(based on the test from PR #1345), to verify that message timeouts and context cancellations function correctly during a producer reconnection cycle. And also other unit tests to cover changes.Does this pull request potentially affect one of the following parts:
If
yeswas chosen, please highlight the changesDocumentation