Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.02 KB | None | 0 0
  1. // I am trying to do this, but I'm getting a compile error:
  2. import odelay.Timer.default
  3. val future = retry.Backoff(4, 1.second) {
  4.   () => someFuture
  5. }
  6.  
  7. //compiler error:
  8. Error:(83, 12) type mismatch;
  9.  found   : () => scala.concurrent.Future[play.api.libs.ws.WSResponse]
  10.  required: odelay.Timer
  11.         () => webRequest(serviceName, wsClient)
  12.            ^
  13.  
  14.  
  15.  
  16.  
  17. // providing the implicit param explicitly does compile:
  18. val timer = odelay.Timer.default
  19. val future = retry.Backoff(4, 1.second)(timer) {
  20.   () => someFuture
  21. }
  22.  
  23.  
  24. // here is the source for Backoff:
  25. object Backoff {
  26.  
  27.   /** Retry with exponential backoff forever */
  28.   def forever(delay: FiniteDuration = Defaults.delay, base: Int = 2)
  29.    (implicit timer: Timer): Policy =
  30.     new Policy {
  31.       def apply[T]
  32.         (promise: () => Future[T])
  33.         (implicit success: Success[T],
  34.          executor: ExecutionContext): Future[T] = {
  35.           def run(delay: FiniteDuration): Future[T] = retry(promise, { () =>
  36.             Delay(delay) {
  37.               run(Duration(delay.length * base, delay.unit))
  38.             }.future.flatMap(identity)
  39.           })
  40.           run(delay)
  41.         }
  42.     }
  43.  
  44.   /** Retry with exponential backoff for a max number of times */
  45.   def apply(
  46.     max: Int = 8,
  47.     delay: FiniteDuration = Defaults.delay,
  48.     base: Int = 2)
  49.    (implicit timer: Timer): Policy =
  50.     new CountingPolicy {
  51.       def apply[T]
  52.         (promise: () => Future[T])
  53.         (implicit success: Success[T],
  54.          executor: ExecutionContext): Future[T] = {
  55.           def run(max: Int, delay: FiniteDuration): Future[T] = countdown(
  56.             max, promise,
  57.             count => Delay(delay) {
  58.               run(count, Duration(delay.length * base, delay.unit))
  59.             }.future.flatMap(identity))
  60.           run(max, delay)
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66.  
  67.      
  68. // The docs say just import odelay.Timer.default, which reads:
  69.  
  70. /** Defines default configurations for timers */
  71. object Timer {
  72.   implicit val default: Timer = jdk.JdkTimer.newTimer
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement