tpolecat / linebacker

Finally Tagless Blocking Implementation

Home Page:https://christopherdavenport.github.io/linebacker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Linebacker Build Status Maven Central Gitter chat

Enabling Functional Blocking where you need it.

Quick Start

To use linebacker in an existing SBT project with Scala 2.11 or a later version, add the following dependency to your build.sbt:

libraryDependencies += "io.chrisdavenport" %% "linebacker" % "<version>"

Examples

First some imports

import scala.concurrent.ExecutionContext.Implicits.global
import fs2.Stream
import cats.effect._
import cats.implicits._
import io.chrisdavenport.linebacker.Linebacker
import io.chrisdavenport.linebacker.contexts.Executors

Creating And Evaluating Pool Behavior

val getThread = IO(Thread.currentThread().getName)

object ThreadNameExample {
  val checkRun = Executors.unbound[IO] // Create Executor
    .map(Linebacker.fromExecutorService[IO](_)) // Create Linebacker From Executor
    .flatMap { implicit linebacker => // Raise Implicitly
      Stream.eval(
        Linebacker[IO].block(getThread) // Block On Linebacker Pool Not Global
      ) ++
      Stream.eval(getThread) // Running On Global
    }
    .evalMap(threadName => IO(println(threadName)))
    .compile
    .drain
}
ThreadNameExample.checkRun.unsafeRunSync

Dual Contexts Are Also Very Useful

import scala.concurrent.ExecutionContext
import io.chrisdavenport.linebacker.DualContext

Executors.unbound[IO].map(blockingExecutor =>
  DualContext.fromContexts[IO](global,  ExecutionContext.fromExecutorService(blockingExecutor))
)

About

Finally Tagless Blocking Implementation

https://christopherdavenport.github.io/linebacker

License:MIT License


Languages

Language:Scala 100.0%