Class Delayer<T>
java.lang.Object
de.aristaflow.adept2.util.threading.Delayer<T>
- Type Parameters:
T- The type of elements for which a delay is required.
A delayer provides the means to delay a call based on the amount of tries a
provided element has experienced. For instance, when logging on with a fixed
user name, one should delay failing authentication tries to avoid brute-force
attacks. As for the authentication, a few free tries are possible to allow
for misspelling without delaying. Additionally, there is an upper bound for
the delay and thus also an upper bound for the time the amount of tries are
remembered. As soon as the authentication (or whatever requires the delay)
has been successful, the delay for the corresponding element should be
reset.
- Author:
- Ulrich Kreher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classA runnable for resetting the try count and thus remove all delays. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final LoggingScheduledThreadPoolExecutorThe executor service for cleaning up after a specified time.protected static final longThe maximum delay per try in milliseconds.protected intThe amount of tries that lead to a delay just below maximum delay per try.protected final longThe time in milliseconds the try count keeps pending and is not reset.protected final Map<T,ScheduledFuture<?>> The futures for the resetting runnables.protected final Map<T,Pair<AtomicInteger, Runnable>> The number of tries of an element (and the corresponding resetting runnable).protected final intThe number of tries, that are not subject of a delay. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDelays the current thread depending on the current amount of tries for the designated element needs a delay.protected longgetDelay(int tryCount) Calculates the delay (in milliseconds) for the designated try count.voidResets the try count and delay for the designated element.
-
Field Details
-
MAX_DELAY_PER_TRY
protected static final long MAX_DELAY_PER_TRYThe maximum delay per try in milliseconds. If the calculated delay exceeds this time, time will be used for delay instead of the calculated one.- See Also:
-
executor
The executor service for cleaning up after a specified time. -
undelayedTries
protected final int undelayedTriesThe number of tries, that are not subject of a delay. -
pendingTries
protected final long pendingTriesThe time in milliseconds the try count keeps pending and is not reset. If there is another try within this time after the last delay, the try count just continues. If this time elapses, the try count resets and allows tries without delay again.
This time has to be at least the number of tries without delay multiplied with the maximum delay per try to compensate for the tries without after this time has elapsed. -
maxDelayTry
protected int maxDelayTryThe amount of tries that lead to a delay just below maximum delay per try. One additional try exceeds this maximum delay. -
tryCounts
The number of tries of an element (and the corresponding resetting runnable).AtomicIntegeris used here since it is mutable in contrast toIntegerand thus avoids the creation of numerous (immutable)Pairs. -
resetFutures
The futures for the resetting runnables. These need to be cancelled after a new try and the runnable needs to be rescheduled.
-
-
Constructor Details
-
Delayer
Creates a new delayer with the designated name and the designated amount of tries without a delay, delaying a call dependent of the element and the amount of tries for the element.- Parameters:
name- The name of the delayer (used for internal scheduler thread).undelayedTries- The number of tries that are not subject of delay. As soon as this number is exceeded, a delay will take place.
-
-
Method Details
-
delay
Delays the current thread depending on the current amount of tries for the designated element needs a delay. This increases the current amount of tries for the designated element and respects the lower (the amount of tries without delay) and the upper (the maximum delay per try) bound for the delay.- Parameters:
element- The element for which to count a try and possibly delay.- Throws:
InterruptedException- If the waiting thread is interrupted while waiting for the delay, anInterruptedExceptionwill be thrown. Since this allows to circumvent the delay, callers must not ignore the interrupt but handle it appropriately for instance by shutting down.
-
reset
Resets the try count and delay for the designated element.- Parameters:
element- The element for which to reset the try count and the delay.
-
getDelay
protected long getDelay(int tryCount) Calculates the delay (in milliseconds) for the designated try count. The delay will be 0 for the number of tries without delay and it will not exceed the maximum delay per try.
This implementation doubles the delay for each try.- Parameters:
tryCount- The amount of tries that have been used yet.- Returns:
- The delay for the designated amount of tries.
-