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

add timeout docs #327

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/timeout.md
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)
```
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ids": [
"errors",
"fork",
"timeout",
"typeclasses"
]
},
Expand Down