Skip to content

Commit

Permalink
Add a variable interval when retrying, to prevent stampeding
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Dec 31, 2024
1 parent ab94e8d commit 97b9c5c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions modules/build/src/main/scala/scala/build/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package scala
import scala.annotation.tailrec
import scala.concurrent.duration.DurationConversions.*
import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.util.Random

package object build {
def retry[T](
maxAttempts: Int = 3,
waitDuration: FiniteDuration = 1.seconds
waitDuration: FiniteDuration = 1.seconds,
variableWaitDelayInMs: Int = 500
)(logger: Logger)(
run: => T
): T = {
Expand All @@ -21,7 +23,9 @@ package object build {
else {
t.getStackTrace.foreach(ste => logger.debug(ste.toString))
logger.log(s"Caught $t, trying again in $waitDuration")
Thread.sleep(waitDuration.toMillis)
val variableDelay = Random.between(0, variableWaitDelayInMs + 1).milliseconds
val currentWaitDuration = waitDuration + variableDelay
Thread.sleep(currentWaitDuration.toMillis)
helper(count + 1)
}
}
Expand Down

0 comments on commit 97b9c5c

Please sign in to comment.