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) |
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!"))) | |
} |
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) |
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!") |
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 | |
} | |
} |
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 = ?", |
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( |
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
trait ExecutionContext { | |
def execute(runnable: Runnable): Unit | |
def reportFailure(cause: Throwable): Unit | |
} |
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[String] = ??? | |
rpcCall | |
.map(_.split('_')) | |
.filter(_.length < 4) | |
.map { | |
case Array(single) => single | |
case Array(first, second@_) => first | |
case Array(first@_, second, third@_) => third | |
case _ => "" |
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
var future = Future.successful(0) | |
for (_ <- 1 to 100) { | |
future = future.map(v => v + 1) | |
} | |
require(Await.result(future, 10.seconds) == 100) |