@@ -2,76 +2,77 @@ package util.retry.blocking
22
33import util .retry .blocking .RetryStrategy .RetryStrategyProducer
44
5+ import scala .concurrent .{ExecutionContext , Future }
56import scala .util .Try
67import scala .util .control .NonFatal
78
89/**
9- * The `Retry` type represents a computation that is retrying itself in case of an exception. It uses a `RetryStrategy`
10- * as a policy for the retry operation.
11- *
12- * The result may be successful consisting of the computation result value or a failure that
13- * is wrapping the underlying exception. The type is similar to the scala [[scala.util.Try ]] type.
14- *
15- * Example:
16- * {{{
17- * import scala.concurrent.duration._
18- * import util.retry.blocking.{RetryStrategy, Failure, Retry, Success}
19- *
20- * implicit val retryStrategy =
21- * RetryStrategy.fixedBackOff(retryDuration = 1.seconds, maxAttempts = 2)
22- *
23- * val r = Retry(1 / 1) match {
24- * case Success(x) => x
25- * case Failure(t) => log("Exception occurred", t)
26- * }
27- * }}}
28- */
10+ * The `Retry` type represents a computation that is retrying itself in case of an exception. It uses a `RetryStrategy`
11+ * as a policy for the retry operation.
12+ *
13+ * The result may be successful consisting of the computation result value or a failure that
14+ * is wrapping the underlying exception. The type is similar to the scala [[scala.util.Try ]] type.
15+ *
16+ * Example:
17+ * {{{
18+ * import scala.concurrent.duration._
19+ * import util.retry.blocking.{RetryStrategy, Failure, Retry, Success}
20+ *
21+ * implicit val retryStrategy =
22+ * RetryStrategy.fixedBackOff(retryDuration = 1.seconds, maxAttempts = 2)
23+ *
24+ * val r = Retry(1 / 1) match {
25+ * case Success(x) => x
26+ * case Failure(t) => log("Exception occurred", t)
27+ * }
28+ * }}}
29+ */
2930sealed trait Retry [+ T ] {
3031
3132 /**
32- * Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
33- */
33+ * Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
34+ */
3435 def flatMap [S ](f : T => Retry [S ]): Retry [S ]
3536
3637 /**
37- * Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
38- */
38+ * Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
39+ */
3940 def map [S ](f : T => S )(implicit strategy : RetryStrategyProducer ): Retry [S ]
4041
4142 /**
42- * Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
43- */
43+ * Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
44+ */
4445 def isFailure : Boolean
4546
4647 /**
47- * Returns `true` if the `Retry` is a `Success` otherwise it returns `false` otherwise.
48- */
48+ * Returns `true` if the `Retry` is a `Success` otherwise it returns `false` otherwise.
49+ */
4950 def isSuccess : Boolean
5051
5152 /**
52- * Returns the computation value in case of a `Success`.
53- * In case of a `Failure` it throws the underlying exception.
54- */
53+ * Returns the computation value in case of a `Success`.
54+ * In case of a `Failure` it throws the underlying exception.
55+ */
5556 def get : T
5657
5758 /**
58- * Returns the computation value in case of a `Success`. Otherwise it is returning the provided default.
59- */
59+ * Returns the computation value in case of a `Success`. Otherwise it is returning the provided default.
60+ */
6061 def getOrElse [U >: T ](default : => U ): U = if (isSuccess) get else default
6162
6263 /**
63- * Applies the given function `f` if this is a `Success`, otherwise returns `Unit` if this is a `Failure`.
64- */
64+ * Applies the given function `f` if this is a `Success`, otherwise returns `Unit` if this is a `Failure`.
65+ */
6566 def foreach [X ](f : T => X )
6667
6768 /**
68- * Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
69- */
69+ * Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
70+ */
7071 def recover [X >: T ](f : PartialFunction [Throwable , X ]): Retry [X ]
7172
7273 /**
73- * Transforms the `Retry` value by applying a transformation function to its underlying value
74- */
74+ * Transforms the `Retry` value by applying a transformation function to its underlying value
75+ */
7576 def transform [X ](f : T => X ): X
7677}
7778
@@ -85,7 +86,7 @@ final case class Success[+T](value: T) extends Retry[T] {
8586 override def transform [X ](f : T => X ): X = f(value)
8687 override def flatMap [S ](f : T => Retry [S ]): Retry [S ] = f(value)
8788 override def map [S ](f : T => S )(
88- implicit strategy : RetryStrategyProducer ): Retry [S ] = Retry (f(value))
89+ implicit strategy : RetryStrategyProducer ): Retry [S ] = Retry (f(value))
8990}
9091
9192final case class Failure [+ T ](exception : Throwable ) extends Retry [T ] {
@@ -106,20 +107,18 @@ final case class Failure[+T](exception: Throwable) extends Retry[T] {
106107 override def transform [X ](f : T => X ): X = throw exception
107108 override def flatMap [S ](f : T => Retry [S ]): Retry [S ] = Failure (exception)
108109 override def map [S ](f : T => S )(
109- implicit strategy : RetryStrategyProducer ): Retry [S ] = Failure (exception)
110+ implicit strategy : () => RetryStrategy ): Retry [S ] = Failure (exception)
110111}
111112
112113object Retry {
113- def apply [T ](fn : => T )(implicit strategy : RetryStrategyProducer ): Retry [T ] = {
114+ def apply [T ](fn : => T )(implicit strategy : () => RetryStrategy ): Retry [T ] = {
114115 def go (fn : => T )(strategy : RetryStrategy ): Retry [T ] = {
115116 Try (fn) match {
116117 case x : scala.util.Success [T ] =>
117118 Success (x.value)
118119 case _ if strategy.shouldRetry() =>
119- println(" retrying" )
120120 go(fn)(strategy.update())
121121 case f : scala.util.Failure [T ] =>
122- println(" giving up" )
123122 Failure (f.exception)
124123 }
125124 }
0 commit comments