Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions core/src/main/scala/org/typelevel/keypool/KeyPool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,19 @@ object KeyPool {
}
}

def createWithPermit: Resource[F, B] =
kp.kpMaxTotalSem.tryPermit.flatMap {
case true =>
kp.kpRes(k)
case false =>
ApplicativeThrow[Resource[F, *]].raiseError(
new NoSuchElementException("Pool is maxed out on idle connections for other keys"))
}

for {
_ <- kp.kpMaxTotalSem.permit
optR <- Resource.eval(kp.kpVar.modify(go))
releasedState <- Resource.eval(Ref[F].of[Reusable](kp.kpDefaultReuseState))
resource <- Resource.make(optR.fold(kp.kpRes(k).allocated)(r => Applicative[F].pure(r))) {
resource <- Resource.make(optR.fold(createWithPermit.allocated)(r => Applicative[F].pure(r))) {
resource =>
for {
reusable <- releasedState.get
Expand Down
24 changes: 23 additions & 1 deletion core/src/test/scala/org/typelevel/keypool/KeyPoolSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,29 @@ class KeyPoolSpec extends CatsEffectSuite {
}
}

test("Do not count idle resources toward maxTotal".only) {
final case class Stats(active: Int, highWater: Int) {
def inc =
copy(active = active + 1, highWater = math.max(active + 1, highWater))
def dec =
copy(active = active - 1)
}
IO.ref(Stats(0, 0)).flatMap(active =>
KeyPool.Builder((i: Int) =>
Resource.make(active.update(_.inc))(i => active.update(_.dec)))
.withMaxTotal(5)
.withMaxIdle(5)
.build
.use(keypool =>
keypool.take(0).replicateA(5).use_ *>
keypool.take(1).replicateA(5).use_
)
.flatMap(_ => active.get.map(_.highWater))
.flatTap(IO.println)
)
.map(i => assert(i <= 5, s"Active connections maxed out at $i"))
}

private def nothing(ref: Ref[IO, Int]): IO[Unit] =
ref.get.void

}