-
Notifications
You must be signed in to change notification settings - Fork 423
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
Adjust Async cancellation to CE 3.5 #2897
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package sttp.tapir.server.finatra.cats | ||
|
||
import cats.effect.Async | ||
import cats.effect.{Async, Sync} | ||
import cats.syntax.all._ | ||
import cats.effect.std.Dispatcher | ||
import com.twitter.util.{Future, Promise} | ||
|
||
|
@@ -25,9 +26,10 @@ object conversions { | |
|
||
/** Convert from a Twitter Future to some F with Async capabilities. Based on https://typelevel.org/cats-effect/docs/typeclasses/async | ||
*/ | ||
private[cats] implicit class RichTwitterFuture[A](val f: Future[A]) { | ||
def asF[F[_]: Async]: F[A] = Async[F].async_ { cb => | ||
f.onSuccess(f => cb(Right(f))).onFailure(e => cb(Left(e))) | ||
private[cats] def fromFuture[F[_]: Async, A](f: => Future[A]): F[A] = Async[F].async { cb => | ||
Sync[F].delay { | ||
val fut = f.onSuccess(f => cb(Right(f))).onFailure(e => cb(Left(e))) | ||
Some(Sync[F].delay(fut.raise(new InterruptedException("Fiber canceled"))).void) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't twitter futures cancellable? (it's a distinct operation from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it was called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok thanks :) |
||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,15 +133,17 @@ object VertxCatsServerInterpreter { | |
|
||
implicit class VertxFutureToCatsF[A](f: => Future[A]) { | ||
def asF[F[_]: Async]: F[A] = { | ||
Async[F].async_ { cb => | ||
f.onComplete({ handler => | ||
if (handler.succeeded()) { | ||
cb(Right(handler.result())) | ||
} else { | ||
cb(Left(handler.cause())) | ||
} | ||
}) | ||
() | ||
Async[F].async { cb => | ||
Sync[F].delay { | ||
f.onComplete({ handler => | ||
if (handler.succeeded()) { | ||
cb(Right(handler.result())) | ||
} else { | ||
cb(Left(handler.cause())) | ||
} | ||
}) | ||
Some(().pure[F]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this fake cancellation logic - shouldn't we wait for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to wait for
If there's no way to cancel underlying future, we need to choose between keeping the previous behavior (canceling the outer fiber and leaving a leaked future) and agreeing to the new uncancellable behavior. I chose the first solution to keep consistent with previous behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, well I guess if we can't do anything, we can't :) |
||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we wait for the future, but then wait for its result, to properly implement cancellation integration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean call the
cancel
but still block until the future completes? I think the finalizer should finish as soon as it finishes its logic. Take a look at this example from TheAsync
documentation:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that the idea in cats-effect is for the finalizers to block until the effect finishes (after cancellation or not). But the example indeed suggests otherwise.
... and reading https://github.com/typelevel/cats-effect/releases/tag/v3.5.0 your code is correct :)