Class RequestRateThrottleFilter

  • All Implemented Interfaces:
    javax.servlet.Filter

    public final class RequestRateThrottleFilter
    extends Object
    implements javax.servlet.Filter
    The RequestRateThrottleFilter is a Servlet filter that can place a hard limit on the number of requests per second. The filter conforms to RFC-6585 by sending HTTP status code 429 (too many requests) if the limit is exceeded. Sample usage:
     <filter>
       <filter-name>RequestRateThrottleFilter</filter-name>
       <filter-class>alpine.filters.RequestRateThrottleFilter</filter-class>
       <init-param>
         <param-name>maximumRequestsPerPeriod</param-name>
         <param-value>5</param-value>
       </init-param>
       <init-param>
         <param-name>timePeriodSeconds</param-name>
         <param-value>10</param-value>
       </init-param>
     </filter>
    
     <!--  Place a request limit on all resources -->
     <filter-mapping>
       <filter-name>RequestRateThrottleFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>
     
    Since:
    1.0.0
    Author:
    Steve Springett
    • Constructor Detail

      • RequestRateThrottleFilter

        public RequestRateThrottleFilter()
    • Method Detail

      • init

        public void init​(javax.servlet.FilterConfig filterConfig)
        Specified by:
        init in interface javax.servlet.Filter
      • doFilter

        public void doFilter​(javax.servlet.ServletRequest request,
                             javax.servlet.ServletResponse response,
                             javax.servlet.FilterChain chain)
                      throws IOException,
                             javax.servlet.ServletException
        Determines if the request rate is below or has exceeded the the maximum requests per second for the given time period. If exceeded, a HTTP status code of 429 (too many requests) will be send and no further processing of the request will be done. If the request has not exceeded the limit, the request will continue on as normal.
        Specified by:
        doFilter in interface javax.servlet.Filter
        Parameters:
        request - a ServletRequest
        response - a ServletResponse
        chain - a FilterChain
        Throws:
        IOException - a IOException
        javax.servlet.ServletException - a ServletException
      • destroy

        public void destroy()
        Specified by:
        destroy in interface javax.servlet.Filter