Skip to content

evolution-gaming/resource-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

resource-pool

Build Status Coverage Status Codacy Badge Version

Pool of cats-effect resources

Features:

  • allocates resources on demand up to configured limit
  • deallocates resources if not active for a configured time
  • tries to minimize number of resources in the pool
  • uses first-in-first-out queue for tasks
  • shuts down gracefully after completing accumulated tasks
  • tolerates resource failures
  • supports cancellation

Example

import com.evolution.resourcepool.ResourcePool.implicits.*

trait Connection {
  def query(): IO[Any]
}

def connection: Resource[IO, Connection] = ???

connection
  .toResourcePool( // you can convert any resource into the pool of resources
    maxSize = 10, // it will create up to `maxSize` connections 
    expireAfter = 1.minute) // pool will release connection if it is not used for 1 minute
  .use { connectionPool =>
    connectionPool
      .resource // this will get first available connection or allocate one
      .use { connection =>
        connection.query() // here you have access to the connection
      }
  }

Setup

addSbtPlugin("com.evolution" % "sbt-artifactory-plugin" % "0.0.2")

libraryDependencies += "com.evolution" %% "resource-pool" % "1.0.2"