tofu.Timeout.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tofu-core_2.13 Show documentation
Show all versions of tofu-core_2.13 Show documentation
Opinionated Set of tool for functional programming in scala
package tofu
import cats.effect.{Concurrent, ContextShift, IO, Timer}
import simulacrum.typeclass
import cats.syntax.apply._
import cats.syntax.functor._
import scala.concurrent.duration.FiniteDuration
@typeclass
trait Timeout[F[_]] {
def timeoutTo[A](fa: F[A], after: FiniteDuration, fallback: F[A]): F[A]
}
object Timeout {
def concurrent[F[_]](implicit F: Concurrent[F], timer: Timer[F]): Timeout[F] =
new Timeout[F] {
override def timeoutTo[A](fa: F[A], after: FiniteDuration, fallback: F[A]): F[A] =
F.race(fa, timer.sleep(after) *> fallback).map(_.merge)
}
implicit def io(implicit timer: Timer[IO], cs: ContextShift[IO]): Timeout[IO] = new Timeout[IO] {
override def timeoutTo[A](fa: IO[A], after: FiniteDuration, fallback: IO[A]): IO[A] = fa.timeoutTo(after, fallback)
}
}