com.univocity.api.io
Class RateLimiter

java.lang.Object
  extended by com.univocity.api.io.RateLimiter

public class RateLimiter
extends Object

A very simple method call rate limiter, based on a time interval. Users can call waitAndGo() or waitAndGo(long) to block the current thread before performing an operation that is potentially costly on other resources (such as HTTP requests to remote servers).


Constructor Summary
RateLimiter(long interval)
          Creates a new rate limiter configured to allow for a given interval of time to elapse before allowing a process to continue.
RateLimiter(String name, long interval)
          Creates a new rate limiter configured to allow for a given interval of time to elapse before allowing a process to continue.
 
Method Summary
 void decreaseWaitTime(long timeToDecrease)
          Decreases the wait time of the next process that will be put to wait.
 long getInterval()
          Returns the current rate interval
 String getName()
          Returns the name associated with this rate limiter, used for logging.
 long getWaitingCount()
          Returns the number of threads blocked and waiting for their turn to execute
 void increaseWaitTime(long timeToIncrease)
          Increases the wait time of the next process that will be put to wait.
 void setInterval(long interval)
          Modifies the rate interval
 long waitAndGo()
          Blocks the current process if the previous call to this method happened at a time less than the configured interval.
 long waitAndGo(long timeout)
          Blocks the current process if the previous call to this method happened at a time less than the configured interval.
 long waitAndGo(String action)
          Blocks the current process if the previous call to this method happened at a time less than the configured interval.
 long waitAndGo(String action, long timeout)
          Blocks the current process if the previous call to this method happened at a time less than the configured interval.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RateLimiter

public RateLimiter(long interval)
Creates a new rate limiter configured to allow for a given interval of time to elapse before allowing a process to continue.

Parameters:
interval - the interval to wait between each call to waitAndGo()

RateLimiter

public RateLimiter(String name,
                   long interval)
Creates a new rate limiter configured to allow for a given interval of time to elapse before allowing a process to continue.

Parameters:
name - a name for the rate limiter, used for logging
interval - the interval to wait between each call to waitAndGo()
Method Detail

getName

public String getName()
Returns the name associated with this rate limiter, used for logging.

Returns:
the name of this rate limiter

getInterval

public final long getInterval()
Returns the current rate interval

Returns:
the current interval

setInterval

public void setInterval(long interval)
Modifies the rate interval

Parameters:
interval - the new interval

getWaitingCount

public final long getWaitingCount()
Returns the number of threads blocked and waiting for their turn to execute

Returns:
the number of threads blocked

waitAndGo

public final long waitAndGo(long timeout)
                     throws TimeoutException
Blocks the current process if the previous call to this method happened at a time less than the configured interval. The waiting time will be the time difference between the configured interval and the time elapsed since the previous call to this method.

Parameters:
timeout - the maximum length of time the process is allowed to wait. A TimeoutException will be thrown if the process got locked up for too long.
Returns:
the number of threads still blocked and waiting.
Throws:
TimeoutException - if the wait time exceeds the given timeout. Will only be thrown after the process gets unblocked - at some time potentially much longer than the given timeout.

waitAndGo

public final long waitAndGo(String action,
                            long timeout)
                     throws TimeoutException
Blocks the current process if the previous call to this method happened at a time less than the configured interval. The waiting time will be the time difference between the configured interval and the time elapsed since the previous call to this method.

Parameters:
action - description of the action to be performed after the wait time is over. Used for logging.
timeout - the maximum length of time the process is allowed to wait. A TimeoutException will be thrown if the process got locked up for too long.
Returns:
the number of threads still blocked and waiting.
Throws:
TimeoutException - if the wait time exceeds the given timeout. Will only be thrown after the process gets unblocked - at some time potentially much longer than the given timeout.

waitAndGo

public final long waitAndGo()
Blocks the current process if the previous call to this method happened at a time less than the configured interval. The waiting time will be the time difference between the configured interval and the time elapsed since the previous call to this method.

Returns:
the number of threads still blocked and waiting.

waitAndGo

public final long waitAndGo(String action)
Blocks the current process if the previous call to this method happened at a time less than the configured interval. The waiting time will be the time difference between the configured interval and the time elapsed since the previous call to this method.

Parameters:
action - description of the action to be performed after the wait time is over. Used for logging.
Returns:
the number of threads still blocked and waiting.

decreaseWaitTime

public final void decreaseWaitTime(long timeToDecrease)
Decreases the wait time of the next process that will be put to wait. The wait time returns back to the interval provided in the constructor of this class after the next process is blocked

Parameters:
timeToDecrease - length of time to decrease from the configured interval.

increaseWaitTime

public final void increaseWaitTime(long timeToIncrease)
Increases the wait time of the next process that will be put to wait. The wait time returns back to the interval provided in the constructor of this class after the next process is blocked

Parameters:
timeToIncrease - length of time add to the configured interval.


Copyright © 2018 uniVocity Software Pty Ltd. All rights reserved.