public class KeyedRateLimiterExecutor
extends java.lang.Object
RateLimiterExecutor
except that the rate is applied on a per key basis.
Tasks submitted to this executor must all be associated to a key. The key is compared by using
Object.hashCode()
and Object.equals(Object)
. For any given key, a rate is
applied, but keys which don't match with the above checks will not impact each other.
Because different keys don't interact, this is not capable of providing a global rate limit (unless the key quantity is known and restricted).
This differs from KeyedExecutorLimiter
in that while that limits concurrency, this
limits by rate (thus there may be periods where nothing is execution, or if executions are long
things may run concurrently). Please see RateLimiterExecutor
for more details about how
rate is limited.
Constructor and Description |
---|
KeyedRateLimiterExecutor(SubmitterScheduler scheduler,
double permitsPerSecond)
Constructs a new key rate limiting executor.
|
KeyedRateLimiterExecutor(SubmitterScheduler scheduler,
double permitsPerSecond,
long maxScheduleDelayMillis)
Constructs a new key rate limiting executor.
|
KeyedRateLimiterExecutor(SubmitterScheduler scheduler,
double permitsPerSecond,
long maxScheduleDelayMillis,
RejectedExecutionHandler rejectedExecutionHandler)
Constructs a new key rate limiting executor.
|
KeyedRateLimiterExecutor(SubmitterScheduler scheduler,
double permitsPerSecond,
long maxScheduleDelayMillis,
RejectedExecutionHandler rejectedExecutionHandler,
java.lang.String subPoolName,
boolean addKeyToThreadName)
Constructs a new key rate limiting executor.
|
KeyedRateLimiterExecutor(SubmitterScheduler scheduler,
double permitsPerSecond,
long maxScheduleDelayMillis,
java.lang.String subPoolName,
boolean addKeyToThreadName)
Constructs a new key rate limiting executor.
|
KeyedRateLimiterExecutor(SubmitterScheduler scheduler,
double permitsPerSecond,
java.lang.String subPoolName,
boolean addKeyToThreadName)
Constructs a new key rate limiting executor.
|
Modifier and Type | Method and Description |
---|---|
long |
execute(double permits,
java.lang.Object taskKey,
java.lang.Runnable task)
Provide a task to be run with a given thread key.
|
void |
execute(java.lang.Object taskKey,
java.lang.Runnable task)
Provide a task to be run with a given thread key.
|
ListenableFuture<?> |
getFutureTillDelay(java.lang.Object taskKey,
long maximumDelay)
In order to help assist with avoiding to schedule too much on the scheduler at any given
time, this call returns a future that will block until the delay for the next task falls
below the maximum delay provided into this call.
|
int |
getMinimumDelay(java.lang.Object taskKey)
This call will check how far out we have already scheduled tasks to be run.
|
SubmitterExecutor |
getSubmitterExecutorForKey(double permits,
java.lang.Object taskKey)
Returns an executor implementation where all tasks submitted on this executor will run on the
provided key.
|
SubmitterExecutor |
getSubmitterExecutorForKey(java.lang.Object taskKey)
Returns an executor implementation where all tasks submitted on this executor will run on the
provided key.
|
int |
getTrackedKeyCount()
Check how many keys are currently being restricted or monitored.
|
<T> ListenableFuture<T> |
submit(double permits,
java.lang.Object taskKey,
java.util.concurrent.Callable<T> task)
Submit a callable to be run with a given thread key.
|
ListenableFuture<?> |
submit(double permits,
java.lang.Object taskKey,
java.lang.Runnable task)
Submit a task to be run with a given thread key.
|
<T> ListenableFuture<T> |
submit(double permits,
java.lang.Object taskKey,
java.lang.Runnable task,
T result)
Submit a task to be run with a given thread key.
|
<T> ListenableFuture<T> |
submit(java.lang.Object taskKey,
java.util.concurrent.Callable<T> task)
Submit a callable to be run with a given thread key.
|
ListenableFuture<?> |
submit(java.lang.Object taskKey,
java.lang.Runnable task)
Submit a task to be run with a given thread key.
|
<T> ListenableFuture<T> |
submit(java.lang.Object taskKey,
java.lang.Runnable task,
T result)
Submit a task to be run with a given thread key.
|
public KeyedRateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond)
This will schedule tasks out infinitely far in order to maintain rate. If you want tasks to
be rejected at a certain point consider using
KeyedRateLimiterExecutor(SubmitterScheduler, double, long)
.
scheduler
- Scheduler to defer executions topermitsPerSecond
- how many permits should be allowed per second per keypublic KeyedRateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, java.lang.String subPoolName, boolean addKeyToThreadName)
subPoolName
and false
for appending
the key to the thread name will result in no thread name adjustments occurring.
This will schedule tasks out infinitely far in order to maintain rate. If you want tasks to
be rejected at a certain point consider using
KeyedRateLimiterExecutor(SubmitterScheduler, double, long, String, boolean)
.
scheduler
- Scheduler to defer executions topermitsPerSecond
- how many permits should be allowed per second per keysubPoolName
- Prefix to give threads while executing tasks submitted through this limiteraddKeyToThreadName
- true
to append the task's key to the thread namepublic KeyedRateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, long maxScheduleDelayMillis)
This constructor accepts a maximum schedule delay. If a task requires being scheduled out
beyond this delay, then a RejectedExecutionException
will be
thrown instead of scheduling the task.
scheduler
- Scheduler to defer executions topermitsPerSecond
- how many permits should be allowed per second per keymaxScheduleDelayMillis
- Maximum amount of time delay tasks in order to maintain ratepublic KeyedRateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, long maxScheduleDelayMillis, RejectedExecutionHandler rejectedExecutionHandler)
This constructor accepts a maximum schedule delay. If a task requires being scheduled out
beyond this delay, then a RejectedExecutionException
will be
thrown instead of scheduling the task.
scheduler
- Scheduler to defer executions topermitsPerSecond
- how many permits should be allowed per second per keymaxScheduleDelayMillis
- Maximum amount of time delay tasks in order to maintain raterejectedExecutionHandler
- Handler to accept tasks which could not be executedpublic KeyedRateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, long maxScheduleDelayMillis, java.lang.String subPoolName, boolean addKeyToThreadName)
subPoolName
and false
for appending
the key to the thread name will result in no thread name adjustments occurring.
This constructor accepts a maximum schedule delay. If a task requires being scheduled out
beyond this delay, then a RejectedExecutionException
will be
thrown instead of scheduling the task.
scheduler
- Scheduler to defer executions topermitsPerSecond
- how many permits should be allowed per second per keymaxScheduleDelayMillis
- Maximum amount of time delay tasks in order to maintain ratesubPoolName
- Prefix to give threads while executing tasks submitted through this limiteraddKeyToThreadName
- true
to append the task's key to the thread namepublic KeyedRateLimiterExecutor(SubmitterScheduler scheduler, double permitsPerSecond, long maxScheduleDelayMillis, RejectedExecutionHandler rejectedExecutionHandler, java.lang.String subPoolName, boolean addKeyToThreadName)
subPoolName
and false
for appending
the key to the thread name will result in no thread name adjustments occurring.
This constructor accepts a maximum schedule delay. If a task requires being scheduled out
beyond this delay, then a RejectedExecutionException
will be
thrown instead of scheduling the task.
scheduler
- Scheduler to defer executions topermitsPerSecond
- how many permits should be allowed per second per keymaxScheduleDelayMillis
- Maximum amount of time delay tasks in order to maintain raterejectedExecutionHandler
- Handler to accept tasks which could not be executedsubPoolName
- Prefix to give threads while executing tasks submitted through this limiteraddKeyToThreadName
- true
to append the task's key to the thread namepublic int getTrackedKeyCount()
public int getMinimumDelay(java.lang.Object taskKey)
taskKey
- object key where equals()
will be used to determine execution threadpublic ListenableFuture<?> getFutureTillDelay(java.lang.Object taskKey, long maximumDelay)
taskKey
- object key where equals()
will be used to determine execution threadmaximumDelay
- maximum delay in milliseconds until returned Future should unblockget()
calls once delay has been reduced below the provided maximumpublic void execute(java.lang.Object taskKey, java.lang.Runnable task)
See also: Executor.execute(Runnable)
and
RateLimiterExecutor.execute(Runnable)
.
taskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic long execute(double permits, java.lang.Object taskKey, java.lang.Runnable task)
See also: Executor.execute(Runnable)
and
RateLimiterExecutor.execute(double, Runnable)
.
permits
- resource permits for this tasktaskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executed-1
if rejected but handler did not throwpublic ListenableFuture<?> submit(java.lang.Object taskKey, java.lang.Runnable task)
See also: SubmitterExecutor.submit(Runnable)
and
SubmitterExecutor.submit(Runnable)
.
taskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic ListenableFuture<?> submit(double permits, java.lang.Object taskKey, java.lang.Runnable task)
See also: SubmitterExecutor.submit(Runnable)
and
RateLimiterExecutor.submit(double, Runnable)
.
permits
- resource permits for this tasktaskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic <T> ListenableFuture<T> submit(java.lang.Object taskKey, java.lang.Runnable task, T result)
See also: SubmitterExecutor.submit(Runnable, Object)
and
RateLimiterExecutor.submit(Runnable, Object)
.
T
- type of result returned from the futuretaskKey
- object key where equals()
will be used to determine execution threadtask
- Runnable to be executedresult
- Result to be returned from future when task completespublic <T> ListenableFuture<T> submit(double permits, java.lang.Object taskKey, java.lang.Runnable task, T result)
See also: SubmitterExecutor.submit(Runnable, Object)
and
RateLimiterExecutor.submit(double, Runnable, Object)
.
T
- type of result returned from the futurepermits
- resource permits for this tasktaskKey
- object key where equals()
will be used to determine execution threadtask
- Runnable to be executedresult
- Result to be returned from future when task completespublic <T> ListenableFuture<T> submit(java.lang.Object taskKey, java.util.concurrent.Callable<T> task)
See also: SubmitterExecutor.submit(Callable)
and
RateLimiterExecutor.submit(Callable)
.
T
- type of result returned from the futuretaskKey
- object key where equals()
will be used to determine execution threadtask
- Callable to be executedpublic <T> ListenableFuture<T> submit(double permits, java.lang.Object taskKey, java.util.concurrent.Callable<T> task)
See also: SubmitterExecutor.submit(Callable)
and
RateLimiterExecutor.submit(double, Callable)
.
T
- type of result returned from the futurepermits
- resource permits for this tasktaskKey
- object key where equals()
will be used to determine execution threadtask
- Callable to be executedpublic SubmitterExecutor getSubmitterExecutorForKey(java.lang.Object taskKey)
taskKey
- object key where equals()
will be used to determine execution threadpublic SubmitterExecutor getSubmitterExecutorForKey(double permits, java.lang.Object taskKey)
permits
- resource permits for all tasks submitted on the returned executortaskKey
- object key where equals()
will be used to determine execution thread