Skip to content

Modelling of ephemeral topics. #2355

Answered by ValdemarGr
ValdemarGr asked this question in Q&A
Discussion options

You must be logged in to vote

I ended up implementing it with Ref and Deferred and lease counting.

import cats.effect._
import cats.effect.concurrent.Ref
import cats.effect.concurrent.Deferred
import cats.data.OptionT
import java.util.UUID
import cats.Monad

abstract class ManagedRefMap[F[_]: Concurrent, K, V] {
  import ManagedRefMap._

  def alloc: K => F[V]

  val state: State[F, K, V]

  import cats.implicits._
  def get(id: K) =
    state.get.map(_.get(id)).flatMap[Option[V]] {
      case None    => Concurrent[F].pure(None)
      case Some(v) => v.value.get.flatMap(Concurrent[F].fromEither).map(x => Some(x))
    }

  def use(id: K): Resource[F, V] =
    useUnderlying(id, None).map{ case (v, _) => v }

  def useIn…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ValdemarGr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant