Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I am trying to do this, but I'm getting a compile error:
- import odelay.Timer.default
- val future = retry.Backoff(4, 1.second) {
- () => someFuture
- }
- //compiler error:
- Error:(83, 12) type mismatch;
- found : () => scala.concurrent.Future[play.api.libs.ws.WSResponse]
- required: odelay.Timer
- () => webRequest(serviceName, wsClient)
- ^
- // providing the implicit param explicitly does compile:
- val timer = odelay.Timer.default
- val future = retry.Backoff(4, 1.second)(timer) {
- () => someFuture
- }
- // here is the source for Backoff:
- object Backoff {
- /** Retry with exponential backoff forever */
- def forever(delay: FiniteDuration = Defaults.delay, base: Int = 2)
- (implicit timer: Timer): Policy =
- new Policy {
- def apply[T]
- (promise: () => Future[T])
- (implicit success: Success[T],
- executor: ExecutionContext): Future[T] = {
- def run(delay: FiniteDuration): Future[T] = retry(promise, { () =>
- Delay(delay) {
- run(Duration(delay.length * base, delay.unit))
- }.future.flatMap(identity)
- })
- run(delay)
- }
- }
- /** Retry with exponential backoff for a max number of times */
- def apply(
- max: Int = 8,
- delay: FiniteDuration = Defaults.delay,
- base: Int = 2)
- (implicit timer: Timer): Policy =
- new CountingPolicy {
- def apply[T]
- (promise: () => Future[T])
- (implicit success: Success[T],
- executor: ExecutionContext): Future[T] = {
- def run(max: Int, delay: FiniteDuration): Future[T] = countdown(
- max, promise,
- count => Delay(delay) {
- run(count, Duration(delay.length * base, delay.unit))
- }.future.flatMap(identity))
- run(max, delay)
- }
- }
- }
- // The docs say just import odelay.Timer.default, which reads:
- /** Defines default configurations for timers */
- object Timer {
- implicit val default: Timer = jdk.JdkTimer.newTimer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement