public class ConstantWaitRateLimiter extends Object implements RateLimiter
A very basic implementation of RateLimiter that will reduce the
throughput of requests by pausing for the minimum period required to ensure the cap is not exceeded. By default this
is 300 ms. Each calling thread is blocked until the request may be sent, so this is not suitable for use as a queue.
If there is more than one server making requests to the same Brightpearl account, this approach will only be effective if this class is configured with a 1/n share of the request cap, which increases latency for all requests, however a distributed tracking approach may be more appropriate.
This class is intended to prevent hitting the request cap for an account, and its behaviour is not affected by an account unexpectedly being throttled.
| Constructor and Description |
|---|
ConstantWaitRateLimiter()
Construct a constant wait rate limiter with the default settings, limiting requests to 200 per minute.
|
ConstantWaitRateLimiter(int cap,
int period,
TimeUnit periodUnit)
Construct a constant wait limiter with a custom rate limit.
|
| Modifier and Type | Method and Description |
|---|---|
void |
rateLimit(Account account)
Called before a request is made, this sleeps for the minimum amount of time that must be left to ensure no more
than the maximum number of requests are made in the configured period.
|
void |
requestCapExceeded(Account account)
This implementation does nothing.
|
void |
requestCompleted(Account account,
int requestsRemaining,
long nextThrottlePeriod)
This implementation does nothing.
|
public ConstantWaitRateLimiter()
public ConstantWaitRateLimiter(int cap,
int period,
TimeUnit periodUnit)
cap - The number of requests allowed in the given period.period - The period of time.periodUnit - Unit of the period of time.public void rateLimit(Account account)
rateLimit in interface RateLimiteraccount - The account a request is about to be sent to.public void requestCompleted(Account account, int requestsRemaining, long nextThrottlePeriod)
requestCompleted in interface RateLimiteraccount - The account a response has been received from.requestsRemaining - The value of the brightpearl-requests-remaining response header.nextThrottlePeriod - The value of the brightpearl-next-throttle-period header.public void requestCapExceeded(Account account)
requestCapExceeded in interface RateLimiteraccount - The account that received a 503 request cap error.Copyright © 2014. All Rights Reserved.