Created
November 5, 2017 17:19
-
-
Save amarrella/036065eab69af2f272058e454ae262ed to your computer and use it in GitHub Desktop.
Timeout fs2 task
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
import fs2.Task | |
import scala.concurrent.duration.FiniteDuration | |
object TaskWithTimeout { | |
implicit class TaskWithTimeout[A](x: Task[A]) { | |
/** | |
* If the main task fails or times out, it returns the default value | |
*/ | |
def timeout(default: A, duration: FiniteDuration)(implicit S:fs2.Strategy, scheduler:fs2.Scheduler):Task[A] = | |
fs2.Stream | |
.eval(x.attempt) | |
.mergeHaltBoth( | |
fs2.time.sleep_[Task](duration) ++ fs2.Stream.eval(Task.now(default)).attempt | |
).runLog.map(_.head.toOption.getOrElse(default)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment