View BlockingDao2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RobustDao(jdbcTemplate: JdbcTemplate, | |
blockingEc: ExecutionContext, | |
applicationEc: ExecutionContext) { | |
def get(id: String): Future[Option[RichDomainObject]] = { | |
Future(getBlocking(id))(blockingEc) | |
.map(blob => blob.map(parseJson))(applicationEc) | |
} | |
private def getBlocking(id: String): Option[String] = | |
jdbcTemplate.queryForObject( |
View BlockingDao1.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit val blockingExecutionContext = | |
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(50)) | |
class MyDao(jdbcTemplate: JdbcTemplate)(implicit ec: ExecutionContext) { | |
def get(id: String): Future[Option[RichDomainObject]] = | |
Future(getBlocking(id)) | |
private def getBlocking(id: String): Option[RichDomainObject] = | |
jdbcTemplate.queryForObject( | |
"SELECT blob FROM table WHERE id = ?", |
View FutureException3.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rpcCall: Future[Option[String]] | |
def reportException(e: Throwable): Unit | |
def getUrlSafe: Future[Option[String]] = { | |
rpcCall.recover { | |
case e: Throwable => | |
reportException(e) | |
None | |
} | |
} |
View safeFuture.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def safeFuture[T](f: => Future[T]): Future[T] = { | |
try { | |
f | |
} catch { | |
case NonFatal(e) => Future.failed(e) | |
} | |
} | |
def handleFromMemcached(r: HttpRequest): Future[Option[Int]] = safeFuture { | |
require(r.uri != "/memcached-boom", "Memcached Boom!") |
View executeLazily.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def executeLazily[Argument, Return](argument: Argument, | |
list: List[Argument => Future[Option[Return]]]) | |
(implicit ec: ExecutionContext): Future[Option[Return]] = { | |
val promise = Promise[Option[Return]]() | |
val iterator = list.iterator | |
def completeWith(t: Try[Option[Return]]): Unit = t match { | |
case Success(value) => | |
if (value.isDefined || !iterator.hasNext) | |
promise.success(value) |
View RpcCaching2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val handlers: List[HttpRequest => Future[Option[Int]]] = List( | |
handleFromMemcached, | |
handleFromCdn, | |
r => handleFromHadoop(r).map(Some.apply) | |
) | |
def handle(r: HttpRequest): Future[Int] = { | |
executeLazily(handlers.map(handler => () => handler(r))) | |
.map(_.getOrElse(throw new IllegalStateException("Hadoop should always return Some!"))) | |
} |
View RpcCaching1.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class HttpRequest(uri: String) | |
def handleFromMemcached(r: HttpRequest): Future[Option[Int]] = { | |
require(r.uri != "/memcached-boom", "Memcached Boom!") | |
Future.successful(if (r.uri == "/memcached") Some(42) else None) | |
} | |
def handleFromCdn(r: HttpRequest): Future[Option[Int]] = { | |
require(r.uri != "/cnd-boom", "CDN Boom!") | |
Future.successful(if (r.uri == "/cdn") Some(42) else None) |
View FutureException2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def innocentFunction(param: AnyRef): Future[Int] = { | |
require(param != null) | |
Future.successful(42) | |
} | |
innocentFunction(null) | |
.map(_ => throw new IllegalStateException) | |
// an IllegalArgumentException will be thrown before map call |
View FutureException1.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future(42) | |
.map[Int](_ => throw new IllegalArgumentException) | |
.flatMap[Int](_ => throw new IllegalStateException) | |
// Effectively: Future.failed(new IllegalArgumentException) |
View RecoverFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class FutureExtensions[T](val future: Future[T]) extends AnyVal { | |
def recoverFilter(f: => T): Future[T] = | |
future.recover { | |
case _: NoSuchElementException | ControlException => f | |
} | |
def recoverFilterWith(f: => Future[T]): Future[T] = | |
future.recoverWith { | |
case _: NoSuchElementException | ControlException => f | |
} |