Syntax highlighter header

Friday, 13 August 2021

Implementing rate limit on an API

Here I am going to discuss an interesting programming problem. How to implement rate limit for an API per customer. You need to implement a rate limit of 10 requests per second with allowing a bust of 50 API calls. It means if user was silent for some time then tokens will accumulate up to 50 tokens and after that they will get expired. Every call will consume one token. One token will get accumulated every 100 milliseconds. 

Following is the implementation of code with limit of 10 API calls per second and allowing a bust of 50 API calls.


import java.util.HashMap;

public class RateLimitting {
	
	HashMap<String, Bucket> rateLimits = new HashMap<>();
	
	public static class Bucket {
		int tokens;
		long lastAccessTime;
		
		public Bucket() {
			tokens = 50;
			lastAccessTime = System.currentTimeMillis();
		}
	}
	
	public String api(String user, String req) {
		Bucket r = rateLimits.get(user);
		if(r==null) {
			r = new Bucket();
			rateLimits.put(user, r);
		} else {
			long current = System.currentTimeMillis();
			if((current - r.lastAccessTime)/100 >=1) {
				r.tokens = (int) (r.tokens + (current - r.lastAccessTime)/100);
				if(r.tokens>50) {
					r.tokens = 50;
				}
				r.lastAccessTime = current;
			}
		}
		
		if(r.tokens<0) {
			// deny the request
			return null;
		}
		
		r.tokens--;
		
		// Process the request
		return "Success";
		
	}
} 

No comments:

Post a Comment