-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for memoised resources (#177)
This adds a construct allowing for lazy allocation/eager de-allocation of resources, which can be handy when users want to keep computationally heavy constructs in the background across several suites (distributed log consumers, and so on). It is currently not cancel-safe, but I intend to specialise it to CE2/CE3 in a later unit of work
- Loading branch information
Showing
7 changed files
with
313 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package weaver | ||
|
||
import cats.effect._ | ||
import cats.syntax.all._ | ||
|
||
import CECompat.{ Deferred, Ref } | ||
|
||
object MemoisedResource { | ||
def apply[F[_]: Concurrent, A]( | ||
resource: Resource[F, A]): F[Resource[F, A]] = | ||
new MemoisedResource[F, A].apply(resource) | ||
} | ||
|
||
private class MemoisedResource[F[_]: Concurrent, A] { | ||
|
||
sealed trait State | ||
case object Uninitialised extends State | ||
case class InUse( | ||
value: Deferred[F, Either[Throwable, A]], | ||
finalizer: F[Unit], | ||
uses: Int) | ||
extends State | ||
|
||
def apply(resource: Resource[F, A]): F[Resource[F, A]] = | ||
Ref[F].of[State](Uninitialised).map { ref => | ||
val initialise: F[A] = for { | ||
valuePromise <- Deferred[F, Either[Throwable, A]] | ||
finaliserPromise <- Deferred[F, F[Unit]] | ||
compute <- ref.modify { | ||
case Uninitialised => | ||
val newState = InUse(valuePromise, finaliserPromise.get.flatten, 1) | ||
val compute = Concurrent[F].attempt(resource.allocated).flatMap { | ||
case Right((a, fin)) => for { | ||
_ <- valuePromise.complete(Right(a)) | ||
_ <- finaliserPromise.complete(fin) | ||
} yield a | ||
case Left(e) => for { | ||
_ <- valuePromise.complete(Left(e)) | ||
_ <- finaliserPromise.complete(Concurrent[F].unit) | ||
_ <- ref.set(Uninitialised) // reset state | ||
a <- Concurrent[F].raiseError[A](e) | ||
} yield a | ||
} | ||
newState -> compute | ||
case InUse(value, finalizer, uses) => | ||
val newState = InUse(value, finalizer, uses + 1) | ||
val compute: F[A] = value.get.flatMap(Concurrent[F].fromEither) | ||
newState -> compute | ||
} | ||
value <- compute | ||
} yield value | ||
|
||
val finalise: F[Unit] = ref.modify[F[Unit]] { | ||
case Uninitialised => | ||
Uninitialised -> Concurrent[F].raiseError( | ||
new IllegalStateException("Implementation error")) | ||
case InUse(_, finaliser, n) if n <= 1 => | ||
Uninitialised -> finaliser | ||
case InUse(value, finaliser, n) => | ||
InUse(value, finaliser, n - 1) -> Concurrent[F].unit | ||
}.flatten | ||
|
||
Resource.make(initialise)(_ => finalise) | ||
} | ||
|
||
} |
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
Oops, something went wrong.