Class ClientRequestDeduplicator
java.lang.Object
com.loomcache.client.ClientRequestDeduplicator
- All Implemented Interfaces:
AutoCloseable
Client-side request deduplicator that prevents duplicate sends and coalesces
identical in-flight requests.
Features: - Assigns unique request IDs (UUID-based) to outgoing requests - Tracks in-flight requests to prevent duplicate sends - Coalesces identical requests: if same operation is already in-flight, returns the same CompletableFuture instead of sending another request
Request coalescing key = operation type + map name + key. This prevents duplicate network traffic for concurrent identical operations.
Thread-safe: uses ReentrantLock for coordinated operations and ConcurrentHashMap for the underlying tracking.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecord representing a request coalescing key.static final recordStatistics for request deduplication.static final recordRecord representing an in-flight request. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new ClientRequestDeduplicator.ClientRequestDeduplicator(Duration requestTimeout) Creates a new ClientRequestDeduplicator with a stale-request timeout derived from the request timeout.ClientRequestDeduplicator(Duration requestTimeout, int maxInFlightRequests) Creates a new ClientRequestDeduplicator with a stale-request timeout derived from the request timeout and an explicit in-flight request limit.ClientRequestDeduplicator(Duration staleRequestTimeout, Duration cleanupInterval) Creates a new ClientRequestDeduplicator with explicit stale-request sweep configuration.ClientRequestDeduplicator(Duration staleRequestTimeout, Duration cleanupInterval, int maxInFlightRequests) Creates a new ClientRequestDeduplicator with explicit stale-request sweep and capacity configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all tracked requests (useful for testing/cleanup).voidclose()Generates a unique request ID.intReturns the number of currently in-flight requests.getStats()Returns current statistics about request deduplication.@Nullable CompletableFuture<?> trackRequest(String requestId, String operation, String mapName, String key, CompletableFuture<?> future) Registers an in-flight request for tracking and coalescing.voiduntrackRequest(String requestId) Unregisters a completed request from tracking.
-
Field Details
-
DEFAULT_MAX_IN_FLIGHT_REQUESTS
public static final int DEFAULT_MAX_IN_FLIGHT_REQUESTS- See Also:
-
-
Constructor Details
-
ClientRequestDeduplicator
public ClientRequestDeduplicator()Creates a new ClientRequestDeduplicator. -
ClientRequestDeduplicator
Creates a new ClientRequestDeduplicator with a stale-request timeout derived from the request timeout.- Parameters:
requestTimeout- request timeout used to derive stale request eviction (2x request timeout)
-
ClientRequestDeduplicator
Creates a new ClientRequestDeduplicator with a stale-request timeout derived from the request timeout and an explicit in-flight request limit.- Parameters:
requestTimeout- request timeout used to derive stale request eviction (2x request timeout)maxInFlightRequests- maximum unique in-flight reads tracked for coalescing
-
ClientRequestDeduplicator
Creates a new ClientRequestDeduplicator with explicit stale-request sweep configuration.- Parameters:
staleRequestTimeout- maximum age for tracked requests before forced cleanupcleanupInterval- how often the stale-request sweep runs
-
ClientRequestDeduplicator
public ClientRequestDeduplicator(Duration staleRequestTimeout, Duration cleanupInterval, int maxInFlightRequests) Creates a new ClientRequestDeduplicator with explicit stale-request sweep and capacity configuration.- Parameters:
staleRequestTimeout- maximum age for tracked requests before forced cleanupcleanupInterval- how often the stale-request sweep runsmaxInFlightRequests- maximum unique in-flight reads tracked for coalescing
-
-
Method Details
-
generateRequestId
Generates a unique request ID.- Returns:
- a new UUID-based request ID
-
trackRequest
public @Nullable CompletableFuture<?> trackRequest(String requestId, String operation, String mapName, String key, CompletableFuture<?> future) Registers an in-flight request for tracking and coalescing. If an identical request is already in-flight, returns the existing future. Otherwise, returns null and the request should be sent.- Parameters:
requestId- the unique request IDoperation- the operation type (e.g., "MAP_GET", "MAP_PUT")mapName- the map/structure namekey- the key being accessedfuture- the CompletableFuture for this request- Returns:
- the existing future if a duplicate is in-flight, null if this is a new request
-
untrackRequest
Unregisters a completed request from tracking.- Parameters:
requestId- the request ID to unregister
-
getStats
Returns current statistics about request deduplication. -
clear
public void clear()Clears all tracked requests (useful for testing/cleanup). -
getInFlightCount
public int getInFlightCount()Returns the number of currently in-flight requests. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-