pekko/akka-actor/src/main/scala/akka/pattern/FutureTimeoutSupport.scala

26 lines
919 B
Scala
Raw Normal View History

/**
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.pattern
import scala.concurrent.duration.Duration
2012-07-04 15:25:30 +02:00
import scala.concurrent.{ ExecutionContext, Promise, Future }
import akka.actor._
import scala.util.control.NonFatal
import scala.concurrent.duration.FiniteDuration
trait FutureTimeoutSupport {
/**
2012-07-04 15:25:30 +02:00
* Returns a [[scala.concurrent.Future]] that will be completed with the success or failure of the provided value
* after the specified duration.
*/
def after[T](duration: FiniteDuration, using: Scheduler)(value: Future[T])(implicit ec: ExecutionContext): Future[T] =
if (duration.isFinite() && duration.length < 1) {
try value catch { case NonFatal(t) Future.failed(t) }
} else {
val p = Promise[T]()
using.scheduleOnce(duration) { p completeWith { try value catch { case NonFatal(t) Future.failed(t) } } }
p.future
}
2013-01-09 01:47:48 +01:00
}