Skip to content

Commit

Permalink
Add maxIdle setting
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Jun 8, 2022
1 parent cf4c0c9 commit c3aa4fd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ lazy val core = crossProject(JVMPlatform, JSPlatform)
),
ProblemFilters.exclude[DirectMissingMethodProblem](
"org.typelevel.keypool.KeyPool#KeyPoolConcrete.kpDestroy"
)
),
ProblemFilters
.exclude[DirectMissingMethodProblem]("org.typelevel.keypool.KeyPoolBuilder.this"),
ProblemFilters.exclude[DirectMissingMethodProblem]("org.typelevel.keypool.KeyPool#Builder.this")
)
)

Expand Down
12 changes: 11 additions & 1 deletion core/src/main/scala/org/typelevel/keypool/KeyPool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ object KeyPool {
private[keypool] val kpRes: A => Resource[F, B],
private[keypool] val kpDefaultReuseState: Reusable,
private[keypool] val kpMaxPerKey: A => Int,
private[keypool] val kpMaxIdle: Int,
private[keypool] val kpMaxTotal: Int,
private[keypool] val kpMaxTotalSem: Semaphore[F],
private[keypool] val kpVar: Ref[F, PoolMap[A, (B, F[Unit])]]
Expand Down Expand Up @@ -255,7 +256,7 @@ object KeyPool {
pc match {
case p @ PoolClosed() => (p, destroy)
case p @ PoolOpen(idleCount, m) =>
if (idleCount > kp.kpMaxTotal) (p, destroy)
if (idleCount > kp.kpMaxIdle) (p, destroy)
else
m.get(k) match {
case None =>
Expand Down Expand Up @@ -314,6 +315,7 @@ object KeyPool {
val kpDefaultReuseState: Reusable,
val idleTimeAllowedInPool: Duration,
val kpMaxPerKey: A => Int,
val kpMaxIdle: Int,
val kpMaxTotal: Int,
val onReaperException: Throwable => F[Unit]
) {
Expand All @@ -322,13 +324,15 @@ object KeyPool {
kpDefaultReuseState: Reusable = this.kpDefaultReuseState,
idleTimeAllowedInPool: Duration = this.idleTimeAllowedInPool,
kpMaxPerKey: A => Int = this.kpMaxPerKey,
kpMaxIdle: Int = this.kpMaxIdle,
kpMaxTotal: Int = this.kpMaxTotal,
onReaperException: Throwable => F[Unit] = this.onReaperException
): Builder[F, A, B] = new Builder[F, A, B](
kpRes,
kpDefaultReuseState,
idleTimeAllowedInPool,
kpMaxPerKey,
kpMaxIdle,
kpMaxTotal,
onReaperException
)
Expand All @@ -350,6 +354,9 @@ object KeyPool {
def withMaxPerKey(f: A => Int): Builder[F, A, B] =
copy(kpMaxPerKey = f)

def withMaxIdle(maxIdle: Int): Builder[F, A, B] =
copy(kpMaxIdle = maxIdle)

def withMaxTotal(total: Int): Builder[F, A, B] =
copy(kpMaxTotal = total)

Expand Down Expand Up @@ -377,6 +384,7 @@ object KeyPool {
kpRes,
kpDefaultReuseState,
kpMaxPerKey,
kpMaxIdle,
kpMaxTotal,
kpMaxTotalSem,
kpVar
Expand All @@ -393,6 +401,7 @@ object KeyPool {
Defaults.defaultReuseState,
Defaults.idleTimeAllowedInPool,
Defaults.maxPerKey,
Defaults.maxIdle,
Defaults.maxTotal,
Defaults.onReaperException[F]
)
Expand All @@ -407,6 +416,7 @@ object KeyPool {
val defaultReuseState = Reusable.Reuse
val idleTimeAllowedInPool = 30.seconds
def maxPerKey[K](k: K): Int = Function.const(100)(k)
val maxIdle = 100
val maxTotal = 100
def onReaperException[F[_]: Applicative] = { (t: Throwable) =>
Function.const(Applicative[F].unit)(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class KeyPoolBuilder[F[_]: Temporal, A, B] private (
val kpDefaultReuseState: Reusable,
val idleTimeAllowedInPool: Duration,
val kpMaxPerKey: A => Int,
val kpMaxIdle: Int,
val kpMaxTotal: Int,
val onReaperException: Throwable => F[Unit]
) {
Expand All @@ -44,6 +45,7 @@ final class KeyPoolBuilder[F[_]: Temporal, A, B] private (
kpDefaultReuseState: Reusable = this.kpDefaultReuseState,
idleTimeAllowedInPool: Duration = this.idleTimeAllowedInPool,
kpMaxPerKey: A => Int = this.kpMaxPerKey,
kpMaxIdle: Int = this.kpMaxIdle,
kpMaxTotal: Int = this.kpMaxTotal,
onReaperException: Throwable => F[Unit] = this.onReaperException
): KeyPoolBuilder[F, A, B] = new KeyPoolBuilder[F, A, B](
Expand All @@ -52,6 +54,7 @@ final class KeyPoolBuilder[F[_]: Temporal, A, B] private (
kpDefaultReuseState,
idleTimeAllowedInPool,
kpMaxPerKey,
kpMaxIdle,
kpMaxTotal,
onReaperException
)
Expand All @@ -71,6 +74,9 @@ final class KeyPoolBuilder[F[_]: Temporal, A, B] private (
def withMaxPerKey(f: A => Int): KeyPoolBuilder[F, A, B] =
copy(kpMaxPerKey = f)

def withMaxIdle(maxIdle: Int): KeyPoolBuilder[F, A, B] =
copy(kpMaxIdle = maxIdle)

def withMaxTotal(total: Int): KeyPoolBuilder[F, A, B] =
copy(kpMaxTotal = total)

Expand Down Expand Up @@ -98,6 +104,7 @@ final class KeyPoolBuilder[F[_]: Temporal, A, B] private (
(a: A) => Resource.make[F, B](kpCreate(a))(kpDestroy),
kpDefaultReuseState,
kpMaxPerKey,
kpMaxIdle,
kpMaxTotal,
kpMaxTotalSem,
kpVar
Expand All @@ -117,6 +124,7 @@ object KeyPoolBuilder {
Defaults.defaultReuseState,
Defaults.idleTimeAllowedInPool,
Defaults.maxPerKey,
Defaults.maxIdle,
Defaults.maxTotal,
Defaults.onReaperException[F]
)
Expand All @@ -125,6 +133,7 @@ object KeyPoolBuilder {
val defaultReuseState = Reusable.Reuse
val idleTimeAllowedInPool = 30.seconds
def maxPerKey[K](k: K): Int = Function.const(100)(k)
val maxIdle = 100
val maxTotal = 100
def onReaperException[F[_]: Applicative] = { (t: Throwable) =>
Function.const(Applicative[F].unit)(t)
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/scala/org/typelevel/keypool/Pool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,22 @@ object Pool {
val kpRes: Resource[F, B],
val kpDefaultReuseState: Reusable,
val idleTimeAllowedInPool: Duration,
val kpMaxIdle: Int,
val kpMaxTotal: Int,
val onReaperException: Throwable => F[Unit]
) {
private def copy(
kpRes: Resource[F, B] = this.kpRes,
kpDefaultReuseState: Reusable = this.kpDefaultReuseState,
idleTimeAllowedInPool: Duration = this.idleTimeAllowedInPool,
kpMaxIdle: Int = this.kpMaxIdle,
kpMaxTotal: Int = this.kpMaxTotal,
onReaperException: Throwable => F[Unit] = this.onReaperException
): Builder[F, B] = new Builder[F, B](
kpRes,
kpDefaultReuseState,
idleTimeAllowedInPool,
kpMaxIdle,
kpMaxTotal,
onReaperException
)
Expand All @@ -105,6 +108,9 @@ object Pool {
def withIdleTimeAllowedInPool(duration: Duration) =
copy(idleTimeAllowedInPool = duration)

def withMaxIdle(maxIdle: Int): Builder[F, B] =
copy(kpMaxIdle = maxIdle)

def withMaxTotal(total: Int): Builder[F, B] =
copy(kpMaxTotal = total)

Expand All @@ -117,6 +123,7 @@ object Pool {
kpDefaultReuseState = kpDefaultReuseState,
idleTimeAllowedInPool = idleTimeAllowedInPool,
kpMaxPerKey = _ => kpMaxTotal,
kpMaxIdle = kpMaxIdle,
kpMaxTotal = kpMaxTotal,
onReaperException = onReaperException
)
Expand All @@ -138,6 +145,7 @@ object Pool {
res,
Defaults.defaultReuseState,
Defaults.idleTimeAllowedInPool,
Defaults.maxIdle,
Defaults.maxTotal,
Defaults.onReaperException[F]
)
Expand All @@ -151,6 +159,7 @@ object Pool {
private object Defaults {
val defaultReuseState = Reusable.Reuse
val idleTimeAllowedInPool = 30.seconds
val maxIdle = 100
val maxTotal = 100
def onReaperException[F[_]: Applicative] = { (t: Throwable) =>
Function.const(Applicative[F].unit)(t)
Expand Down

0 comments on commit c3aa4fd

Please sign in to comment.