Skip to content

Commit

Permalink
updated doc
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFradet committed May 9, 2016
1 parent 26f42a7 commit 5cfd3f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Expand Up @@ -28,19 +28,25 @@ abstract class JavaFutureConverter {
}

/**
* Converter based on a [[FuturePool]] which will create one thread per future.
* Converter based on the specified <code>futurePool</code> which will create one thread per
* future possibly limited by the maximum size of the pool.
* To favor if there aren't too many futures to convert and one cares about latency.
* @param futurePool future pool used to retrieve the result of every future
* @param mayInterruptIfRunning whether or not the initial java future can be interrupted if it's
* running
*/
class FuturePoolJavaFutureConverter(mayInterruptIfRunning: Boolean) extends JavaFutureConverter {
class FuturePoolJavaFutureConverter(
futurePool: FuturePool,
mayInterruptIfRunning: Boolean
) extends JavaFutureConverter {
override def apply[T](javaFuture: JFuture[T]): Future[T] = {
val f = FuturePool.unboundedPool { javaFuture.get() }
val f = futurePool { javaFuture.get() }
val p = Promise.attached(f)
p.setInterruptHandler { case NonFatal(e) =>
if (p.detach()) {
f.raise(e)
javaFuture.cancel(mayInterruptIfRunning)
p.setException(e)
}
}
p
Expand All @@ -49,10 +55,12 @@ class FuturePoolJavaFutureConverter(mayInterruptIfRunning: Boolean) extends Java

/**
* Converter based on a [[Timer]] which will create a task which will check every
* <code>checkFrequency</code> if the java future is completed, one thread will be used for every
* conversion.
* <code>checkFrequency</code> if the java future is completed, the threading model is the one
* used by the specified <code>timer</code> which is often a thread pool of size 1.
* To favor if there are a lot of futures to convert and one cares less about the latency induced
* by <code>checkFrequency</code>.
* <code>checkFrequency</code> needs to be a multiple of the timer implementation's granularity
* which is often 1ms.
* @param timer timer used to schedule a task which will check if the java future is done
* @param checkFrequency frequency at which the java future will be checked for completion
* @param mayInterruptIfRunning whether or not the initial java future can be interrupted if it's
Expand Down
Expand Up @@ -18,7 +18,7 @@ package com.twitter.bijection.twitter_util

import com.twitter.bijection.{ CheckProperties, BaseProperties }
import com.twitter.io.Buf
import com.twitter.util.{ Future => TwitterFuture, Try => TwitterTry, Await => TwitterAwait, JavaTimer }
import com.twitter.util.{ Future => TwitterFuture, Try => TwitterTry, Await => TwitterAwait, FuturePool, JavaTimer }
import java.lang.{ Integer => JInt, Long => JLong }
import java.util.concurrent.{ Future => JavaFuture, Callable, FutureTask }
import org.scalacheck.Arbitrary
Expand Down Expand Up @@ -96,7 +96,7 @@ class UtilBijectionLaws extends CheckProperties with BaseProperties with BeforeA

property("round trips TwitterFuture[Map[JInt, JLong]] <-> JavaFuture[Map[JInt, JLong]] " +
"using FuturePool") {
implicit val converter = new FuturePoolJavaFutureConverter(true)
implicit val converter = new FuturePoolJavaFutureConverter(FuturePool.unboundedPool, true)
isBijection[TwitterFuture[ToMap], JavaFuture[ToMap]]
}

Expand Down

0 comments on commit 5cfd3f5

Please sign in to comment.