Application.scala 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = fs :+ Fortune(anorm.NotAssigned, "Additional fortune added at request time.")
  53. Ok(views.html.fortune(fortunes))
  54. }
  55. }
  56. }
  57. }
  58. }