public class ThrottlingFilter extends Object implements javax.servlet.Filter, ThrottlingMXBean
Retry-After HTTP header giving a backoff
time in seconds.
Further more, if a client does not back off when being sent HTTP 429, and it
looks abusive, it is completely banned for a period of time, and will
receive HTTP 403 (Forbidden) during the duration of the ban.
The clients are segmented in different buckets and resource consumption is
tracked individually for each of those buckets. The segmentation is done by
[IP address, User Agent], but could be extended to support more complex
strategies. A bucket is only kept while its client is active. After a period
of inactivity, the bucket is deleted.
All state is limited to a single JVM, this filter is not cluster aware.| Constructor and Description |
|---|
ThrottlingFilter() |
| Modifier and Type | Method and Description |
|---|---|
static Callable<ThrottlingState> |
createThrottlingState(Duration timeBucketCapacity,
Duration timeBucketRefillAmount,
Duration timeBucketRefillPeriod,
int errorBucketCapacity,
int errorBucketRefillAmount,
Duration errorBucketRefillPeriod,
int throttleBucketCapacity,
int throttleBucketRefillAmount,
Duration throttleBucketRefillPeriod,
Duration banDuration)
Create Callable to initialize throttling state.
|
void |
destroy()
Unregister MBean.
|
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
Check resource consumption and throttle requests as needed.
|
long |
getNumberOfBannedRequests()
Monotonic increasing counter of the the number of requests that have been banned.
|
long |
getNumberOfThrottledRequests()
Monotonic increasing counter of the the number of requests that have been throttled.
|
long |
getStateSize()
The number of entries in the state store.
|
void |
init(javax.servlet.FilterConfig filterConfig)
Initialise the filter.
|
public void init(javax.servlet.FilterConfig filterConfig)
TokenBucket for the details on bucket
configuration, see implementation for the default values):
request-duration-threshold-in-millis: requests longer
than this threshold will start the tracking for this usertime-bucket-capacity-in-seconds,
time-bucket-refill-amount-in-seconds,
time-bucket-refill-period-in-minutes: configuration of the
bucket tracking request durationserror-bucket-capacity,
error-bucket-refill-amount,
error-bucket-refill-period-in-minutes: configuration of the
bucket tracking errorsthrottle-bucket-capacity,
throttle-bucket-refill-amount,
throttle-bucket-refill-period-in-minutes: configuration of
the bucket tracking throttlingban-duration-in-minutes: how long should a user be
banned when a ban is triggeredmax-state-size: how many users to trackstate-expiration-in-minutes: tracking of a user expires
after this durationenable-throttling-if-header: enable the throttling on
the requests which have this header setenable-ban-if-header: enable the banning on the requests
which have this header setalways-throttle-param: always throttle requests where
this parameter is set (useful for testing)always-ban-param: always ban requests where this
parameter is set (useful for testing)enabled: entirely disable this filter if set to
falseThrottlingFilterConfig.loadStringParam(String, FilterConfig) for
the details of where the configuration is loaded from.init in interface javax.servlet.FilterfilterConfig - public static Callable<ThrottlingState> createThrottlingState(Duration timeBucketCapacity, Duration timeBucketRefillAmount, Duration timeBucketRefillPeriod, int errorBucketCapacity, int errorBucketRefillAmount, Duration errorBucketRefillPeriod, int throttleBucketCapacity, int throttleBucketRefillAmount, Duration throttleBucketRefillPeriod, Duration banDuration)
public void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
doFilter in interface javax.servlet.Filterrequest - response - chain - IOExceptionjavax.servlet.ServletExceptionpublic void destroy()
destroy in interface javax.servlet.Filterpublic long getStateSize()
ThrottlingMXBeangetStateSize in interface ThrottlingMXBeanpublic long getNumberOfThrottledRequests()
ThrottlingMXBeangetNumberOfThrottledRequests in interface ThrottlingMXBeanpublic long getNumberOfBannedRequests()
ThrottlingMXBeangetNumberOfBannedRequests in interface ThrottlingMXBeanCopyright © 2019. All rights reserved.