Skip to content
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

Allow specifying a more concrete Throwable type for retrying #3414

Closed
wants to merge 4 commits into from

Conversation

nomisRev
Copy link
Member

@nomisRev nomisRev commented Apr 30, 2024

Small use-case I came across several times, and I stumbled across it again. This PR allows specifying concrete types for retrying. I.e.

Schedule.exponential<PSQLException>(...)
  .and(Schedule.recurs(5))
  .doWhile { e: PSQLExeption, _ -> e.isRetryable() }
  .retry {
    exposedTransaction { doDatabaseStuff() }
  }

TODO

  • Write test cases
  • Broke something, investigate

@nomisRev nomisRev requested review from serras, kyay10 and a team April 30, 2024 13:45
@nomisRev
Copy link
Member Author

nomisRev commented Apr 30, 2024

Platform declaration clash: The following declarations have the same JVM signature

Of course... 😂

Copy link
Contributor

github-actions bot commented May 10, 2024

Kover Report

File Coverage [68.39%]
arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/Schedule.kt 68.39%
Total Project Coverage 57.95%

* Returns [Either] with the fallback value if the [Schedule] is exhausted, or the successful result of [action].
*/
@JvmName("retryOrElseEitherReified")
public suspend inline fun <Input, Output, A, reified E : Throwable> Schedule<E, Output>.retryOrElseEither(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should strive to keep the inline functions to be just small wrappers over the real functions (otherwise every change in the implementation breaks binary compatibility). My suggestion is to have a version which takes a KClass<E> as additional parameter (so you don't need the reified anymore), and then have a version which simply calls that function with E::class.

* It will throw the last [E] if the [Schedule] is exhausted, and ignores the output of the [Schedule].
*/
@JvmName("retryReified")
public suspend inline fun <A, reified E : Throwable> Schedule<E, *>.retry(action: suspend () -> A): A =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I think the reverse order of type arguments would look nicer on calls.

retry<ConnectionError, _> {
  // do something
}

@@ -412,6 +417,14 @@ public value class Schedule<in Input, out Output>(public val step: ScheduleStep<
public suspend fun <A> Schedule<Throwable, *>.retry(action: suspend () -> A): A =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that the compiler is able to infer which version of retry you are calling? I'm a bit concerned about problems in resolving which one you want. Maybe we can change the other one to retryInstanceOf? This would follow the naming conventions in Kotlin (filterInstanceOf...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have tests, for this, but let's double check. I like to cover these API concerns in the tests as well, so we can also track it never breaks by accident.

@serras
Copy link
Member

serras commented May 16, 2024

Should we close this now that #3418 is merged? I don't want to step on your toes...

@nomisRev
Copy link
Member Author

Not stepping on my toes @serras 😘 Love collaborating together, the resulting Schedule is 😍

@nomisRev nomisRev closed this May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants