Class HttpClientProvider

java.lang.Object
se.liu.ida.hefquin.base.net.http.HttpClientProvider

public class HttpClientProvider extends Object
Provides shared HttpClient instances.

Each HttpClient instance manages its own internal connection pool. A single client handles requests to many different hosts while reusing connections where possible.

The connectTimeout is a client-level configuration and cannot be changed after a client has been created. To support different connection-timeout values, this provider caches one client instance per timeout. Each such client manages its own independent set of pooled connections for all routes it is used with.

Returned HttpClient instances are backed by an internal HttpClientProvider.LimitedHttpClient wrapper to enforce concurrency limits using Semaphores. Applicable concurrency limits are applied based on hierarchical lookup by trimming path segments from the request URI.

  • Field Details

    • LIMITERS_BY_ENDPOINT_ADDRESS

      protected static final Map<String,Semaphore> LIMITERS_BY_ENDPOINT_ADDRESS
      One limiter per endpoint address. The limiter for an endpoint address is shared across all clients.
    • CLIENT_BY_CONNECT_TIMEOUT

      protected static final Map<Long,HttpClient> CLIENT_BY_CONNECT_TIMEOUT
  • Constructor Details

    • HttpClientProvider

      public HttpClientProvider()
  • Method Details

    • client

      public static HttpClient client()
      Returns a shared client with no configured connect timeout.
      Returns:
      a shared client instance
    • client

      public static HttpClient client(long connectTimeout)
      Returns a shared client for a given connect timeout. A separate client instance is cached for each effective timeout value, where non-positive values are treated as "no connect timeout".
      Parameters:
      connectTimeout - connect timeout in milliseconds, or non-positive value for no timeout
      Returns:
      shared client instance for the given timeout configuration
    • setDefaultParallelRequestLimitPerServer

      public static void setDefaultParallelRequestLimitPerServer(int limit)
      Sets the default limit on the number of concurrent requests per server. This value is used when creating new endpoint limiters via
      invalid reference
      #getOrCreateEndpointLimiter(String)
      .
      Parameters:
      limit - maximum number of concurrent requests per server
      Throws:
      IllegalArgumentException - if limit is non-positive
    • registerEndpointLimiter

      public static void registerEndpointLimiter(String endpointAddress, int limit)
      Registers a concurrency limiter for the given endpoint address. If a limiter is already registered for the endpoint address, it is replaced.
      Parameters:
      endpointAddress - endpoint address
      limit - limit on the number of concurrent requests allowed for the endpoint address
      Throws:
      IllegalArgumentException - if limit is non-positive.
    • normalizeEndpointAddress

      public static String normalizeEndpointAddress(String endpointAddress)
      Normalizes an endpoint address by removing a trailing slash, if present.
      Parameters:
      endpointAddress - the endpoint address to normalize
      Returns:
      the normalized endpoint address
    • reset

      public static void reset()
      Resets provider state to its default configuration.

      This clears all cached clients and registered endpoint address limiters, and restores the default maximum number of parallel requests.

      This method is primarily intended for test isolation.