-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
retries.scala
79 lines (71 loc) · 2.32 KB
/
retries.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package dispatch.retry
import scala.concurrent.{Future,ExecutionContext}
import scala.concurrent.duration.Duration
import java.util.concurrent.TimeUnit
import io.netty.util.Timer
import dispatch._
/** Retry immediately after failure */
object Directly extends CountingRetry {
def apply[T](max: Int = 3)(promise: () => Future[T])
(implicit success: Success[T],
executor: ExecutionContext): Future[T] = {
retry(max, promise, success, Directly(_)(promise))
}
}
/** Retry with a pause between attempts */
object Pause extends CountingRetry {
def apply[T](max: Int = 4,
delay: Duration = Duration(500, TimeUnit.MILLISECONDS))
(promise: () => Future[T])
(implicit success: Success[T],
timer: Timer,
executor: ExecutionContext): Future[T] = {
retry(max,
promise,
success,
c => SleepFuture(delay) {
Pause(c, delay)(promise)
}.flatten)
}
}
/** Retry with exponential backoff */
object Backoff extends CountingRetry {
def apply[T](max: Int = 8,
delay: Duration = Duration(500, TimeUnit.MILLISECONDS),
base: Int = 2)
(promise: () => Future[T])
(implicit success: Success[T],
timer: Timer,
executor: ExecutionContext): Future[T] = {
retry(max,
promise,
success,
count => SleepFuture(delay) {
Backoff(count,
Duration(delay.length * base, delay.unit),
base)(promise)
}.flatten)
}
}
class Success[-T](val predicate: T => Boolean)
object Success {
implicit def either[A,B]: Success[Either[A,B]] =
new Success(_.isRight)
implicit def option[A]: Success[Option[A]] =
new Success(!_.isEmpty)
}
trait CountingRetry {
/** Applies the given function and will retry up to `max` times,
until a successful result is produced. */
protected def retry[T](max: Int,
promise: () => Future[T],
success: Success[T],
orElse: Int => Future[T]
)(implicit executor: ExecutionContext) = {
val fut = promise()
fut.flatMap { res =>
if (max < 1 || success.predicate(res)) fut
else orElse(max - 1)
}
}
}