Skip to content

Commit 6c9bdaa

Browse files
committed
Add a variable interval when retrying, to prevent stampeding
1 parent ab94e8d commit 6c9bdaa

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

modules/build/src/main/scala/scala/build/package.scala

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package scala
33
import scala.annotation.tailrec
44
import scala.concurrent.duration.DurationConversions.*
55
import scala.concurrent.duration.{DurationInt, FiniteDuration}
6+
import scala.util.Random
67

78
package object build {
89
def retry[T](
910
maxAttempts: Int = 3,
10-
waitDuration: FiniteDuration = 1.seconds
11+
waitDuration: FiniteDuration = 1.seconds,
12+
variableWaitDelayInMs: Int = 500
1113
)(logger: Logger)(
1214
run: => T
1315
): T = {
@@ -21,7 +23,9 @@ package object build {
2123
else {
2224
t.getStackTrace.foreach(ste => logger.debug(ste.toString))
2325
logger.log(s"Caught $t, trying again in $waitDuration")
24-
Thread.sleep(waitDuration.toMillis)
26+
val variableDelay = Random.between(0, variableWaitDelayInMs + 1).milliseconds
27+
val currentWaitDuration = waitDuration + variableDelay
28+
Thread.sleep(currentWaitDuration.toMillis)
2529
helper(count + 1)
2630
}
2731
}

0 commit comments

Comments
 (0)