Skip to content

Instantly share code, notes, and snippets.

Avatar

Dmitry Komanov dkomanov

View GitHub Profile
@dkomanov
dkomanov / StackTraceDirect.txt
Created May 25, 2020
[writing-async-app-in-scala-part-3] stack trace for direct
View StackTraceDirect.txt
java.lang.IllegalStateException
at com.komanov.future.examples.ExceptionDemo$.$anonfun$stackTrace$3(ExceptionDemo.scala:14)
at scala.runtime.java8.JFunction1$mcII$sp.apply(JFunction1$mcII$sp.java:23)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
at com.komanov.future.package$DirectExecutor.execute(package.scala:29)
@dkomanov
dkomanov / StackTraceGlobal.txt
Created May 25, 2020
[writing-async-app-in-scala-part-3] stack trace for global
View StackTraceGlobal.txt
java.lang.IllegalStateException
at com.komanov.future.examples.ExceptionDemo$.$anonfun$stackTrace$3(ExceptionDemo.scala:14)
at scala.runtime.java8.JFunction1$mcII$sp.apply(JFunction1$mcII$sp.java:23)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
@dkomanov
dkomanov / DirectStackTraceDemo.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] demo for stack trace
View DirectStackTraceDemo.scala
def stackTrace(implicit ec: ExecutionContext): Any = {
Await.result(
Future.successful(1)
.map(v => v + 1)
.flatMap(v => Future.successful(v))
.map { v =>
new IllegalStateException().printStackTrace()
v
},
10.seconds
@dkomanov
dkomanov / RpcClient.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] rpc client with 2 ExecutionContexts
View RpcClient.scala
class RpcClient(ownEc: ExecutionContext, appEc: ExecutionContext) {
def call: Future[String] =
Future("a")(ownEc)
.map(identity)(appEc) // here we move back to an application execution context
}
@dkomanov
dkomanov / DirectExecutorContextUsage.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] direct executor context usage
View DirectExecutorContextUsage.scala
def rpcCall: Future[String]
def daoCall(id: String): Future[Int]
def extractId(idStr: String): Future[String]
def convertDbValue(value: Int): Future[Int]
import directExecutionContext
for {
idStr <- rpcCall
id <- extractId(idStr) // executed in RPC execution context
@dkomanov
dkomanov / MapDescribed.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] map described
View MapDescribed.scala
def map[U](f: T => U)
(implicit ec: ExecutionContext): Future[U] = {
val promise = Promise()
if (isCompleted) // in real code this is an atomic operation + pattern matching
// even if it's completed, callback is submitted to ExecutionContext
ec.execute(() => {
f(result)
})
else
registerCallback(f)(ec)
@dkomanov
dkomanov / SmartMap.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] smartMap extension
View SmartMap.scala
def smartMap[U](f: T => U)(implicit ec: ExecutionContext): Future[U] =
if (future.isCompleted) {
if (future.value.get.isSuccess)
wrap(f(future.value.get.get))
else
future.asInstanceOf[Future[U]]
} else {
future.map(f)(ec)
}
@dkomanov
dkomanov / Benchmark.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] benchmark
View Benchmark.scala
var future = Future.successful(0)
for (_ <- 1 to 100) {
future = future.map(v => v + 1)
}
require(Await.result(future, 10.seconds) == 100)
@dkomanov
dkomanov / Transformations.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] multiple transformations
View Transformations.scala
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 _ => ""
@dkomanov
dkomanov / ExecutionContext.scala
Created May 25, 2020
[writing-async-app-in-scala-part-3] execution context
View ExecutionContext.scala
trait ExecutionContext {
def execute(runnable: Runnable): Unit
def reportFailure(cause: Throwable): Unit
}