Действительно ли создание субъекта для запроса базы данных в запросе-ответе playframework является чрезмерным? - PullRequest
1 голос
/ 10 июля 2019

Некоторые люди говорят, что не следует использовать актер при запросе БД. Я попробовал этот тест - и нашел этот результат

С простым использованием актера:

      package services.ActorServices

    import akka.actor.{Actor, PoisonPill}
    import akka.pattern._
    import services.dbServices.HomePageDAO

    import scala.concurrent.ExecutionContext.Implicits._
    import scala.concurrent.Future

    class IndexActor extends Actor {
      override def receive: Receive = {
        case "something" =>
          println("in actor,before query thread count:"+Thread.activeCount())
          val x: Future[Seq[(String, String)]] = HomePageDAO.getSloganAndInstruction
          pipe(x) to sender()
          println("in actor,after query thread count:"+Thread.activeCount())
          println("_________________________________________________________")
          self ! PoisonPill


      }
    }


    package controllers

    import akka.actor.{ActorRef, ActorSystem, Props}
    import akka.util.Timeout
    import javax.inject._
    import play.api.cache.Cached
    import play.api.mvc._
    import services.ActorServices.IndexActor

    import scala.concurrent.ExecutionContext
    import scala.concurrent.duration._

    @Singleton
    class IndexController @Inject()(cc: ControllerComponents, cached: Cached, actorSystem: ActorSystem)(implicit assetsFinder: AssetsFinder, executionContext: ExecutionContext)
      extends AbstractController(cc) {



      def index: Action[AnyContent] = Action.async {
        println("in Action,before Actor create thread count:"+Thread.activeCount())
        val indexActor: ActorRef = actorSystem.actorOf(Props[IndexActor])
        import akka.pattern.ask
        implicit val timeOut = Timeout(20 seconds)

          (indexActor ? "something").mapTo[Seq[(String, String)]].map(seqOfSandI => Ok(views.html.index("index", "index", "yellowLogo", seqOfSandI.head._1, seqOfSandI.head._2)))

        }


    }

Результат по запросу:

    [info] play.api.Play - Application started (Dev)
    in Action,before Actor create thread count:37
    in actor,before query thread count:37
    in actor,after query thread count:40
    _________________________________________________________
    in Action,before Actor create thread count:47
    in actor,before query thread count:47
    in actor,after query thread count:48
    _________________________________________________________
    in Action,before Actor create thread count:48
    in actor,before query thread count:48
    in actor,after query thread count:49
    _________________________________________________________
    in Action,before Actor create thread count:49
    in actor,before query thread count:49
    in actor,after query thread count:50
    _________________________________________________________
    in Action,before Actor create thread count:49
    in actor,before query thread count:49
    in actor,after query thread count:50
    _________________________________________________________
    in Action,before Actor create thread count:49
    in actor,before query thread count:49
    in actor,after query thread count:50
    _________________________________________________________
    in Action,before Actor create thread count:50
    in actor,before query thread count:50
    in actor,after query thread count:51
    _________________________________________________________
    in Action,before Actor create thread count:51
    in actor,before query thread count:51
    in actor,after query thread count:52
    _________________________________________________________
    in Action,before Actor create thread count:52
    in actor,before query thread count:52
    in actor,after query thread count:53
    _________________________________________________________
    in Action,before Actor create thread count:52
    in actor,before query thread count:52
    in actor,after query thread count:53
    _________________________________________________________
    in Action,before Actor create thread count:53
    in actor,before query thread count:53
    in actor,after query thread count:54
    _________________________________________________________
    in Action,before Actor create thread count:54
    in actor,before query thread count:54
    in actor,after query thread count:55
    _________________________________________________________
    in Action,before Actor create thread count:47
    in actor,before query thread count:47
    in actor,after query thread count:48
    _________________________________________________________
    in Action,before Actor create thread count:54
    in actor,before query thread count:54
    in actor,after query thread count:55
    _________________________________________________________
    in Action,before Actor create thread count:55
    in actor,before query thread count:55
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:57
    _________________________________________________________
    in Action,before Actor create thread count:57
    in actor,before query thread count:57
    in actor,after query thread count:58
    _________________________________________________________
    in Action,before Actor create thread count:58
    in actor,before query thread count:58
    in actor,after query thread count:59
    _________________________________________________________
    in Action,before Actor create thread count:59
    in actor,before query thread count:59
    in actor,after query thread count:60
    _________________________________________________________
    in Action,before Actor create thread count:60
    in actor,before query thread count:60
    in actor,after query thread count:61
    _________________________________________________________
    in Action,before Actor create thread count:61
    in actor,before query thread count:61
    in actor,after query thread count:61
    _________________________________________________________
    in Action,before Actor create thread count:61
    in actor,before query thread count:61
    in actor,after query thread count:61
    _________________________________________________________
    in Action,before Actor create thread count:61
    in actor,before query thread count:61
    in actor,after query thread count:61
    _________________________________________________________
    in Action,before Actor create thread count:45
    in actor,before query thread count:46
    in actor,after query thread count:46
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:48
    in actor,before query thread count:48
    in actor,after query thread count:48
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________
    in Action,before Actor create thread count:56
    in actor,before query thread count:56
    in actor,after query thread count:56
    _________________________________________________________

