QuotaServer

public class QuotaServer extends Object

Utility object to enforce quota requirements.

Create an instance to enforce individual quota maximums on a set of operations defined by an enumeration. Before executing a quota restricted operation, call the acquire(T) method to restrict the rate at which the operation is allowed to execute. Internally, quota is enforced using a RateLimiter object. The calling thread is blocked if the token cannot be granted immediately.

Nested Class Summary

class QuotaServer.Builder<T extends Enum<T>, K extends QuotaServer<T>> Builder for QuotaServer instances. 

Constant Summary

double DEFAULT_QPS

Public Method Summary

double
acquire(T operation)
Acquires a token before allowing an operation to execute.
static <T extends Enum<T>> QuotaServer<T>
createFromConfiguration(String quotaPrefix, Class<T> enumClass)
Creates a QuotaServer instance using parameter values from the configuration file.
double
getRate(T operation)
Returns current QPS rate limit for the specified enumeration operation.

Inherited Method Summary

Constants

public static final double DEFAULT_QPS

Constant Value: 10.0

Public Methods

public double acquire (T operation)

Acquires a token before allowing an operation to execute.

Note: The calling thread is blocked if the token cannot be granted immediately.

Parameters
operation the enumeration operation to rate limit base on its quota
Returns
  • time spent sleeping to enforce quota in seconds (0.0 if not rate limited)

public static QuotaServer<T> createFromConfiguration (String quotaPrefix, Class<T> enumClass)

Creates a QuotaServer instance using parameter values from the configuration file.

The configuration file should have QPS value parameters in the format of:

  • quotaServer.[quotaPrefix].defaultQps = 10
  • quotaServer.[quotaPrefix].ENUM_OPERATION1 = 2;
  • quotaServer.[quotaPrefix].ENUM_OPERATION2 = 5;
  • quotaServer.[quotaPrefix].ENUM_OPERATION3 = 20;

Where quotaPrefix is defined by the connector and the ENUM_* values are operation types defined in an enumeration. Any unspecified ENUM_* values take the QPS value from the defaultQps parameter.

Parameters
quotaPrefix prefix for configuration keys related to quota server parameters
enumClass class for enumerations representing operations
Returns

public double getRate (T operation)

Returns current QPS rate limit for the specified enumeration operation.

Parameters
operation for rate limit lookup
Returns
  • current configured rate limit for the operation