From e0ceb71ccd2f7c55d1fc4d1b0b58f6631d08c04f Mon Sep 17 00:00:00 2001 From: Aleh Reishal Date: Tue, 8 Sep 2020 13:38:32 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20calculating=20backoff=20delay=20in=20orde?= =?UTF-8?q?r=20to=20prevent=20IllegalArgumentExce=E2=80=A6=20(#29569)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix calculating backoff delay in order to prevent IllegalArgumentException #29568 * Fix calculating backoff delay in order to prevent IllegalArgumentException #29568 / remove BackoffCalculatorSpec due to it is difficult to make calculateDelay testable and not tp break binary backward compatibility --- .../akka/actor/typed/internal/Supervision.scala | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Supervision.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Supervision.scala index 506c8b1966..5aa31b292b 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Supervision.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/Supervision.scala @@ -12,9 +12,7 @@ import scala.concurrent.duration.FiniteDuration import scala.reflect.ClassTag import scala.util.control.Exception.Catcher import scala.util.control.NonFatal - import org.slf4j.event.Level - import akka.actor.DeadLetterSuppression import akka.actor.Dropped import akka.actor.typed.BehaviorInterceptor.PreStartTarget @@ -28,6 +26,8 @@ import akka.event.Logging import akka.util.OptionVal import akka.util.unused +import scala.util.Try + /** * INTERNAL API */ @@ -171,13 +171,11 @@ private object RestartSupervisor { maxBackoff: FiniteDuration, randomFactor: Double): FiniteDuration = { val rnd = 1.0 + ThreadLocalRandom.current().nextDouble() * randomFactor - if (restartCount >= 30) // Duration overflow protection (> 100 years) - maxBackoff - else - maxBackoff.min(minBackoff * math.pow(2, restartCount)) * rnd match { - case f: FiniteDuration => f - case _ => maxBackoff - } + val calculatedDuration = Try(maxBackoff.min(minBackoff * math.pow(2, restartCount)) * rnd).getOrElse(maxBackoff) + calculatedDuration match { + case f: FiniteDuration => f + case _ => maxBackoff + } } final case class ScheduledRestart(owner: RestartSupervisor[_, _ <: Throwable]) extends DeadLetterSuppression