Application.scala 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.async {
  36. val random = ThreadLocalRandom.current()
  37. val worlds = Future.sequence((for {
  38. _ <- 1 to queries
  39. } yield Future(World.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
  40. ).toList)
  41. worlds map {
  42. w => Ok(Json.toJson(w))
  43. }
  44. }
  45. }
  46. def fortunes() = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  47. Action.async {
  48. Future(Fortune.getAll())(dbEc).map { fs =>
  49. val fortunes = Fortune(0.toLong, "Additional fortune added at request time.") +: fs
  50. Ok(views.html.fortune(fortunes))
  51. }
  52. }
  53. }
  54. def update(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
  55. Action.async {
  56. val random = ThreadLocalRandom.current()
  57. val boundsCheckedQueries = queries match {
  58. case q if q > 500 => 500
  59. case q if q < 1 => 1
  60. case _ => queries
  61. }
  62. val worlds = Future.sequence((for {
  63. _ <- 1 to boundsCheckedQueries
  64. } yield Future {
  65. val world = World.findById(random.nextInt(TestDatabaseRows) + 1)
  66. val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
  67. World.updateRandom(updatedWorld)
  68. updatedWorld
  69. }(dbEc)
  70. ).toList)
  71. worlds.map {
  72. w => Ok(Json.toJson(w)).withHeaders("Server" -> "Netty")
  73. }
  74. }
  75. }
  76. }