Skip to content

Commit

Permalink
Merge pull request #211 from masahitojp/feat/use-apache-commons-pool-2.x
Browse files Browse the repository at this point in the history
using apache pool to v2(latest)
  • Loading branch information
debasishg authored Dec 7, 2018
2 parents 5dd0079 + 5f95dae commit 3f30725
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ lazy val commonSettings: Seq[Setting[_]] = Seq(
lazy val coreSettings = commonSettings ++ Seq(
name := "RedisClient",
libraryDependencies ++= Seq(
"commons-pool" % "commons-pool" % "1.6",
"commons-pool" % "commons-pool" % "2.6.0",
"org.slf4j" % "slf4j-api" % "1.7.25",
"org.slf4j" % "slf4j-log4j12" % "1.7.25" % "provided",
"log4j" % "log4j" % "1.2.17" % "provided",
Expand Down
51 changes: 30 additions & 21 deletions src/main/scala/com/redis/Pool.scala
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
package com.redis

import org.apache.commons.pool._
import org.apache.commons.pool.impl._
import java.util.concurrent.TimeUnit

import org.apache.commons.pool2._
import org.apache.commons.pool2.impl._
import com.redis.cluster.ClusterNode

private [redis] class RedisClientFactory(val host: String, val port: Int, val database: Int = 0, val secret: Option[Any] = None, val timeout : Int = 0)
extends PoolableObjectFactory[RedisClient] {
extends PooledObjectFactory[RedisClient] {

// when we make an object it's already connected
def makeObject: RedisClient = {
new RedisClient(host, port, database, secret, timeout)
override def makeObject: PooledObject[RedisClient] = {
new DefaultPooledObject[RedisClient](new RedisClient(host, port, database, secret, timeout))
}

// quit & disconnect
def destroyObject(rc: RedisClient): Unit = {
override def destroyObject(p: PooledObject[RedisClient]): Unit = {
val rc = p.getObject
rc.quit // need to quit for closing the connection
rc.disconnect // need to disconnect for releasing sockets
}

// noop: we want to have it connected
def passivateObject(rc: RedisClient): Unit = {}
def validateObject(rc: RedisClient): Boolean = rc.connected
override def passivateObject(p: PooledObject[RedisClient]): Unit = {}
override def validateObject(p: PooledObject[RedisClient]): Boolean = p.getObject.connected

// noop: it should be connected already
def activateObject(rc: RedisClient): Unit = {}
override def activateObject(p: PooledObject[RedisClient]): Unit = {}
}

object RedisClientPool {
val UNLIMITED_CONNECTIONS = -1

val WHEN_EXHAUSTED_BLOCK = GenericObjectPool.WHEN_EXHAUSTED_BLOCK
val WHEN_EXHAUSTED_FAIL = GenericObjectPool.WHEN_EXHAUSTED_FAIL
val WHEN_EXHAUSTED_GROW = GenericObjectPool.WHEN_EXHAUSTED_GROW
val UNLIMITED_CONNECTIONS: Int = -1
}

class RedisClientPool(val host: String, val port: Int, val maxIdle: Int = 8, val database: Int = 0, val secret: Option[Any] = None, val timeout : Int = 0,
val maxConnections: Int = RedisClientPool.UNLIMITED_CONNECTIONS, val whenExhaustedBehavior: Byte = RedisClientPool.WHEN_EXHAUSTED_BLOCK, val poolWaitTimeout: Long = 3000) {
val pool = new GenericObjectPool(new RedisClientFactory(host, port, database, secret, timeout), maxConnections, whenExhaustedBehavior, poolWaitTimeout, maxIdle, false, true)
class RedisClientPool(val host: String, val port: Int, val maxIdle: Int = 8, val database: Int = 0, val secret: Option[Any] = None, val timeout : Int = 0,
val maxConnections: Int = RedisClientPool.UNLIMITED_CONNECTIONS, val poolWaitTimeout: Long = 3000) {

val objectPoolConfig = new GenericObjectPoolConfig[RedisClient]
objectPoolConfig.setMaxIdle(maxIdle)
objectPoolConfig.setMaxTotal(maxConnections)
objectPoolConfig.setBlockWhenExhausted(true)
objectPoolConfig.setTestOnBorrow(false)
objectPoolConfig.setTestOnReturn(true)

val abandonedConfig = new AbandonedConfig
abandonedConfig.setRemoveAbandonedTimeout(TimeUnit.MILLISECONDS.toSeconds(poolWaitTimeout).toInt)
val pool = new GenericObjectPool(new RedisClientFactory(host, port, database, secret, timeout), objectPoolConfig,abandonedConfig)
override def toString: String = host + ":" + String.valueOf(port)

def withClient[T](body: RedisClient => T) = {
def withClient[T](body: RedisClient => T): T = {
val client = pool.borrowObject
try {
body(client)
Expand All @@ -53,9 +62,9 @@ class RedisClientPool(val host: String, val port: Int, val maxIdle: Int = 8, val
}

/**
*
* @param node
*/
*
* @param node
*/
class IdentifiableRedisClientPool(val node: ClusterNode)
extends RedisClientPool (node.host, node.port, node.maxIdle, node.database, node.secret,node.timeout){
override def toString: String = node.nodename
Expand Down

0 comments on commit 3f30725

Please sign in to comment.