-
Notifications
You must be signed in to change notification settings - Fork 586
Add automatic publisher confirmation tracking with throttling #1824
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
base: main
Are you sure you want to change the base?
Add automatic publisher confirmation tracking with throttling #1824
Conversation
|
Hey @acogoluegnes! Here's a proposal for implementing "automatic" publisher confirmation tracking in a similar manner to the .NET client. As you're the expert here, please suggest better names, a better implementation, etc, as I'm just barely familiar enough with Java and this project to implement this feature with the help of a genie. I did, of course, review the code. If you like the way this is going, I thought I'd also add rate-throttling in a similar manner to the .NET client, i.e. as the outstanding confirmation window closes, increase delay between publishes to (hopefully) allow the broker to catch up. |
6a31a46 to
714370e
Compare
|
Thanks for this contributon @lukebakken. I think it is on the right track. I added some comments in the code. Just a few more remarks:
|
714370e to
44c3aca
Compare
f785dc5 to
c17cd9f
Compare
…text Traditional publisher confirms in the Java client require manual tracking of sequence numbers and correlation of Basic.Return messages. This makes per-message error handling complex and provides no built-in async pattern, backpressure mechanism, or message correlation support. This change introduces automatic publisher confirmation tracking with a `CompletableFuture`-based API, progressive throttling, and generic context parameter for message correlation, following the design of the .NET client's publisher confirmation implementation. When enabled via `ChannelOptions`, the library automatically tracks each message's confirmation status and completes futures when the broker sends Basic.Ack, Basic.Nack, or Basic.Return. New API methods: - `Channel.basicPublishAsync()` - Returns `CompletableFuture<T>` that completes with user-provided context when broker confirms the message. The generic context parameter (such as a correlation ID) eliminates the need for separate tracking structures. - `Connection.createChannel(ChannelOptions)` - Creates channel with publisher confirmation tracking enabled New classes: - `ChannelOptions` - Configuration with builder pattern for channel creation with tracking settings and optional `RateLimiter` for backpressure control - `PublishException` - Exception thrown when message is nack'd or returned, with full details including sequence number, routing information, and user-provided context - `RateLimiter` - Interface for custom rate limiting strategies - `ThrottlingRateLimiter` - Progressive throttling implementation that applies delays proportional to capacity usage (0-1000ms) when available permits fall below a configurable threshold (default 50%) The implementation adds a sequence number header (`x-seq-no`) to each tracked message, allowing correlation of Basic.Return responses with specific messages. The `ThrottlingRateLimiter` uses `Semaphore` for concurrency control and calculates delay as `percentageUsed * 1000ms`, matching the .NET client's algorithm exactly. Passing `null` for the rate limiter disables backpressure for unlimited outstanding confirmations. Publisher confirmation state is stored in a single `ConcurrentHashMap` containing `ConfirmationEntry` objects that hold the future, rate limiter permit, and user context. The map is sized based on the rate limiter's capacity hint for optimal memory usage. The feature is opt-in and maintains full backward compatibility. Existing `basicPublish()` and `waitForConfirms()` methods remain unchanged. Tests include 9 unit tests for `ThrottlingRateLimiter` and 25 integration tests for publisher confirmation tracking with context parameter verification and various rate limiting scenarios.
c17cd9f to
c64f7ba
Compare
Traditional publisher confirms in the Java client require manual
tracking of sequence numbers and correlation of Basic.Return messages.
This makes per-message error handling complex and provides no built-in
async pattern, backpressure mechanism, or message correlation support.
This change introduces automatic publisher confirmation tracking with a
CompletableFuture-based API, progressive throttling, and genericcontext parameter for message correlation, following the design of the
.NET client's publisher confirmation implementation. When enabled via
ChannelOptions, the library automatically tracks each message'sconfirmation status and completes futures when the broker sends
Basic.Ack, Basic.Nack, or Basic.Return.
New API methods:
Channel.basicPublishAsync()- ReturnsCompletableFuture<T>thatcompletes with user-provided context when broker confirms the message.
The generic context parameter (such as a correlation ID) eliminates
the need for separate tracking structures.
Connection.createChannel(ChannelOptions)- Creates channel withpublisher confirmation tracking enabled
New classes:
ChannelOptions- Configuration with builder pattern for channelcreation with tracking settings and optional
RateLimiterforbackpressure control
PublishException- Exception thrown when message is nack'd orreturned, with full details including sequence number, routing
information, and user-provided context
RateLimiter- Interface for custom rate limiting strategiesThrottlingRateLimiter- Progressive throttling implementation thatapplies delays proportional to capacity usage (0-1000ms) when
available permits fall below a configurable threshold (default 50%)
The implementation adds a sequence number header (
x-seq-no) to eachtracked message, allowing correlation of Basic.Return responses with
specific messages. The
ThrottlingRateLimiterusesSemaphoreforconcurrency control and calculates delay as
percentageUsed * 1000ms,matching the .NET client's algorithm exactly. Passing
nullfor therate limiter disables backpressure for unlimited outstanding
confirmations.
Publisher confirmation state is stored in a single
ConcurrentHashMapcontaining
ConfirmationEntryobjects that hold the future, ratelimiter permit, and user context. The map is sized based on the rate
limiter's capacity hint for optimal memory usage.
The feature is opt-in and maintains full backward compatibility.
Existing
basicPublish()andwaitForConfirms()methods remainunchanged. Tests include 9 unit tests for
ThrottlingRateLimiterand 25integration tests for publisher confirmation tracking with context
parameter verification and various rate limiting scenarios.