Custom retry in Reactor
up vote
0
down vote
favorite
I was trying to implement a retry logic in Kotlin and Reactor based on the Reactor extra package's features. What I'm trying to do is pass a list of durations, and on each context.iteration
I'm getting the (iteration-1)th element of the list. It works partly, I'm always getting an IndexOutOfBoundsException
on the last iteration, which is more than I wanted, although I've provided a max number of retries - the size of the list. The retries are running though, in the given duration and "correct" number of times (surely because IndexOutOfBoundsException
prevents more), only this exception (and it's root cause) bothers me.
This is my custom BackOff interface:
interface MyCustomBackoff : Backoff
companion object
fun getBackoffDelay(backoffList: List<Duration>): (IterationContext<*>) -> BackoffDelay
return context -> BackoffDelay(backoffList[(context.iteration() - 1).toInt()])
And my Kotlin extension is:
fun <T> Mono<T>.retryCustomBackoffs(backoffList: List<Duration>, doOnRetry: ((RetryContext<T>) -> Unit)? = null): Mono<T>
val retry = Retry.any<T>().retryMax(backoffList.size.toLong()).backoff(MyCustomBackoff.getBackoffDelay(backoffList))
return if (doOnRetry == null)
this.retryWhen(retry)
else
this.retryWhen(retry.doOnRetry(doOnRetry))
What am I missing here?
kotlin spring-webflux project-reactor retry-logic
add a comment |
up vote
0
down vote
favorite
I was trying to implement a retry logic in Kotlin and Reactor based on the Reactor extra package's features. What I'm trying to do is pass a list of durations, and on each context.iteration
I'm getting the (iteration-1)th element of the list. It works partly, I'm always getting an IndexOutOfBoundsException
on the last iteration, which is more than I wanted, although I've provided a max number of retries - the size of the list. The retries are running though, in the given duration and "correct" number of times (surely because IndexOutOfBoundsException
prevents more), only this exception (and it's root cause) bothers me.
This is my custom BackOff interface:
interface MyCustomBackoff : Backoff
companion object
fun getBackoffDelay(backoffList: List<Duration>): (IterationContext<*>) -> BackoffDelay
return context -> BackoffDelay(backoffList[(context.iteration() - 1).toInt()])
And my Kotlin extension is:
fun <T> Mono<T>.retryCustomBackoffs(backoffList: List<Duration>, doOnRetry: ((RetryContext<T>) -> Unit)? = null): Mono<T>
val retry = Retry.any<T>().retryMax(backoffList.size.toLong()).backoff(MyCustomBackoff.getBackoffDelay(backoffList))
return if (doOnRetry == null)
this.retryWhen(retry)
else
this.retryWhen(retry.doOnRetry(doOnRetry))
What am I missing here?
kotlin spring-webflux project-reactor retry-logic
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was trying to implement a retry logic in Kotlin and Reactor based on the Reactor extra package's features. What I'm trying to do is pass a list of durations, and on each context.iteration
I'm getting the (iteration-1)th element of the list. It works partly, I'm always getting an IndexOutOfBoundsException
on the last iteration, which is more than I wanted, although I've provided a max number of retries - the size of the list. The retries are running though, in the given duration and "correct" number of times (surely because IndexOutOfBoundsException
prevents more), only this exception (and it's root cause) bothers me.
This is my custom BackOff interface:
interface MyCustomBackoff : Backoff
companion object
fun getBackoffDelay(backoffList: List<Duration>): (IterationContext<*>) -> BackoffDelay
return context -> BackoffDelay(backoffList[(context.iteration() - 1).toInt()])
And my Kotlin extension is:
fun <T> Mono<T>.retryCustomBackoffs(backoffList: List<Duration>, doOnRetry: ((RetryContext<T>) -> Unit)? = null): Mono<T>
val retry = Retry.any<T>().retryMax(backoffList.size.toLong()).backoff(MyCustomBackoff.getBackoffDelay(backoffList))
return if (doOnRetry == null)
this.retryWhen(retry)
else
this.retryWhen(retry.doOnRetry(doOnRetry))
What am I missing here?
kotlin spring-webflux project-reactor retry-logic
I was trying to implement a retry logic in Kotlin and Reactor based on the Reactor extra package's features. What I'm trying to do is pass a list of durations, and on each context.iteration
I'm getting the (iteration-1)th element of the list. It works partly, I'm always getting an IndexOutOfBoundsException
on the last iteration, which is more than I wanted, although I've provided a max number of retries - the size of the list. The retries are running though, in the given duration and "correct" number of times (surely because IndexOutOfBoundsException
prevents more), only this exception (and it's root cause) bothers me.
This is my custom BackOff interface:
interface MyCustomBackoff : Backoff
companion object
fun getBackoffDelay(backoffList: List<Duration>): (IterationContext<*>) -> BackoffDelay
return context -> BackoffDelay(backoffList[(context.iteration() - 1).toInt()])
And my Kotlin extension is:
fun <T> Mono<T>.retryCustomBackoffs(backoffList: List<Duration>, doOnRetry: ((RetryContext<T>) -> Unit)? = null): Mono<T>
val retry = Retry.any<T>().retryMax(backoffList.size.toLong()).backoff(MyCustomBackoff.getBackoffDelay(backoffList))
return if (doOnRetry == null)
this.retryWhen(retry)
else
this.retryWhen(retry.doOnRetry(doOnRetry))
What am I missing here?
kotlin spring-webflux project-reactor retry-logic
kotlin spring-webflux project-reactor retry-logic
edited Nov 12 at 12:07
asked Nov 10 at 20:46
Peter
104215
104215
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
If you look at reactor.retry.AbstractRetry#calculateBackoff
, you could find that there is special BackoffDelay
named RETRY_EXHAUSTED
. And it returns when retryContext.iteration() > maxIterations
(not >=
) after backoff.apply(retryContext)
if (retryContext.iteration() > maxIterations || Instant.now(clock).plus(jitteredBackoff).isAfter(timeoutInstant))
return RETRY_EXHAUSTED;
So, if you have 2 custom backoff delays in the list, there will be 3 backoff delays generated by calculateBackoff
.
You could change your MyCustomBackoff
like so (excuse me for Java, I'm not familiar with Kotlin):
public interface MyCustomBackoff extends Backoff
static Backoff getBackoffDelay(List<Duration> backoffList)
return context -> context.iteration() <= backoffList.size() ?
new BackoffDelay(backoffList.get(Long.valueOf(context.iteration() - 1).intValue())) :
new BackoffDelay(Duration.ZERO);
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Actualy, it looks like issue. I thinkcalculateBackoff
should check exhausted condition then callbackoff.apply(retryContext)
. I'll ask about it on Github.
– Alexander Pankin
Nov 12 at 19:57
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If you look at reactor.retry.AbstractRetry#calculateBackoff
, you could find that there is special BackoffDelay
named RETRY_EXHAUSTED
. And it returns when retryContext.iteration() > maxIterations
(not >=
) after backoff.apply(retryContext)
if (retryContext.iteration() > maxIterations || Instant.now(clock).plus(jitteredBackoff).isAfter(timeoutInstant))
return RETRY_EXHAUSTED;
So, if you have 2 custom backoff delays in the list, there will be 3 backoff delays generated by calculateBackoff
.
You could change your MyCustomBackoff
like so (excuse me for Java, I'm not familiar with Kotlin):
public interface MyCustomBackoff extends Backoff
static Backoff getBackoffDelay(List<Duration> backoffList)
return context -> context.iteration() <= backoffList.size() ?
new BackoffDelay(backoffList.get(Long.valueOf(context.iteration() - 1).intValue())) :
new BackoffDelay(Duration.ZERO);
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Actualy, it looks like issue. I thinkcalculateBackoff
should check exhausted condition then callbackoff.apply(retryContext)
. I'll ask about it on Github.
– Alexander Pankin
Nov 12 at 19:57
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
add a comment |
up vote
1
down vote
accepted
If you look at reactor.retry.AbstractRetry#calculateBackoff
, you could find that there is special BackoffDelay
named RETRY_EXHAUSTED
. And it returns when retryContext.iteration() > maxIterations
(not >=
) after backoff.apply(retryContext)
if (retryContext.iteration() > maxIterations || Instant.now(clock).plus(jitteredBackoff).isAfter(timeoutInstant))
return RETRY_EXHAUSTED;
So, if you have 2 custom backoff delays in the list, there will be 3 backoff delays generated by calculateBackoff
.
You could change your MyCustomBackoff
like so (excuse me for Java, I'm not familiar with Kotlin):
public interface MyCustomBackoff extends Backoff
static Backoff getBackoffDelay(List<Duration> backoffList)
return context -> context.iteration() <= backoffList.size() ?
new BackoffDelay(backoffList.get(Long.valueOf(context.iteration() - 1).intValue())) :
new BackoffDelay(Duration.ZERO);
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Actualy, it looks like issue. I thinkcalculateBackoff
should check exhausted condition then callbackoff.apply(retryContext)
. I'll ask about it on Github.
– Alexander Pankin
Nov 12 at 19:57
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If you look at reactor.retry.AbstractRetry#calculateBackoff
, you could find that there is special BackoffDelay
named RETRY_EXHAUSTED
. And it returns when retryContext.iteration() > maxIterations
(not >=
) after backoff.apply(retryContext)
if (retryContext.iteration() > maxIterations || Instant.now(clock).plus(jitteredBackoff).isAfter(timeoutInstant))
return RETRY_EXHAUSTED;
So, if you have 2 custom backoff delays in the list, there will be 3 backoff delays generated by calculateBackoff
.
You could change your MyCustomBackoff
like so (excuse me for Java, I'm not familiar with Kotlin):
public interface MyCustomBackoff extends Backoff
static Backoff getBackoffDelay(List<Duration> backoffList)
return context -> context.iteration() <= backoffList.size() ?
new BackoffDelay(backoffList.get(Long.valueOf(context.iteration() - 1).intValue())) :
new BackoffDelay(Duration.ZERO);
If you look at reactor.retry.AbstractRetry#calculateBackoff
, you could find that there is special BackoffDelay
named RETRY_EXHAUSTED
. And it returns when retryContext.iteration() > maxIterations
(not >=
) after backoff.apply(retryContext)
if (retryContext.iteration() > maxIterations || Instant.now(clock).plus(jitteredBackoff).isAfter(timeoutInstant))
return RETRY_EXHAUSTED;
So, if you have 2 custom backoff delays in the list, there will be 3 backoff delays generated by calculateBackoff
.
You could change your MyCustomBackoff
like so (excuse me for Java, I'm not familiar with Kotlin):
public interface MyCustomBackoff extends Backoff
static Backoff getBackoffDelay(List<Duration> backoffList)
return context -> context.iteration() <= backoffList.size() ?
new BackoffDelay(backoffList.get(Long.valueOf(context.iteration() - 1).intValue())) :
new BackoffDelay(Duration.ZERO);
edited Nov 13 at 12:55
answered Nov 12 at 11:13
Alexander Pankin
56116
56116
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Actualy, it looks like issue. I thinkcalculateBackoff
should check exhausted condition then callbackoff.apply(retryContext)
. I'll ask about it on Github.
– Alexander Pankin
Nov 12 at 19:57
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
add a comment |
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Actualy, it looks like issue. I thinkcalculateBackoff
should check exhausted condition then callbackoff.apply(retryContext)
. I'll ask about it on Github.
– Alexander Pankin
Nov 12 at 19:57
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Thank you so much, I missed that one!
– Peter
Nov 12 at 12:02
Actualy, it looks like issue. I think
calculateBackoff
should check exhausted condition then call backoff.apply(retryContext)
. I'll ask about it on Github.– Alexander Pankin
Nov 12 at 19:57
Actualy, it looks like issue. I think
calculateBackoff
should check exhausted condition then call backoff.apply(retryContext)
. I'll ask about it on Github.– Alexander Pankin
Nov 12 at 19:57
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
It seems illogical to me as well. Thanks for pursuing it!
– Peter
Nov 13 at 20:44
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243246%2fcustom-retry-in-reactor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown