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

Migrate frontend to use Tapir endpoints #28

Merged
merged 9 commits into from
Sep 12, 2022
Merged

Conversation

lolgab
Copy link
Contributor

@lolgab lolgab commented Sep 11, 2022

No description provided.

Comment on lines +12 to +41
class RetryingBackend[P](
delegate: SttpBackend[Future, P]
)(using stability: Stability)
extends DelegateSttpBackend[Future, P](delegate):
import scala.concurrent.ExecutionContext.Implicits.global

private given retry.Success[
(Request[?, ?], Either[Throwable, Response[?]])
]((req, res) => !RetryWhen.Default(req, res))

// The default timer is currently broken in JS
// https://github.com/softwaremill/odelay/pull/19
private given odelay.Timer = odelay.js.JsTimer.newTimer

override def send[T, R >: P & Effect[Future]](
request: Request[T, R]
): Future[Response[T]] =
retry
.Backoff(stability.maxRetries, stability.delay)
.apply {
delegate
.send(request)
.transform(tryResponse =>
scala.util.Success((request, tryResponse.toEither))
)
}
.flatMap {
case (_, Right(response)) => Future.successful(response)
case (_, Left(exception)) => Future.failed(exception)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MIgrating api.stability to Futures and to be used in sttp wasn't trivial, so I reimplemented an exponential backoff retrying backend using retry.
I left the original file in case we want to migrate it later.

Copy link
Owner

Choose a reason for hiding this comment

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

I think it's fine to remove it completely - though I would've imagined that sttp has a retying backend built-in given that both libraries are part of softwaremillverse :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be an interesting thing to try, actually! But as far as I know, there is no retry integration for sttp.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened an issue here

@lolgab lolgab marked this pull request as ready for review September 11, 2022 08:34
build.sbt Outdated Show resolved Hide resolved
@lolgab lolgab requested a review from keynmol September 12, 2022 12:28
@lolgab
Copy link
Contributor Author

lolgab commented Sep 12, 2022

Good to go from my side

Copy link
Owner

@keynmol keynmol left a comment

Choose a reason for hiding this comment

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

I had a couple of nitpicks but I will fix them myself - let's see if it works in production first 🎉

@keynmol keynmol merged commit 863bf6f into keynmol:main Sep 12, 2022
@lolgab lolgab deleted the tapir-frontend branch September 12, 2022 16:47
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

Successfully merging this pull request may close these issues.

2 participants