- All Implemented Interfaces:
IuTaskController
,UnsafeConsumer<IuTaskController>
controlled
tasks
.
This rate limiter is fail-fast. All tasks are expected to complete normally without error. The first error or timeout condition should be thrown when the controlling process reaches its next blocking operation.
Once a fixed limit on incomplete tasks has been reached, new tasks cannot be
accepted without first removing and joining the task at the head of the
queue. Once any task has timed out or produced an error, any attempt to
accept(IuTaskController)
or join()
will throw that error.
Additional errors encountered after the first error will be suppressed.
final var limit = new IuRateLimitter(10, Duration.ofSeconds(1L)); for (UnsafeRunnable task : tasks) limit.accept(workload.accept(task)); limit.join();
-
Constructor Summary
ConstructorsConstructorDescriptionIuRateLimitter
(int limit, Duration timeout) Constructor.IuRateLimitter
(int limit, Instant expires) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(IuTaskController taskController) Accepts a task for parallel processing, blocking until an error is observed, a timeout interval expires, the task completes successfully, or until there is room in the queue.void
failFast()
Checks for a non-blocking failure condition that would prevent a new task from beingaccepted
for processing.Gets time elapsed since the task started.getError()
Gets an error thrown by the task.Gets the point in time the timeout interval expires, or expired.Gets time remaining until the task expires.getStart()
Gets task execution start time.void
Interrupts all threads that invokedIuTaskController.pause()
orIuTaskController.join()
on this task.boolean
Determines if task is completed.boolean
Determines if task was still executing when the timeout interval expired; the task may havecompleted successfully
after expiring.boolean
Determines if the task completed successfully.void
join()
Waits for task execution to complete.void
pause()
Pauses the current thread untilunpaused
or until the task is completed or expired.void
unpause()
Unpauses all threads that invokedIuTaskController.pause()
on this task.
-
Constructor Details
-
IuRateLimitter
Constructor.- Parameters:
limit
- upper limit on the number of tasks that may be pending in the queue.timeout
- timeout interval
-
IuRateLimitter
Constructor.- Parameters:
limit
- upper limit on the number of tasks that may be pending in the queue.expires
- instant the workload timeout interval expires
-
-
Method Details
-
failFast
Checks for a non-blocking failure condition that would prevent a new task from beingaccepted
for processing.The method should be called returning control from any blocking operation, before scheduling a new task to be accepted by this rate limiter. If the task would be rejected, an error is thrown to prevent it from being scheduled.
At the instant of return, the following conditions are guaranteed to have been true:
- No
error
was observed by anaccepted
task.ExecutionException
will be thrown with the first error observed as the cause; additional observed errors will be suppressed. - No tasks have expired without completing successfully.
TimeoutException
will be thrown; any related taskTimeoutException
s will be suppressed.
- Throws:
ExecutionException
- if anerror
has been observed by anaccepted
task.TimeoutException
- if any task has expired; related task timeouts will be suppressed
- No
-
accept
public void accept(IuTaskController taskController) throws ExecutionException, InterruptedException, TimeoutException Accepts a task for parallel processing, blocking until an error is observed, a timeout interval expires, the task completes successfully, or until there is room in the queue.At the instant of return, the following conditions are guaranteed to have been true:
failFast()
returned successfully.- The new task did not observe an error
- else
ExecutionException
is thrown with the observed error as the cause.
- else
- The new task did not
expire
- else
TimeoutException
is thrown.
- else
- The task either
completed
succesfully
, or was accepted for parallel processing. - Accepted tasks may have been
joined
to create room in the queue. - No more than
limit
accepted tasks, including the new task, were incomplete.
- Specified by:
accept
in interfaceUnsafeConsumer<IuTaskController>
- Parameters:
taskController
- task controller- Throws:
ExecutionException
- if an observed; the observed error is the causeInterruptedException
- fromIuTaskController.join()
TimeoutException
- if a timeout interval expires
-
getStart
Description copied from interface:IuTaskController
Gets task execution start time.- Specified by:
getStart
in interfaceIuTaskController
- Returns:
- start time; null if the task has not started executing
-
getElapsed
Description copied from interface:IuTaskController
Gets time elapsed since the task started.- Specified by:
getElapsed
in interfaceIuTaskController
- Returns:
- elapsed time; execution time if the task is completed, null if the task has not started
-
getRemaining
Description copied from interface:IuTaskController
Gets time remaining until the task expires.- Specified by:
getRemaining
in interfaceIuTaskController
- Returns:
- remaining time until the task expires; zero if the task completed within the timeout interval, negative to represent post-expiration processing time if expired and incomplete or completed after the timeout interval.
-
getExpires
Description copied from interface:IuTaskController
Gets the point in time the timeout interval expires, or expired.- Specified by:
getExpires
in interfaceIuTaskController
- Returns:
Instant
-
isComplete
public boolean isComplete()Description copied from interface:IuTaskController
Determines if task is completed.- Specified by:
isComplete
in interfaceIuTaskController
- Returns:
- true if the task has completed: either
IuTaskController.isSuccess()
will returns true orIuTaskController.getError()
returns a non-null value; false indicates the task has not started or is still executing.
-
isSuccess
public boolean isSuccess()Description copied from interface:IuTaskController
Determines if the task completed successfully.- Specified by:
isSuccess
in interfaceIuTaskController
- Returns:
- true if the task completed successfully.
-
getError
Description copied from interface:IuTaskController
Gets an error thrown by the task.- Specified by:
getError
in interfaceIuTaskController
- Returns:
- error, if thrown by the task; else null
-
isExpired
public boolean isExpired()Description copied from interface:IuTaskController
Determines if task was still executing when the timeout interval expired; the task may havecompleted successfully
after expiring.Attempting to
IuTaskController.join()
anexpired
task should result inTimeoutException
being thrown.- Specified by:
isExpired
in interfaceIuTaskController
- Returns:
- true if the task was still executing when the timeout interval expired; else false, the task completed or is still executing and the interval hasn't expired.
-
join
Description copied from interface:IuTaskController
Waits for task execution to complete.Attempting to
IuTaskController.join()
anexpired
task should result inTimeoutException
being thrown.- Specified by:
join
in interfaceIuTaskController
- Throws:
ExecutionException
- If anyThrowable
is thrown from the task; the thrown value will be the causeInterruptedException
- If the task is interrupted.TimeoutException
- If the timeout interval expires.
-
pause
Description copied from interface:IuTaskController
Pauses the current thread untilunpaused
or until the task is completed or expired.The state of the current thread is not guaranteed upon completion. The application using this interface is responsible for inspecting and/or updating after the pause is complete.
- Specified by:
pause
in interfaceIuTaskController
- Throws:
InterruptedException
- If the current thread is interrupted while paused.TimeoutException
- If the timeout interval is reached while paused.
-
unpause
public void unpause()Description copied from interface:IuTaskController
Unpauses all threads that invokedIuTaskController.pause()
on this task.- Specified by:
unpause
in interfaceIuTaskController
-
interrupt
public void interrupt()Description copied from interface:IuTaskController
Interrupts all threads that invokedIuTaskController.pause()
orIuTaskController.join()
on this task.- Specified by:
interrupt
in interfaceIuTaskController
-