- 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 TypeMethodDescriptionvoidaccept(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.voidfailFast()Checks for a non-blocking failure condition that would prevent a new task from beingacceptedfor 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.voidInterrupts all threads that invokedIuTaskController.pause()orIuTaskController.join()on this task.booleanDetermines if task is completed.booleanDetermines if task was still executing when the timeout interval expired; the task may havecompleted successfullyafter expiring.booleanDetermines if the task completed successfully.voidjoin()Waits for task execution to complete.voidpause()Pauses the current thread untilunpausedor until the task is completed or expired.voidunpause()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 beingacceptedfor 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
errorwas observed by anacceptedtask.ExecutionExceptionwill be thrown with the first error observed as the cause; additional observed errors will be suppressed. - No tasks have expired without completing successfully.
TimeoutExceptionwill be thrown; any related taskTimeoutExceptions will be suppressed.
- Throws:
ExecutionException- if anerrorhas been observed by anacceptedtask.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
ExecutionExceptionis thrown with the observed error as the cause.
- else
- The new task did not
expire- else
TimeoutExceptionis thrown.
- else
- The task either
completedsuccesfully, or was accepted for parallel processing. - Accepted tasks may have been
joinedto create room in the queue. - No more than
limitaccepted tasks, including the new task, were incomplete.
- Specified by:
acceptin 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:IuTaskControllerGets task execution start time.- Specified by:
getStartin interfaceIuTaskController- Returns:
- start time; null if the task has not started executing
-
getElapsed
Description copied from interface:IuTaskControllerGets time elapsed since the task started.- Specified by:
getElapsedin interfaceIuTaskController- Returns:
- elapsed time; execution time if the task is completed, null if the task has not started
-
getRemaining
Description copied from interface:IuTaskControllerGets time remaining until the task expires.- Specified by:
getRemainingin 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:IuTaskControllerGets the point in time the timeout interval expires, or expired.- Specified by:
getExpiresin interfaceIuTaskController- Returns:
Instant
-
isComplete
public boolean isComplete()Description copied from interface:IuTaskControllerDetermines if task is completed.- Specified by:
isCompletein 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:IuTaskControllerDetermines if the task completed successfully.- Specified by:
isSuccessin interfaceIuTaskController- Returns:
- true if the task completed successfully.
-
getError
Description copied from interface:IuTaskControllerGets an error thrown by the task.- Specified by:
getErrorin interfaceIuTaskController- Returns:
- error, if thrown by the task; else null
-
isExpired
public boolean isExpired()Description copied from interface:IuTaskControllerDetermines if task was still executing when the timeout interval expired; the task may havecompleted successfullyafter expiring.Attempting to
IuTaskController.join()anexpiredtask should result inTimeoutExceptionbeing thrown.- Specified by:
isExpiredin 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:IuTaskControllerWaits for task execution to complete.Attempting to
IuTaskController.join()anexpiredtask should result inTimeoutExceptionbeing thrown.- Specified by:
joinin interfaceIuTaskController- Throws:
ExecutionException- If anyThrowableis 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:IuTaskControllerPauses the current thread untilunpausedor 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:
pausein 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:IuTaskControllerUnpauses all threads that invokedIuTaskController.pause()on this task.- Specified by:
unpausein interfaceIuTaskController
-
interrupt
public void interrupt()Description copied from interface:IuTaskControllerInterrupts all threads that invokedIuTaskController.pause()orIuTaskController.join()on this task.- Specified by:
interruptin interfaceIuTaskController
-