Package fr.becpg.repo.entity.remote
Class RemoteRateLimiter
java.lang.Object
fr.becpg.repo.entity.remote.RemoteRateLimiter
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
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longA bucket untouched for this long is forgotten.private final Map<String, RemoteRateLimiter.Bucket> private final RemoteRateLimiter.Bucketprivate static final longOne log line per client and per window: a throttled client retries, and would flood the log.private static final org.apache.commons.logging.Logprivate static final intBeyond this many known callers, the idle ones are dropped rather than kept for ever.private SystemConfigurationServiceprivate static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanallowRequest.private RemoteRateLimiter.BucketclientBucket(String client) private Integerprivate Doubleprotected StringThe 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.private voidprivate voidlogRejection(String client, String which) private IntegerremoteRateLimiterCapacity.private DoubleremoteRateLimiterRefillRate.voidsetSystemConfigurationService(SystemConfigurationService systemConfigurationService) Setter for the fieldsystemConfigurationService.
-
Field Details
-
logger
private static final org.apache.commons.logging.Log logger -
MAX_TRACKED_CLIENTS
private static final int MAX_TRACKED_CLIENTSBeyond 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_MILLISA bucket untouched for this long is forgotten.- See Also:
-
LOG_THROTTLE_MILLIS
private static final long LOG_THROTTLE_MILLISOne log line per client and per window: a throttled client retries, and would flood the log.- See Also:
-
UNAUTHENTICATED
- See Also:
-
globalBucket
-
clientBuckets
-
systemConfigurationService
-
-
Constructor Details
-
RemoteRateLimiter
public RemoteRateLimiter()
-
-
Method Details
-
setSystemConfigurationService
Setter for the field
systemConfigurationService.- Parameters:
systemConfigurationService- aSystemConfigurationServiceobject
-
remoteRateLimiterCapacity
remoteRateLimiterCapacity.
- Returns:
- a
Integerobject
-
remoteRateLimiterRefillRate
remoteRateLimiterRefillRate.
- Returns:
- a
Doubleobject
-
clientCapacity
-
clientRefillRate
-
allowRequest
public boolean allowRequest()allowRequest.
- Returns:
- a boolean
-
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
Stringobject
-
clientBucket
-
evictIdleClientsIfCrowded
private void evictIdleClientsIfCrowded() -
logRejection
-