-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run Actions in their ExecutionContext #8225
Comments
I've noticed that this is happening especially when you have a mixed java/scala project. E.g. some controllers are in Java, others are in Scala and especially if the Custom HttpRequestHandler is written in Scala (even when its copied exactly from the documentations VirtualHostRequestHandler) |
Thanks for sharing your experience @domdorn. Once we've got a patch in it would be great if you could test it to see if it's an improvement for you. 😄 |
I created an issue for HttpRequestHandler API improvements: #8278. We should keep this in mind while fixing this issue. |
FYI I have been working on this: playframework:e6ed774...richdougherty:2d0b61e |
Due to several changes in how we handle threading,
Action
code no longer always runs in the action'sExecutionContext
.The
Action
runs in the default dispatcher:playframework/framework/src/play-akka-http-server/src/main/scala/play/core/server/AkkaHttpServer.scala
Lines 283 to 295 in f7f4727
Also an Action is usually wrapped in a series of filters:
playframework/framework/src/play/src/main/scala/play/api/http/HttpRequestHandler.scala
Lines 194 to 202 in f7f4727
The action uses
Accumulator.mapFuture
to call its logic:playframework/framework/src/play/src/main/scala/play/api/mvc/Action.scala
Lines 89 to 104 in f7f4727
But for performance reasons this only uses the
Action
'sExecutionContext
for aSinkAccumulator
, not aStrictAccumulator
:playframework/framework/src/play-streams/src/main/scala/play/api/libs/streams/Accumulator.scala
Lines 122 to 123 in f7f4727
playframework/framework/src/play-streams/src/main/scala/play/api/libs/streams/Accumulator.scala
Lines 169 to 180 in f7f4727
Since it's such a big performance win to avoid a thread-switch when using a
StrictAccumulator
, I think we want to stick with the current behaviour inAction
. (See also: #8224)One approach may be to change the code in
AkkaHttpServer
(and the NettyPlayRequestHandler
) to run the action and filter code in the correctExecutionContext
when the action is first called, before the body is even parsed. In other words the server should somehow figure out the action'sExecutionContext
and use that value when it dispatches the request. This means that the action will run in the rightExecutionContext
even if the action has a strict body.It's a bit tricky to figure out the
Action
'sExecutionContext
since the server may not have a direct reference to the ultimateAction
objeect. The server only has a reference to anEssentialAction
, which is usually constructed from be a series ofFilter
s wrapping anAction
.We need to figure out a way to get the
ExecutionContext
for anAction
even if wrapped by some filters. This potentially will require a change to theHttpRequestHandler
orHandler
APIs, but we should try to make changes minimal.Another approach is to ask the
HttpRequestHandler
to handle the details ofExecutionContext
switching. For example, thefilterAction
method could return anEssentialAction
that runs theFilter
s andAction
in theAction
'sExecutionContext
.playframework/framework/src/play/src/main/scala/play/api/http/HttpRequestHandler.scala
Lines 230 to 235 in f7f4727
The
HttpRequestHandler
might be the most appropriate place to put the logic, since it know aboutFilter
s andAction
s. It does come at the cost of more complexity in theHttpRequetHandler
, but I think that's where we should be putting more request processing logic anyway, since it abstracts out the details that are shared between server backends. See: #8005.The text was updated successfully, but these errors were encountered: