Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/redis connection pool #63

Merged
merged 3 commits into from
Sep 27, 2022
Merged
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
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