I've been playing around with a thottling system and stumbled upon the Guava RateLimiter. From what I can tell the two main ways it handles throttling are either by queueing the overlowing requests (the .acquire(...) methods) or by discarding them (the tryAcquire(...) methods)
I was thinking that there would be an option that would allow requests up to the specified amount and only after reaching said limit queue or discard requests.
For instance:
public static void main( String[] args )
{
try
{
RateLimiter limiter = RateLimiter.create(5.0 );
//fictive call not saying it should be implemented this way
limiter.allowBursts(true);
for( int i = 0; i < 20; i++ )
{
Thread.sleep( 100 );
performOperation( limiter );
}
}
catch( InterruptedException e )
{
e.printStackTrace();
}
}
private static void performOperation( RateLimiter limiter )
{
if( limiter.tryAcquire() )
{
System.out.println( Instant.now() + ": Beep" );
}
}
This would then print out five beeps, omit the next five and then print five again
Am I the only one thinking this would be a usefull feature to have or am I missing the point?
Test code courtesy of: Throttling method calls using Guava RateLimiter class