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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 12:07

























      asked Nov 10 at 20:46









      Peter

      104215




      104215
























          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);
          }
          }





          share|improve this answer























          • 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










          • It seems illogical to me as well. Thanks for pursuing it!
            – Peter
            Nov 13 at 20:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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);
          }
          }





          share|improve this answer























          • 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










          • It seems illogical to me as well. Thanks for pursuing it!
            – Peter
            Nov 13 at 20:44















          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);
          }
          }





          share|improve this answer























          • 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










          • It seems illogical to me as well. Thanks for pursuing it!
            – Peter
            Nov 13 at 20:44













          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);
          }
          }





          share|improve this answer














          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);
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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


















          • 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










          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python