Skip to content

Commit

Permalink
Merge pull request #63 from ing-bank/fix/redis-connection-pool
Browse files Browse the repository at this point in the history
Fix/redis connection pool
  • Loading branch information
jahnestacado committed Sep 27, 2022
2 parents 58de90c + 48cb605 commit dba6d6b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/main/scala/com/ing/wbaa/rokku/sts/RokkuStsService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.typesafe.scalalogging.LazyLogging
import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }
import scala.util.{ Failure, Success }
import scala.collection.mutable.ListBuffer

trait RokkuStsService
extends LazyLogging
Expand All @@ -27,6 +28,8 @@ trait RokkuStsService

protected[this] def httpSettings: HttpSettings

private final val terminationCallbacks: ListBuffer[() => Unit] = ListBuffer()

// The routes we serve
final val allRoutes: Route =
toStrictEntity(3.seconds) {
Expand All @@ -40,8 +43,14 @@ trait RokkuStsService

Http().newServerAt(httpSettings.httpBind, httpSettings.httpPort).bind(allRoutes)
.andThen {
case Success(binding) => logger.info(s"Sts service started listening: ${binding.localAddress}")
case Failure(reason) => logger.error("Sts service failed to start.", reason)
case Success(binding) =>
logger.info(s"Sts service started listening: ${binding.localAddress}")
sys.addShutdownHook {
logger.info("Received termination signal")
terminationCallbacks.foreach(c => c())
shutdown()
}
case Failure(reason) => logger.error("Sts service failed to start.", reason)
}
}

Expand All @@ -52,4 +61,8 @@ trait RokkuStsService
case Failure(reason) => logger.error("Sts service failed to stop.", reason)
}
}

def registerTerminationCallback(f: () => Unit): Unit = {
terminationCallbacks += f
}
}
14 changes: 13 additions & 1 deletion src/main/scala/com/ing/wbaa/rokku/sts/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ object Server extends App {

override protected[this] def redisSettings: RedisSettings = RedisSettings(system)

registerTerminationCallback(() => {
logger.info("Closing redis connection pool...")
redisPooledConnection.close()
})

//Connects to Redis on startup and initializes indeces
initializeUserSearchIndex(redisPooledConnection)
try {
initializeUserSearchIndex(redisPooledConnection)
} catch {
case ex: RedisSecondaryIndexException =>
logger.error(s"Unable to create index $UsersIndex. Error: ${ex.getMessage()}. Exiting...")
sys.exit(1)
}

}.startup
}
4 changes: 3 additions & 1 deletion src/main/scala/com/ing/wbaa/rokku/sts/service/db/Redis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ trait Redis extends LazyLogging {
*/
protected[this] final def checkDbConnection(): Future[Unit] = {
Future {
val response = new Jedis(redisPooledConnection.getPool().getResource()).ping()
val connection = redisPooledConnection.getPool().getResource()
val response = new Jedis(connection).ping()
connection.close()
assert(response.toLowerCase().equals("pong"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ trait RedisModel extends Encryption {

private val DuplicateIndexExceptionMsg = "Index already exists"

case class RedisSecondaryIndexException(message: String) extends Exception

object UserKey {
def encode(username: Username): String = {
s"$UserKeyPrefix${username.value}"
Expand Down Expand Up @@ -89,7 +91,7 @@ trait RedisModel extends Encryption {
case DuplicateIndexExceptionMsg =>
logger.info(s"Index ${UsersIndex} already exists. Continuing...")
case _ =>
logger.error(s"Unable to create index $UsersIndex. Error: ${exc.getMessage()}")
throw new RedisSecondaryIndexException(s"Unable to create index $UsersIndex. Error: ${exc.getMessage()}")
}
case exc: Exception =>
logger.error(s"Unable to create index $UsersIndex. Error: ${exc.getMessage()}")
Expand Down

0 comments on commit dba6d6b

Please sign in to comment.