Class ClientRequestDeduplicator

java.lang.Object
com.loomcache.client.ClientRequestDeduplicator
All Implemented Interfaces:
AutoCloseable

public class ClientRequestDeduplicator extends Object implements 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.

  • 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

      public ClientRequestDeduplicator(Duration requestTimeout)
      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

      public 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.
      Parameters:
      requestTimeout - request timeout used to derive stale request eviction (2x request timeout)
      maxInFlightRequests - maximum unique in-flight reads tracked for coalescing
    • ClientRequestDeduplicator

      public ClientRequestDeduplicator(Duration staleRequestTimeout, Duration cleanupInterval)
      Creates a new ClientRequestDeduplicator with explicit stale-request sweep configuration.
      Parameters:
      staleRequestTimeout - maximum age for tracked requests before forced cleanup
      cleanupInterval - 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 cleanup
      cleanupInterval - how often the stale-request sweep runs
      maxInFlightRequests - maximum unique in-flight reads tracked for coalescing
  • Method Details

    • generateRequestId

      public String 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 ID
      operation - the operation type (e.g., "MAP_GET", "MAP_PUT")
      mapName - the map/structure name
      key - the key being accessed
      future - the CompletableFuture for this request
      Returns:
      the existing future if a duplicate is in-flight, null if this is a new request
    • untrackRequest

      public void untrackRequest(String requestId)
      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:
      close in interface AutoCloseable