без актера Я нашел этот результат

package controllers

import akka.actor.ActorSystem
import javax.inject._
import play.api.cache.Cached
import play.api.mvc._
import services.dbServices.HomePageDAO

import scala.concurrent.ExecutionContext

@Singleton
class IndexController @Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder, executionContext: ExecutionContext)
  extends AbstractController(cc) {


  def index: Action[AnyContent] = Action.async {
    println("in Action,before query create thread count:" + Thread.activeCount())
    HomePageDAO.getSloganAndInstruction.map { seqOfSandI =>
      println("after query creation, thread count:" + Thread.activeCount())
      println("_________________________________________________________")
      Ok(views.html.index("index", "index", "yellowLogo", seqOfSandI.head._1, seqOfSandI.head._2))
    }

  }


}

Результат :::::::::::::::::::::::::::::::::::::::::::

[info] play.api.Play - Application started (Dev)
in Action,before query create thread count:76
after query creation, thread count:79
_________________________________________________________
in Action,before query create thread count:85
after query creation, thread count:86
_________________________________________________________
in Action,before query create thread count:85
after query creation, thread count:86
_________________________________________________________
in Action,before query create thread count:86
after query creation, thread count:87
_________________________________________________________
in Action,before query create thread count:87
after query creation, thread count:88
_________________________________________________________
in Action,before query create thread count:86
after query creation, thread count:87
_________________________________________________________
in Action,before query create thread count:87
after query creation, thread count:88
_________________________________________________________
in Action,before query create thread count:87
after query creation, thread count:88
_________________________________________________________
in Action,before query create thread count:87
after query creation, thread count:88
_________________________________________________________
in Action,before query create thread count:87
after query creation, thread count:88
_________________________________________________________
in Action,before query create thread count:90
after query creation, thread count:91
_________________________________________________________
in Action,before query create thread count:91
after query creation, thread count:92
_________________________________________________________
in Action,before query create thread count:92
after query creation, thread count:93
_________________________________________________________
in Action,before query create thread count:90
after query creation, thread count:91
_________________________________________________________
in Action,before query create thread count:93
after query creation, thread count:94
_________________________________________________________
in Action,before query create thread count:94
after query creation, thread count:95
_________________________________________________________
in Action,before query create thread count:95
after query creation, thread count:96
_________________________________________________________
in Action,before query create thread count:96
after query creation, thread count:97
_________________________________________________________
in Action,before query create thread count:97
after query creation, thread count:98
_________________________________________________________
in Action,before query create thread count:98
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________
in Action,before query create thread count:99
after query creation, thread count:99
_________________________________________________________

Что означает, что с актером я получаю хорошее управление потоками. Это действительно хорошо? Или здесь я допустил ошибку ??????????? Ищу предложение.

...