pjfanning commented on code in PR #1424:
URL: https://github.com/apache/pekko/pull/1424#discussion_r1966510908


##########
actor/src/main/scala/org/apache/pekko/pattern/FutureTimeoutSupport.scala:
##########
@@ -87,4 +86,64 @@ trait FutureTimeoutSupport {
       }
       p
     }
+
+  /**
+   * Returns a [[scala.concurrent.Future]] that will be completed with a 
[[TimeoutException]]
+   * if the provided value is not completed within the specified duration.
+   */
+  def timeout[T](duration: FiniteDuration, using: Scheduler)(value: => 
Future[T])(
+      implicit ec: ExecutionContext): Future[T] = {
+    val future =
+      try value
+      catch {
+        case NonFatal(t) => Future.failed(t)
+      }
+    future.value match {
+      case Some(_) => future
+      case None => // not completed yet
+        val p = Promise[T]()
+        val timeout = using.scheduleOnce(duration) {
+          p.tryFailure(new TimeoutException(s"Timeout of $duration expired"))
+          if (future.isInstanceOf[CompletableFuture[T @unchecked]]) {
+            future.asInstanceOf[CompletableFuture[T]]
+              .toCompletableFuture
+              .cancel(true)
+          }
+        }
+        future.onComplete { result =>
+          timeout.cancel()
+          p.tryComplete(result)
+        }(pekko.dispatch.ExecutionContexts.parasitic)
+        p.future
+    }
+  }
+
+  /**
+   * Returns a [[java.util.concurrent.CompletionStage]] that will be completed 
with a [[TimeoutException]]
+   * if the provided value is not completed within the specified duration.
+   */

Review Comment:
   can you add `@since 1.2.0` to new public methods?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org
For additional commands, e-mail: notifications-h...@pekko.apache.org

Reply via email to