-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from d1skort/timeout-docs
add timeout docs
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
id: timeout | ||
title: Timeout | ||
--- | ||
|
||
A simple typeclass, which allows you to timeout any process. | ||
|
||
```scala mdoc | ||
import cats.Monad | ||
import cats.effect.Timer | ||
import monix.eval.Task | ||
import monix.execution.Scheduler.Implicits.global | ||
|
||
import tofu.{ Timeout, Raise } | ||
import tofu.syntax.monadic._ | ||
|
||
import scala.concurrent.duration._ | ||
|
||
|
||
def computeValue[F[_]: Monad](implicit timer: Timer[F]): F[String] = | ||
timer.sleep(3.seconds) >> "value".pure[F] | ||
|
||
def program[F[_]: Monad: Timeout: Timer]: F[String] = | ||
Timeout[F].timeoutTo(computeValue, 1.seconds, "fallback".pure[F]) | ||
|
||
program[Task].runSyncUnsafe(Duration.Inf) | ||
``` | ||
|
||
Timeout comes with handy syntax: | ||
|
||
```scala mdoc | ||
import tofu.syntax.timeout._ | ||
|
||
def program1[F[_]: Monad: Timeout: Timer]: F[Option[String]] = | ||
computeValue.timeout(1.seconds) | ||
|
||
def program2[F[_]: Monad: Timeout: Timer]: F[String] = | ||
computeValue.timeoutOr(1.seconds, "fallback") | ||
|
||
def program3[F[_]: Monad: Raise[*[_], Exception]: Timeout: Timer]: F[String] = | ||
computeValue.timeoutRaise(1.seconds, new Exception("boom")) | ||
|
||
program1[Task].runSyncUnsafe(Duration.Inf) | ||
|
||
program2[Task].runSyncUnsafe(Duration.Inf) | ||
|
||
program3[Task].onErrorHandle(_ => "fallback").runSyncUnsafe(Duration.Inf) | ||
``` |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"ids": [ | ||
"errors", | ||
"fork", | ||
"timeout", | ||
"typeclasses" | ||
] | ||
}, | ||
|