
These Algo. typically used as Rate limiting Algorithm.
It is used to control amount of data or number of requests that can be proccessed in a given time period.
WORKING :
- Token are added to the Bucket at a fixed rate.
- The Bucket has a Max. Capacity, it can't hold more tokens than that.
- When a Request came, the Bucket is checked for number of token available
- If req. tokens are available then remove that tokens from the bucket and process the request.
- If no token are available, the request is rejected or pushed to a retry queue.
IMPLEMENTATION :
Token Bucket
can be implemented in respect to user or application.
Means either we can use it rate limit the user, that a certain user can only make 10req/sec.
or we can use it to rate limit the application, that a certain application can only make 10req/sec.
classTokenBucket{privaterefillRate:number;// no of token added to the bucket per secondprivatecapacity:number;privateBucket:number;privatelastRefillTime:number;constructor(refillRate:number,capacity:number){this.refillRate=refillRate;this.capacity=capacity;this.Bucket=capacity;this.lastRefillTime=Date.now();}publicrefill(){consttime=Date.now();constdiff=time-this.lastRefillTime;consttokensToAdd=Math.floor(diff*this.refillRate);this.Bucket=min(this.capacity,this.Bucket+tokensToAdd);this.lastRefillTime=time;}publicallowRequest(){// Update the Bucket with tokensrefill();if(this.Bucket>=1){this.Bucket=this.Bucket-1;returntrue;}returnfalse;}}
Example : LIMITING REQUESTS PER SECOND FOR USER IN NESTJS
FLOW
Client──>NestJSAPI(Producer)└──alwayssendtoRabbitMQ➤(freeorpremiumqueue)Worker(Consumer)└──Pullfromqueue(s)└──CheckRedistokenbucket├──✅Allowed→processjob└──⛔Notallowed→requeuewithdelay
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse