Class ThrottlingFilter

  • All Implemented Interfaces:
    Filter

    public class ThrottlingFilter
    extends Object
    implements Filter
    This filter applies a rate limitation to incoming requests : over the limit requests will be rejected with a 429 (Too Many Requests) response, others will pass through. The rate limiting is implemented as a token bucket strategy that gives us the ability to handle rate limits through a sliding window. Multiple rates can be supported in parallel with the support of a partition key (we first try to find the bucket to use for each incoming request, then we apply the rate limit). Note that if no rate definition is found, this filter let the request goes through.
    • Constructor Detail

      • ThrottlingFilter

        public ThrottlingFilter​(AsyncFunction<ContextAndRequest,​String,​Exception> requestGroupingPolicy,
                                ThrottlingPolicy throttlingRatePolicy,
                                ThrottlingStrategy throttlingStrategy)
        Constructs a ThrottlingFilter.
        Parameters:
        requestGroupingPolicy - the key used to identify the token bucket (must not be null).
        throttlingRatePolicy - the datasource where to lookup for the rate to apply (must not be null).
        throttlingStrategy - the throttling strategy to apply.
    • Method Detail

      • stop

        public void stop()
        Stops this filter and frees the resources.
      • filter

        public Promise<Response,​NeverThrowsException> filter​(Context context,
                                                                   Request request,
                                                                   Handler next)
        Description copied from interface: Filter
        Filters the request and/or response of an exchange. To pass the request to the next filter or handler in the chain, the filter calls next.handle(context, request).

        This method may elect not to pass the request to the next filter or handler, and instead handle the request itself. It can achieve this by merely avoiding a call to next.handle(context, request) and creating its own response object. The filter is also at liberty to replace a response with another of its own by intercepting the response returned by the next handler.

        Specified by:
        filter in interface Filter
        Parameters:
        context - The request context.
        request - The request.
        next - The next filter or handler in the chain to handle the request.
        Returns:
        A Promise representing the response to be returned to the client.