Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
run("caveat 1") {
val someEffect = IO.shift(ec1) *> printThread
printThread *> someEffect *> printThread
}
/*
Outputs:
-- caveat 1 --
main
printThread *> printThread
run("Eval on") {
printThread *> cs1.evalOn(ec2)(printThread) *> printThread
}
/*
Outputs:
-- Eval on --
main
ec2-1-1945604815
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") {
val printThread = IO { println(Thread.currentThread().getName) }
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))
implicit val backend: SttpBackend[Id, Nothing] =
new LoggingSttpBackend[Id, Nothing](HttpURLConnectionBackend())
request.send()
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
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
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"
*/