Class Retryer.Delay<E>
- All Implemented Interfaces:
Comparable<Retryer.Delay<E>>
- Enclosing class:
Retryer
E
prior to the retry attempt.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterDelay
(E event) Called after the delay, immediately before the retry.void
beforeDelay
(E event) Called ifevent
will be retried after the delay.int
compareTo
(Retryer.Delay<E> that) abstract Duration
duration()
Returns the delay interval.boolean
final List
<Retryer.Delay<E>> exponentialBackoff
(double multiplier, int size) Returns an immutableList
of delays withsize
.final List
<Retryer.Delay<E>> fibonacci
(int size) Returns a fibonacci list of delays ofsize
, as in1, 1, 2, 3, 5, 8, ...
withthis
delay being the multiplier.int
hashCode()
final Retryer.Delay
<E> multipliedBy
(double multiplier) Returns a newDelay
with duration multiplied bymultiplier
.static <E> Retryer.Delay
<E> Returns aDelay
ofduration
.static <E> Retryer.Delay
<E> ofMillis
(long millis) Shorthand forof(Duration.ofMillis(millis))
.final Retryer.Delay
<E> randomized
(Random random, double randomness) Returns a newDelay
with some extra randomness.final <T extends Retryer.Delay<?>>
List<T> Returns a view oflist
that while not modifiable, will become empty whenduration()
has elapsed since the time the view was created as if another thread had just concurrently removed all elements from it.final <T extends Retryer.Delay<?>>
List<T> Returns a view oflist
that while not modifiable, will become empty whenduration()
has elapsed since the time the view was created as if another thread had just concurrently removed all elements from it.toString()
-
Constructor Details
-
Delay
public Delay()
-
-
Method Details
-
duration
Returns the delay interval. -
ofMillis
Shorthand forof(Duration.ofMillis(millis))
.- Parameters:
millis
- must not be negative
-
of
Returns aDelay
ofduration
.- Parameters:
duration
- must not be negative
-
timed
Returns a view oflist
that while not modifiable, will become empty whenduration()
has elapsed since the time the view was created as if another thread had just concurrently removed all elements from it.Useful for setting a retry deadline to avoid long response time. For example:
Delay<?> deadline = Delay.ofMillis(500); new Retryer() .upon(RpcException.class, deadline.timed(Delay.ofMillis(30).exponentialBackoff(2, 5), clock)) .retry(this::getAccount, executor);
The returned
List
view's state is dependent on the current time. Beware of copying the list, because when you do, time is frozen as far as the copy is concerned. Passing the copy toupon()
no longer respects "timed" semantics.Note that if the timed deadline would have been exceeded after the current delay, that delay will be considered "removed" and hence cause the retry to stop.
clock
is used to measure time. -
timed
Returns a view oflist
that while not modifiable, will become empty whenduration()
has elapsed since the time the view was created as if another thread had just concurrently removed all elements from it.Useful for setting a retry deadline to avoid long response time. For example:
Delay<?> deadline = Delay.ofMillis(500); new Retryer() .upon(RpcException.class, deadline.timed(Delay.ofMillis(30).exponentialBackoff(2, 5))) .retry(this::getAccount, executor);
The returned
List
view's state is dependent on the current time. Beware of copying the list, because when you do, time is frozen as far as the copy is concerned. Passing the copy toupon()
no longer respects "timed" semantics.Note that if the timed deadline would have been exceeded after the current delay, that delay will be considered "removed" and hence cause the retry to stop.
-
exponentialBackoff
Returns an immutableList
of delays withsize
. The first delay (ifsize > 0
) isthis
and the following delays are exponentially multiplied usingmultiplier
.- Parameters:
multiplier
- must be positivesize
- must not be negative
-
multipliedBy
Returns a newDelay
with duration multiplied bymultiplier
.- Parameters:
multiplier
- must not be negative
-
randomized
Returns a newDelay
with some extra randomness. To randomize a list ofDelay
s, for example:Random random = new Random(); List<Delay> randomized = Delay.ofMillis(100).exponentialBackoff(2, 5).stream() .map(d -> d.randomized(random, 0.5)) .collect(toList());
- Parameters:
random
- random generatorrandomness
- Must be in the range of [0, 1]. 0 means no randomness; and 1 means the delay randomly ranges from 0x to 2x.
-
fibonacci
Returns a fibonacci list of delays ofsize
, as in1, 1, 2, 3, 5, 8, ...
withthis
delay being the multiplier. -
beforeDelay
Called ifevent
will be retried after the delay. Logs the event by default. -
afterDelay
Called after the delay, immediately before the retry. Logs the event by default. -
compareTo
- Specified by:
compareTo
in interfaceComparable<E>
-
equals
-
hashCode
public int hashCode() -
toString
-