-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Rerunnable: Sync + Clock + MonadError
- Loading branch information
Showing
14 changed files
with
159 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
effect/src/main/scala/io/catbird/util/effect/FutureInstances.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
effect/src/main/scala/io/catbird/util/effect/RerunnableClock.scala
This file was deleted.
Oops, something went wrong.
73 changes: 0 additions & 73 deletions
73
effect/src/main/scala/io/catbird/util/effect/RerunnableContextShift.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
effect/src/main/scala/io/catbird/util/effect/RerunnableTimer.scala
This file was deleted.
Oops, something went wrong.
42 changes: 20 additions & 22 deletions
42
effect/src/main/scala/io/catbird/util/effect/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,51 @@ | ||
package io.catbird.util | ||
|
||
import cats.effect.{ Async, ContextShift, ExitCase, IO } | ||
import cats.effect.{ Async, IO } | ||
import com.twitter.util.{ Future, Return, Throw, Try } | ||
|
||
import scala.None | ||
import java.lang.Throwable | ||
|
||
import cats.effect.Outcome | ||
import cats.effect.kernel.Resource.ExitCase | ||
|
||
import scala.util.{ Left, Right } | ||
|
||
package object effect extends FutureInstances with RerunnableInstances { | ||
|
||
/** | ||
* Converts the `Future` to `F` without changing the underlying execution (same thread pool!). | ||
* Converts the `Future` to `F`. | ||
*/ | ||
def futureToAsync[F[_], A](fa: => Future[A])(implicit F: Async[F]): F[A] = F.async { k => | ||
fa.respond { | ||
case Return(a) => k(Right[Throwable, A](a)) | ||
case Throw(err) => k(Left[Throwable, A](err)) | ||
} | ||
} | ||
|
||
/** | ||
* The same as `futureToAsync` but doesn't stay on the thread pool of the `Future` and instead shifts execution | ||
* back to the one provided by `ContextShift[F]` (which is usually the default one). | ||
* | ||
* This is likely what you want when you interact with libraries that return a `Future` like `finagle-http` where | ||
* the `Future` is running on a thread pool controlled by the library (e.g. the underlying Netty pool). | ||
* It also is closer to the behavior of `IO.fromFuture` for Scala futures which also shifts back. | ||
*/ | ||
def futureToAsyncAndShift[F[_], A](fa: => Future[A])(implicit F: Async[F], CS: ContextShift[F]): F[A] = | ||
F.guarantee(futureToAsync[F, A](fa))(CS.shift) | ||
F.pure(None) // No cancel token | ||
} | ||
|
||
/** | ||
* Converts the `Rerunnable` to `F` without changing the underlying execution (same thread pool!). | ||
* Converts the `Rerunnable` to `IO`. | ||
*/ | ||
final def rerunnableToIO[A](fa: Rerunnable[A]): IO[A] = | ||
futureToAsync[IO, A](fa.run) | ||
|
||
/** | ||
* The same as `rerunnableToIO` but doesn't stay on the thread pool of the `Rerunnable` and instead shifts execution | ||
* back to the one provided by `ContextShift[F]` (which is usually the default one). | ||
* Convert a twitter-util Try to cats-effect ExitCase | ||
*/ | ||
final def rerunnableToIOAndShift[A](fa: Rerunnable[A])(implicit CS: ContextShift[IO]): IO[A] = | ||
futureToAsyncAndShift[IO, A](fa.run) | ||
final def tryToExitCase[A](ta: Try[A]): ExitCase = | ||
ta match { | ||
case Return(_) => ExitCase.Succeeded | ||
case Throw(e) => ExitCase.Errored(e) | ||
} | ||
|
||
/** | ||
* Convert a twitter-util Try to cats-effect ExitCase | ||
* Convert a twitter-util Try to cats-effect Outcome | ||
*/ | ||
final def tryToExitCase[A](ta: Try[A]): ExitCase[Throwable] = | ||
final def tryToRerunnableOutcome[A](ta: Try[A]): Outcome[Rerunnable, Throwable, A] = | ||
ta match { | ||
case Return(_) => ExitCase.complete | ||
case Throw(e) => ExitCase.error(e) | ||
case Return(a) => Outcome.Succeeded(Rerunnable.const(a)) | ||
case Throw(e) => Outcome.Errored(e) | ||
} | ||
} |
Oops, something went wrong.