This file contains hidden or 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
run("caveat 1") { | |
val someEffect = IO.shift(ec1) *> printThread | |
printThread *> someEffect *> printThread | |
} | |
/* | |
Outputs: | |
-- caveat 1 -- | |
main |
This file contains hidden or 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
printThread *> printThread |
This file contains hidden or 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
run("Eval on") { | |
printThread *> cs1.evalOn(ec2)(printThread) *> printThread | |
} | |
/* | |
Outputs: | |
-- Eval on -- | |
main | |
ec2-1-1945604815 |
This file contains hidden or 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 cs1 = IO.contextShift(ec1) | |
val cs2 = IO.contextShift(ec2) | |
def run(name: String)(th: IO[_]): Unit = { | |
println(s"-- $name --") | |
th.unsafeRunSync() | |
println() | |
} | |
run("Shift") { |
This file contains hidden or 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 printThread = IO { println(Thread.currentThread().getName) } |
This file contains hidden or 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 ec1 = ExecutionContext.fromExecutor(Executors.newCachedThreadPool( | |
new NamedThreadFactory("ec1", true))) | |
val ec2 = ExecutionContext.fromExecutor(Executors.newCachedThreadPool( | |
new NamedThreadFactory("ec2", true))) | |
val ec3 = Executors.newCachedThreadPool(new NamedThreadFactory("ec3", true)) |
This file contains hidden or 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 backend: SttpBackend[Id, Nothing] = | |
new LoggingSttpBackend[Id, Nothing](HttpURLConnectionBackend()) | |
request.send() |
This file contains hidden or 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 LoggingSttpBackend[R[_], S](delegate: SttpBackend[R, S]) | |
extends SttpBackend[R, S] with StrictLogging { | |
override def send[T](request: Request[T, S]): R[Response[T]] = { | |
responseMonad.map(responseMonad.handleError(delegate.send(request)) { | |
case e: Exception => | |
logger.error( | |
s"Exception when sending request: $request.\n" + | |
s"To reproduce, run: ${request.toCurl}", | |
e |
This file contains hidden or 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 LoggingSttpBackend[R[_], S](delegate: SttpBackend[R, S]) | |
extends SttpBackend[R, S] with StrictLogging { | |
override def send[T](request: Request[T, S]): R[Response[T]] = { | |
responseMonad.map(responseMonad.handleError(delegate.send(request)) { | |
case e: Exception => | |
logger.error( | |
s"Exception when sending request: $request.\n" + | |
s"To reproduce, run: ${request.toCurl}", | |
e |
This file contains hidden or 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
println(request.toCurl) | |
/* | |
Outputs: | |
curl -L --max-redirs 32 -X POST | |
-H "Content-Type: application/json; charset=utf-8" -H "X-Auth: 123" | |
--data '{"name":"Mary","age":52}' | |
"http://httpbin.org/post" | |
*/ |