-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ChristopherDavenport/blockTimer
Add blockTimer and rename to blockShift
- Loading branch information
Showing
11 changed files
with
66 additions
and
77 deletions.
There are no files selected for viewing
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
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
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
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
26 changes: 13 additions & 13 deletions
26
linebacker/src/main/scala/io/chrisdavenport/linebacker/Linebacker.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
package io.chrisdavenport.linebacker | ||
|
||
import cats._ | ||
import cats.implicits._ | ||
import cats.effect.Async | ||
import cats.effect._ | ||
import java.util.concurrent.ExecutorService | ||
import scala.concurrent.ExecutionContext | ||
|
||
trait Linebacker[F[_]] { | ||
|
||
def blockingPool: ExecutionContext | ||
def blockingContext: ExecutionContext | ||
|
||
/** | ||
* Attempts to Run the Given `F[A]` on the blocking pool. | ||
* Then shifts back to the given implicit execution context | ||
* after the Async `F[A]` is evaluated. | ||
*/ | ||
final def block[A](fa: F[A])(implicit F: Async[F], ec: ExecutionContext): F[A] = | ||
for { | ||
_ <- Async.shift(blockingPool) | ||
eA <- fa.attempt | ||
_ <- Async.shift(ec) | ||
a <- Applicative[F].pure(eA).rethrow | ||
} yield a | ||
final def blockEc[A](fa: F[A])(implicit F: Async[F], ec: ExecutionContext): F[A] = | ||
dualShift(blockingContext, ec, fa) | ||
|
||
/** | ||
* Attempts to Run the Given `F[A]` on the blocking pool. | ||
* Then shifts back to the F for the timer. | ||
*/ | ||
final def blockTimer[A](fa: F[A])(implicit F: Async[F], timer: Timer[F]): F[A] = | ||
F.bracket(Async.shift[F](blockingContext))(_ => fa)(_ => timer.shift) | ||
} | ||
|
||
object Linebacker { | ||
def apply[F[_]](implicit ev: Linebacker[F]): Linebacker[F] = ev | ||
|
||
def fromExecutorService[F[_]](es: ExecutorService): Linebacker[F] = new Linebacker[F] { | ||
def blockingPool = ExecutionContext.fromExecutorService(es) | ||
def blockingContext = ExecutionContext.fromExecutorService(es) | ||
} | ||
def fromExecutionContext[F[_]](ec: ExecutionContext): Linebacker[F] = new Linebacker[F] { | ||
def blockingPool = ec | ||
def blockingContext = ec | ||
} | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
linebacker/src/main/scala/io/chrisdavenport/linebacker/package.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.chrisdavenport | ||
|
||
import cats.effect.Async | ||
import scala.concurrent.ExecutionContext | ||
|
||
package object linebacker { | ||
private[linebacker] def dualShift[F[_]: Async, A]( | ||
initialEc: ExecutionContext, | ||
endEc: ExecutionContext, | ||
fa: F[A]) = | ||
Async[F].bracket(Async.shift[F](initialEc))(_ => fa)(_ => Async.shift[F](endEc)) | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.1.5 | ||
sbt.version=1.1.6 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version in ThisBuild := "0.1.1-SNAPSHOT" | ||
version in ThisBuild := "0.2.0-SNAPSHOT" |