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

Remove util.Execution.withTimeout #798

Open
wants to merge 1 commit into
base: series/0.23
Choose a base branch
from
Open
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
21 changes: 1 addition & 20 deletions core/src/main/scala/org/http4s/blaze/util/Execution.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,19 @@

package org.http4s.blaze.util

import scala.concurrent.{ExecutionContext, Future, Promise}
import java.util

import org.log4s.getLogger

import scala.annotation.tailrec
import scala.concurrent.duration.Duration
import scala.util.Try
import scala.util.control.NonFatal
import scala.concurrent.ExecutionContext

/** Special `ExecutionContext` instances.
*/
object Execution {
private[this] val logger = getLogger

// Race a future vs a default value. The timer used is the default blaze scheduler
private[blaze] def withTimeout[T](d: Duration, fallback: Try[T])(f: Future[T]): Future[T] =
if (!d.isFinite) f
else if (f.isCompleted) f
else {
val p = Promise[T]()
val r = new Runnable {
def run(): Unit =
if (p.tryComplete(fallback))
logger.debug(s"Future $f timed out after $d, yielding reesult $fallback")
}
// Race the future vs a timeout
Execution.scheduler.schedule(r, d)
f.onComplete(p.tryComplete(_))(Execution.directec)
p.future
}

/** A trampolining `ExecutionContext`
*
* This `ExecutionContext` is run thread locally to avoid context switches. Because this is a
Expand Down