Application.scala 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.{Worlds, World, Fortunes, Fortune}
  8. import utils._
  9. import scala.concurrent.Future
  10. import play.api.libs.concurrent.Execution.Implicits._
  11. import play.core.NamedThreadFactory
  12. object Application extends Controller {
  13. private val MaxQueriesPerRequest = 20
  14. private val TestDatabaseRows = 10000
  15. private val partitionCount = current.configuration.getInt("db.default.partitionCount").getOrElse(2)
  16. private val maxConnections =
  17. partitionCount * current.configuration.getInt("db.default.maxConnectionsPerPartition").getOrElse(5)
  18. private val minConnections =
  19. partitionCount * current.configuration.getInt("db.default.minConnectionsPerPartition").getOrElse(5)
  20. private val tpe = new ThreadPoolExecutor(minConnections, maxConnections,
  21. 0L, TimeUnit.MILLISECONDS,
  22. new LinkedBlockingQueue[Runnable](),
  23. new NamedThreadFactory("dbEc"))
  24. private val dbEc = ExecutionContext.fromExecutorService(tpe)
  25. private val worldsTable = new Worlds
  26. private val fortunesTable = new Fortunes
  27. // A predicate for checking our ability to service database requests is determined by ensuring that the request
  28. // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections * the max
  29. // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
  30. // queries we're going to make or what other threads are running in parallel etc. Nevertheless, the check is
  31. // adequate in order to throttle the acceptance of requests to the size of the pool.
  32. def isDbAvailable: Boolean = (tpe.getQueue.size() < maxConnections * MaxQueriesPerRequest)
  33. def db(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  34. Action.async {
  35. val random = ThreadLocalRandom.current()
  36. val _worlds = Future.sequence((for {
  37. _ <- 1 to queries
  38. } yield Future(worldsTable.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
  39. ).toList)
  40. _worlds map {
  41. w => Ok(Json.toJson(w))
  42. }
  43. }
  44. }
  45. def fortunes() = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  46. Action.async {
  47. Future(fortunesTable.getAll())(dbEc).map { fs =>
  48. val fortunes = Fortune(0, "Additional fortune added at request time.") +: fs
  49. Ok(views.html.fortune(fortunes))
  50. }
  51. }
  52. }
  53. def update(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  54. Action.async {
  55. val random = ThreadLocalRandom.current()
  56. val boundsCheckedQueries = queries match {
  57. case q if q > 500 => 500
  58. case q if q < 1 => 1
  59. case _ => queries
  60. }
  61. val worlds = Future.sequence((for {
  62. _ <- 1 to boundsCheckedQueries
  63. } yield Future {
  64. for {
  65. world <- worldsTable.findById(random.nextInt(TestDatabaseRows) + 1)
  66. } yield {
  67. val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
  68. worldsTable.updateRandom(updatedWorld)
  69. updatedWorld
  70. }
  71. }(dbEc)
  72. ).toList)
  73. worlds.map {
  74. w => Ok(Json.toJson(w)).withHeaders("Server" -> "Netty")
  75. }
  76. }
  77. }
  78. }