Application.scala 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.{World, 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. // A predicate for checking our ability to service database requests is determined by ensuring that the request
  26. // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections * the max
  27. // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
  28. // queries we're going to make or what other threads are running in parallel etc. Nevertheless, the check is
  29. // adequate in order to throttle the acceptance of requests to the size of the pool.
  30. def isDbAvailable: Boolean = (tpe.getQueue.size() < maxConnections * MaxQueriesPerRequest)
  31. def json() = Action {
  32. Ok(Json.obj("message" -> "Hello World!"))
  33. }
  34. def db(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  35. Action {
  36. Async {
  37. val random = ThreadLocalRandom.current()
  38. val worlds = Future.sequence((for {
  39. _ <- 1 to queries
  40. } yield Future(World.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
  41. ).toList)
  42. worlds map {
  43. w => Ok(Json.toJson(w))
  44. }
  45. }
  46. }
  47. }
  48. def fortunes() = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  49. Action {
  50. Async {
  51. Future(Fortune.getAll())(dbEc).map { fs =>
  52. val fortunes = Fortune(anorm.NotAssigned, "Additional fortune added at request time.") +: fs
  53. Ok(views.html.fortune(fortunes))
  54. }
  55. }
  56. }
  57. }
  58. def update(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  59. Action {
  60. Async {
  61. val random = ThreadLocalRandom.current()
  62. val boundsCheckedQueries = queries match {
  63. case q if q > 500 => 500
  64. case q if q < 1 => 1
  65. case _ => queries
  66. }
  67. val worlds = Future.sequence((for {
  68. _ <- 1 to boundsCheckedQueries
  69. } yield Future {
  70. val world = World.findById(random.nextInt(TestDatabaseRows) + 1)
  71. val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
  72. World.updateRandom(updatedWorld)
  73. updatedWorld
  74. }(dbEc)
  75. ).toList)
  76. worlds.map {
  77. w => Ok(Json.toJson(w)).withHeaders("Server" -> "Netty")
  78. }
  79. }
  80. }
  81. }
  82. }