Skip to content

Success condition can be a failure #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
grogdotcom opened this issue Dec 9, 2019 · 4 comments
Closed

Success condition can be a failure #33

grogdotcom opened this issue Dec 9, 2019 · 4 comments

Comments

@grogdotcom
Copy link

I have a method that I want to retry that could result in multiple exceptions. Only one of them I want to retry on the other ones I want to consider as a success and throw that exception.
The Success condition for the Policy is only checked within a .map of the future being evaluated so a failed future will always result in a retry.
Is there a way to only retry on certain failures without having to wrap the content of the futures in a Try or Either?

@GEverding
Copy link

@grogdotcom did you find a solution to this? I'm trying to do the same thing.

@grogdotcom
Copy link
Author

grogdotcom commented Feb 5, 2020

@GEverding No there is no good solution. what I ended up doing was wrapping the future in a Try

def retryOnlyOnSpecificException[A, B <: Throwable: ClassTag](attempts: Int, initialDelayInMillis: Long)(f: () => Future[A])(implicit ctx: ExecutionContext): Future[A] = {
    val success: retry.Success[Try[A]] = retry.Success[Try[A]] { r =>
      r match {
        case Failure(_: B) => false
        case _ => true
      }
    }
    val retryJittered = JitterBackoff(attempts, new FiniteDuration(initialDelayInMillis, TimeUnit.MILLISECONDS))
    retryJittered(f().map(r => Success(r)).recover { case l: Throwable => Failure(l) })(success, ctx).map {
      case Success(r) => r
      case Failure(e) => throw e
    }
  }

@noahlz
Copy link

noahlz commented May 27, 2020

I also need this, would be nice for it to be built into the library.

@noahlz
Copy link

noahlz commented May 29, 2020

I'm not sure if a library change is needed, I just defined an inline Success that treated exceptions I didn't want to retry as "success" - wrapped in a scala.util.Try

  val failFastCertainErrors = retry.Success[Try[Any]] =
    retry.Success[Try[Any]]({
      case scala.util.Success(_) => true
      case scala.util.Failure(ex) => ex match {
        case _: java.sql.DataTruncation => true
        case _: java.sql.SQLIntegrityConstraintViolationException => true
        case _ => false
      }
    })

This worked fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants