We are a cloud based file storage company and we allow many access point to the cloud. One of the access point is Webdav api and people can use any webdav client to access the cloud. But some of the webdav client especially on mac OS are really abusive. Though the user is not doing any abusive action, the webdav client does aggressive caching so even when you are navigating at a top directory it does PROPFINDs for depth at 5 or 6 level to make the user experience seamless as if he is navigating local drive. This makes life miserable on the server because from some clients we get more than 1000 requests in a minute. If there are 5-10 clients do the webdav activity then it causes 100 or more propfinds per sec. Luckily the server is able to process these but it hurts other activities. So we needed to rate limit this. Now as the user is really not doing any abusive action it would be bad to slow down on penalize the user in normal circumstance, however if the server is under load then it would be worth while to throttle the user for some time.
Luckily Jdk1.6 has a light weight api to get system load average.
And now I can write code like
The logic I used for propfind rate limiting is simple where I keep track of all users who made>1000 requests in last 5 min and only if loadAvg is >4, I send 503. For brevity I am not putting the code to count no of request made by users in last 5 min but I am using memcache to maintain the counters in one minute buckets.
Luckily Jdk1.6 has a light weight api to get system load average.
public double getSystemLoadAverage() {
OperatingSystemMXBean osStats = ManagementFactory.getOperatingSystemMXBean();
double loadAverage = osStats.getSystemLoadAverage();
return loadAverage;
}
And now I can write code like
if(throttlePropFinds(req, user)) {
logger.info("Throttling due to excessive webdav requests from user {}", user.getId());
if(throttlingHelper.isBlockUsers()) {
double loadAverage = SpringBeanLocator.getJvmStatsLogger().getSystemLoadAverage();
if (loadAverage > 4) {
logger.info("Sending 503 as loadAverage>4 and excessive webdav requests from user {}", user.getId());
resp.sendError(WebdavStatus.SC_SERVICE_UNAVAILABLE);
return;
}
}
}
The logic I used for propfind rate limiting is simple where I keep track of all users who made>1000 requests in last 5 min and only if loadAvg is >4, I send 503. For brevity I am not putting the code to count no of request made by users in last 5 min but I am using memcache to maintain the counters in one minute buckets.
Comments
Post a Comment