I wanted to add a way to throttle the number of requests coming on each API from a certain client. So, I wanted to basically limit the number of requests per API per client.
I am using DropWizard as framework. Can somebody recommend the ways to achieve this? I need something that will work for Distributed system.
A simplistic approach would be to use a Filter and wrap it around all your API calls in web.xml
. Assuming your clients send an API keys identifying them in a HTTP header, you could implement a filter like this:
public class MyThrottlingFilter extends Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpreq = (HttpServletRequest) req;
String apiKey = httpreq.getHeader("API_KEY")
if (invocationLimitNotReached(apiKey))
chain.doFilter(req, res);
else
throw ...
}
}
and then register it like this:
<filter>
<filter-name>MyThrottlingFilter</filter-name>
<filter-class>com.my.throttler.MyThrottlingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyThrottlingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Of course, identifying your clients may be more difficult than this, if you use some other authentication methods, but the general idea should be the same.