如何在 Scala 中为未来添加截止日期?
How to add a deadline to a future in Scala?
假设我有一个函数 fab: A => Future[B]
并希望它 return 一个在截止日期前完成的新未来。所以我正在写一个像这样的新函数 deadlined
def deadlined[B](fut: => Future[B], deadline: Deadline): Future[B] = ???
现在我正在使用 java.util.Timer
,但可以在评论中将 ScheduledThreadPoolExecutor
用作 . The best solution though is probably a wrapper to abstract out the scheduling implementation and mock it in tests as 。
object Deadlined {
private val timer = new java.util.Timer() // todo: replace it with a wrapper
def apply[B](fut: => Future[B], deadline: Deadline)(implicit ec: ExecutionContext): Future[B] = {
val promise = Promise[B]()
val timerTask = new java.util.TimerTask {
override def run(): Unit = promise.failure(new Exception(s"$deadline is exceeded"))
}
timer.schedule(timerTask, deadline.timeLeft.toMillis)
fut.transform { result =>
timerTask.cancel()
result match {
case Success(b) => promise.success(b)
case Failure(t) => promise.failure(t)
}
result
}
promise.future
}
}
有意义吗?我还想知道如何从对我上一个问题的回答中提取 Deadlined
和 的共同部分。
我可能会做类似于以下的事情,这样我就可以为任何 Future(YMMV、Caveat Emptor 等)添加截止日期:
import scala.concurrent.{Future, ExecutionContext}
import scala.concurrent.duration.FiniteDuration
import java.util.{Timer, TimerTask}
implicit class DeadlineFuture[T](future: Future[T]) {
def deadline(d: FiniteDuration)(implicit timer: Timer): Future[T] = {
if (future.isCompleted) future
else {
val promise = Promise[T]()
val timerTask = new TimerTask {
override def run(): Unit = promise.tryFailure(new Exception(s"$d is exceeded"))
}
timer.schedule(timerTask, d.toMillis)
future.onComplete(_ => timerTask.cancel())(ExecutionContext.parasitic)
promise.completeWith(future).future
}
}
}
// Usage:
Future.never.deadline(5.seconds).onComplete(println)
假设我有一个函数 fab: A => Future[B]
并希望它 return 一个在截止日期前完成的新未来。所以我正在写一个像这样的新函数 deadlined
def deadlined[B](fut: => Future[B], deadline: Deadline): Future[B] = ???
现在我正在使用 java.util.Timer
,但可以在评论中将 ScheduledThreadPoolExecutor
用作
object Deadlined {
private val timer = new java.util.Timer() // todo: replace it with a wrapper
def apply[B](fut: => Future[B], deadline: Deadline)(implicit ec: ExecutionContext): Future[B] = {
val promise = Promise[B]()
val timerTask = new java.util.TimerTask {
override def run(): Unit = promise.failure(new Exception(s"$deadline is exceeded"))
}
timer.schedule(timerTask, deadline.timeLeft.toMillis)
fut.transform { result =>
timerTask.cancel()
result match {
case Success(b) => promise.success(b)
case Failure(t) => promise.failure(t)
}
result
}
promise.future
}
}
有意义吗?我还想知道如何从对我上一个问题的回答中提取 Deadlined
和
我可能会做类似于以下的事情,这样我就可以为任何 Future(YMMV、Caveat Emptor 等)添加截止日期:
import scala.concurrent.{Future, ExecutionContext}
import scala.concurrent.duration.FiniteDuration
import java.util.{Timer, TimerTask}
implicit class DeadlineFuture[T](future: Future[T]) {
def deadline(d: FiniteDuration)(implicit timer: Timer): Future[T] = {
if (future.isCompleted) future
else {
val promise = Promise[T]()
val timerTask = new TimerTask {
override def run(): Unit = promise.tryFailure(new Exception(s"$d is exceeded"))
}
timer.schedule(timerTask, d.toMillis)
future.onComplete(_ => timerTask.cancel())(ExecutionContext.parasitic)
promise.completeWith(future).future
}
}
}
// Usage:
Future.never.deadline(5.seconds).onComplete(println)