Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given this:
- def shouldRetry(res: Response): Boolean = ??? // Some responses indicate that a request
- def rawCall(reqs: Seq[Request]): Seq[Response] = ??? // should be retried.
- val MaxRetryLimit: Int = ???
- // Perform this:
- def callWithRetries(reqs: Seq[Request], retryCount: Int = 0): Seq[Response] = {
- val results = rawCall(reqs)
- val retryIndices = results.zipWithIndex.collect { case (res, i) if shouldRetry(res) => i }
- if (retryIndices.isEmpty || retryCount == MaxRetryLimit) {
- results
- } else {
- // Bookkeeping
- val retries = retryIndices.map(reqs)
- val retryResultMap = retryIndices.zip(callWithRetries(retries, retryCount + 1)).toMap
- results.indices.map { i =>
- retryResultMap.get(i) match {
- case Some(res) => res
- case None => results(i)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement