Class RemoteRateLimiter

java.lang.Object
fr.becpg.repo.entity.remote.RemoteRateLimiter

@Service("remoteRateLimiter") public class RemoteRateLimiter extends Object

Token bucket guarding the beCPG remote API.

Each caller gets its own bucket, so a single chatty client can no longer starve the connectors of every other client. A global bucket is kept on top as a backstop on the total load reaching the repository, with the same settings as before, so a lone caller behaves exactly as it used to.

Author:
Matthieu
  • Field Details

    • logger

      private static final org.apache.commons.logging.Log logger
    • MAX_TRACKED_CLIENTS

      private static final int MAX_TRACKED_CLIENTS
      Beyond this many known callers, the idle ones are dropped rather than kept for ever.
      See Also:
    • CLIENT_IDLE_MILLIS

      private static final long CLIENT_IDLE_MILLIS
      A bucket untouched for this long is forgotten.
      See Also:
    • LOG_THROTTLE_MILLIS

      private static final long LOG_THROTTLE_MILLIS
      One log line per client and per window: a throttled client retries, and would flood the log.
      See Also:
    • UNAUTHENTICATED

      private static final String UNAUTHENTICATED
      See Also:
    • globalBucket

      private final RemoteRateLimiter.Bucket globalBucket
    • clientBuckets

      private final Map<String,RemoteRateLimiter.Bucket> clientBuckets
    • systemConfigurationService

      @Autowired private SystemConfigurationService systemConfigurationService
  • Constructor Details

    • RemoteRateLimiter

      public RemoteRateLimiter()
  • Method Details

    • setSystemConfigurationService

      public void setSystemConfigurationService(SystemConfigurationService systemConfigurationService)

      Setter for the field systemConfigurationService.

      Parameters:
      systemConfigurationService - a SystemConfigurationService object
    • remoteRateLimiterCapacity

      private Integer remoteRateLimiterCapacity()

      remoteRateLimiterCapacity.

      Returns:
      a Integer object
    • remoteRateLimiterRefillRate

      private Double remoteRateLimiterRefillRate()

      remoteRateLimiterRefillRate.

      Returns:
      a Double object
    • clientCapacity

      private Integer clientCapacity()
    • clientRefillRate

      private Double clientRefillRate()
    • allowRequest

      public boolean allowRequest()

      allowRequest.

      Returns:
      a boolean
    • currentClient

      protected String currentClient()

      The authenticated user is the caller identity here: a connector authenticates with its own account, so it is what tells apart the client to talk to when the limit is reached.

      Returns:
      a String object
    • clientBucket

      private RemoteRateLimiter.Bucket clientBucket(String client)
    • evictIdleClientsIfCrowded

      private void evictIdleClientsIfCrowded()
    • logRejection

      private void logRejection(String client, String which)