Advertisement
Guest User

Untitled

a guest
Jun 29th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.91 KB | None | 0 0
  1.     // Given this:
  2.     def shouldRetry(res: Response): Boolean = ???        // Some responses indicate that a request
  3.     def rawCall(reqs: Seq[Request]): Seq[Response] = ??? // should be retried.
  4.     val MaxRetryLimit: Int = ???
  5.  
  6.     // Perform this:
  7.     def callWithRetries(reqs: Seq[Request], retryCount: Int = 0): Seq[Response] = {
  8.       val results = rawCall(reqs)
  9.       val retryIndices = results.zipWithIndex.collect { case (res, i) if shouldRetry(res) => i }
  10.  
  11.       if (retryIndices.isEmpty || retryCount == MaxRetryLimit) {
  12.         results
  13.       } else {
  14.         // Bookkeeping
  15.         val retries = retryIndices.map(reqs)
  16.         val retryResultMap = retryIndices.zip(callWithRetries(retries, retryCount + 1)).toMap
  17.  
  18.         results.indices.map { i =>
  19.           retryResultMap.get(i) match {
  20.             case Some(res) => res
  21.             case None => results(i)
  22.           }
  23.         }
  24.       }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement