Nitido Inc.

com.nitido.throttle
Class CountPerTimeThrottle

java.lang.Object
  extended by com.nitido.throttle.CountPerTimeThrottle
All Implemented Interfaces:
NonBlockingThrottle

public class CountPerTimeThrottle
extends java.lang.Object
implements NonBlockingThrottle

Provides throttling that controls the number of calls within a specific time duration. This throttle remembers when the successful request happens. As a result, it can control the total number of requests can be processed within a period of time (such as 100 requests per minute). If you simply want to control the number of threads that can access the protected resource at the same time, you should use CountThrottle instead (which is faster and uses less memory).

This throttle is non-blocking and the result will be returned quickly. (i.e. if the throttle limit is exceeded, the enterThrottle() method will return false immediately.)

This object is designed to be thread-safe and can be accessed by multiple threads. All operation will happen quickly and should not block the thread for a long time.

The default constructor takes in the countLimit (let's say N) and the duration (let's say T). When you call the enterThrottle() method, this object will return true if the request can enter the throttle (i.e. it is within the limit of N request per T milliseconds). Otherwise, it will return false.

Although the exitThrottle() method does not do anything, developers should still call it after they finish their own processing. In other words, the code should be in the format:

     CountPerTimeThrottle _throttle = new CounterPerTimeThrottle( N, T );
     public void myMethod( )
     {
         if( !_throttle.enterThrottle() )
         {
             throw new Excpetion("Exceed throttle limit.");
         }

         try {
             // Start of your operation
             // ....
             // End of your operation
         } finally {
             _throttle.exitThrottle();
         }
     }
 


Constructor Summary
CountPerTimeThrottle(int countLimit, long duration)
          Default constructor.
 
Method Summary
 boolean enterThrottle()
          Check if the current call can enter the throttle sucessfully.
 void exitThrottle()
          Leaving the throttle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CountPerTimeThrottle

public CountPerTimeThrottle(int countLimit,
                            long duration)
Default constructor.

Parameters:
countLimit - The number of calls allowed within the duration. It must be larger than 0.
duration - The duration time in milliseconds. It must be larger than 0.
Method Detail

enterThrottle

public boolean enterThrottle()
Check if the current call can enter the throttle sucessfully.

Specified by:
enterThrottle in interface NonBlockingThrottle
Returns:
true if pass the throttle control successfully, false if otherwise.

exitThrottle

public void exitThrottle()
Leaving the throttle.

Specified by:
exitThrottle in interface NonBlockingThrottle

Nitido NiM 2.5 Java API

These JavaDoc pages are generated for release/nim_2_5-2.5.44

Copyright © 1999-2009 Nitido Inc.    Proprietary and Confidential.    All Rights Reserved.