Scala retry with capped exponential backoff

Scala
import scala.util.Random

// Retries with capped exponential delays and jitter.
def retryWithBackoff[A](maxAttempts: Int, baseMs: Long, capMs: Long)(op: => A): A =
  def attempt(n: Int, delay: Long): A =
    try op
    catch case e: Exception =>
      if n >= maxAttempts then throw e
      val capped = math.min(capMs, delay)
      val jitter = Random.nextLong(capped / 2 + 1)
      Thread.sleep(capped / 2 + jitter)
      attempt(n + 1, math.min(capMs, delay * 2))
  attempt(1, math.max(1L, baseMs))