Facebook
From Maruf Ahmed, 1 Month ago, written in Java.
Embed
Download Paste or View Raw
Hits: 104
  1. /**
  2.  *
  3.  * @author Maruf
  4.  */
  5. import java.time.Instant ;
  6. import java.time.Duration ;
  7. import java.time.temporal.ChronoUnit;
  8.  
  9. public class RateLimitedCalculator {
  10.        
  11.     private final int callLimit ; // Request Limit will be initialized by the constructor
  12.     private int callCount  ; // Current call count
  13.     private final Duration resetTime ; // Amount of time after the Limiter will be reset
  14.     private Instant lastReset;
  15.     private final long resetTimeUserDefinedMinutes = 1 ;
  16.     public RateLimitedCalculator(int intendedLimit)
  17.             {
  18.                    this.callLimit = intendedLimit ;
  19.                    this.callCount = 0 ; // Default call count is 0
  20.                    this.resetTime = Duration.ofMinutes(resetTimeUserDefinedMinutes);  
  21.                    // Use it for Debugging  
  22.                    
  23.                    //this.resetTime = Duration.ofMinutes(1);
  24.                                                            /*
  25.                                                               The Value is set for per minute requests.
  26.                                                               Duration Set . This method can be
  27.                                                               used for days , hours , minutes as well .
  28.                                                            */
  29.                                                            
  30.                    this.lastReset = Instant.now() ; /*
  31.                                                        Sets the current time as last reset time
  32.                                                        after resetting .
  33.                                                     */
  34.                  
  35.                    
  36.             }
  37.     public void rateLimitResetter()
  38.         {
  39.           //System.out.println(" Reset "); // Use to Debug whether the Requests are resetted or not
  40.             Instant currentTime = Instant.now();
  41.            
  42.           //  System.out.print(currentTime+"       "+lastReset); // Check the Time Difference
  43.             if(Duration.between(lastReset,currentTime).compareTo(resetTime)>=0)
  44.                 {
  45.                     callCount = 0 ;
  46.                     lastReset = currentTime ;
  47.                 }
  48.         }
  49.     public int getSum(int a , int b) throws rateLimitException, InterruptedException
  50.         {
  51.             rateLimitResetter() ;
  52.             if(callCount >= callLimit)
  53.                 {
  54.                     Instant currentTime = Instant.now();
  55.                    
  56.                     long elapsedTime = ChronoUnit.SECONDS.between(lastReset, currentTime) ;
  57.                     long remainingTime = resetTimeUserDefinedMinutes*60 - elapsedTime ;
  58.                     throw new rateLimitException(" Your request limit has exceeded or a problem occured ! "
  59.                             + "Try again later after "
  60.                             + remainingTime
  61.                             + " Seconds "
  62.                     );                    
  63.                 }
  64.            
  65.             Thread.sleep(100); /* Slows Down to Check Output .
  66.                                 Use this Function to check when the requests are processed .
  67.                                */
  68.             callCount++ ; // Function Access is Granted , so add 1 .
  69.            
  70.             return a+b ;
  71.         }
  72.    
  73.     public static void main(String args[])
  74.                 {
  75.                     RateLimitedCalculator request = new RateLimitedCalculator(5) ; // Insert the Call Limit to be processed
  76.                    
  77.                    
  78.                         for(int i= 0 ; i<10000;i++)   // A huge number of requests .
  79.                         {
  80.                             try{
  81.                             System.out.println("The Sum is : " + request.getSum(10,1));
  82.                             }
  83.                             catch(InterruptedException | rateLimitException e )  
  84.                             {
  85.                             System.out.println(e) ;
  86.                             }
  87.                         }
  88.                    
  89.                 }
  90. }
  91.  
  92. class rateLimitException extends Exception{
  93.     public rateLimitException(String alertMessage)
  94.     {
  95.         super(alertMessage) ;
  96.     }
  97. }