Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gmethvin
Copy link
Member

This defines a RequestScopedRouter class that can be extended with Guice modules to allow injecting the RequestHeader and other dependencies that use a RequestHeader.

@gmethvin
Copy link
Member Author

This could eventually be used as an alternative to Http.Context in Java, but to do that we'd need to refactor how the context is created. Right now it's done inside the JavaAction.

@marcospereira
Copy link
Member

Great!

It will work as expected with the HttpRequestHandler and other components. If I understand it properly, the router still a singleton, but it will "fabricate" a new one for each request. And then, for each new "fabricated" Router we will get new controllers instances.

But, if the controllers are annotated with @Singleton, they will be reused (I suppose createChildInjector inherits the state of the parent Injector).

Will it have any (negative) impact on performance?

@gmethvin
Copy link
Member Author

Will it have any (negative) impact on performance?

The main concern I have is the performance impact creating a child injector. I tried looking at using a Scope in Guice, but it seems the built-in SimpleScope is thread-based. Maybe we need to do some benchmarks and see.

The cost of creating new instances is not really a problem. Normally it only makes sense to use @Singleton if your class has state, or if it is very expensive to create.

@mkurz
Copy link
Member

mkurz commented Dec 17, 2016

Looking at this validator class does it mean we could just inject a component (like we do with messagesApi) to replace Http.Context.current.get()? That would be awesome, specially since there is now way to pass the current context into the isValid method of a validator.

Also we could inject such components into the HttpErrorHandler (?)

@gmethvin
Copy link
Member Author

@mkurz Actually, it wouldn't work as I wrote it right now, because the JavaHandlerComponents doesn't use the request-scoped injector. I think it would work if we created an HttpRequestHandler on each request. That would also allow you to bind the request in your HttpErrorHandler implementation.

@gmethvin gmethvin force-pushed the request-scoped-routes branch from 046da10 to 464d91d Compare December 20, 2016 04:00
@gmethvin
Copy link
Member Author

gmethvin commented Dec 23, 2016

It seems like child injectors are the right way to achieve what we want to do (see https://groups.google.com/d/msg/google-guice/qZqkaWPkhoo/tcCRWNmIDQAJ).

One issue is that child injectors can't override bindings from the parent module, but we can't access the request unless the binding was in a request scoped module. So we probably have to separate out the request-scoped things into a separate Play module, so we can bind them only in the child injector.

@gmethvin
Copy link
Member Author

gmethvin commented Jun 21, 2017

Closing since this is not likely to happen, at least not in this form. If someone wants to pick this up and continue working on it, it should be possible to do it in another project. It's also possible you could make a request-scoped HttpRequestHandler instead.

@gmethvin gmethvin closed this Jun 21, 2017
AlexITC added a commit to AlexITC/play-request-tracer that referenced this pull request Sep 10, 2017
The work is mostly based in the code from this pull request
on play-framework project [1].

[1] - playframework/playframework#6798
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants