Application.scala 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package controllers
  2. import play.api.Play.current
  3. import play.api.mvc._
  4. import play.api.libs.json.Json
  5. import java.util.concurrent._
  6. import scala.concurrent._
  7. import models._
  8. import utils._
  9. import scala.concurrent.Future
  10. import play.api.libs.concurrent.Execution.Implicits._
  11. object Application extends Controller {
  12. private val TestDatabaseRows = 10000
  13. private val partitionCount = current.configuration.getInt("db.default.partitionCount").getOrElse(2)
  14. private val maxConnections =
  15. partitionCount * current.configuration.getInt("db.default.maxConnectionsPerPartition").getOrElse(5)
  16. private val minConnections =
  17. partitionCount * current.configuration.getInt("db.default.minConnectionsPerPartition").getOrElse(5)
  18. private val tpe = new ThreadPoolExecutor(minConnections, maxConnections,
  19. 0L, TimeUnit.MILLISECONDS,
  20. new LinkedBlockingQueue[Runnable]())
  21. private val dbEc = ExecutionContext.fromExecutorService(tpe)
  22. // A predicate for checking our ability to service database requests is determined by ensuring that the request
  23. // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections
  24. // to determine this threshold. It is a rough check as we don't know how many queries we're going
  25. // to make or what other threads are running in parallel etc. Nevertheless, the check is adequate in order to
  26. // throttle the acceptance of requests to the size of the pool.
  27. def isDbAvailable: Boolean = (tpe.getQueue.size() < maxConnections)
  28. def json() = Action {
  29. Ok(Json.obj("message" -> "Hello World!"))
  30. }
  31. def db(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  32. Action {
  33. Async {
  34. val random = ThreadLocalRandom.current()
  35. val worlds = Future.sequence((for {
  36. _ <- 1 to queries
  37. } yield Future(World.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
  38. ).toList)
  39. worlds map {
  40. w => Ok(Json.toJson(w))
  41. }
  42. }
  43. }
  44. }
  45. }