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
Add API to make it easier to write method interceptors and support Kotlin suspend functions #3921
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Definitely a 2.1 feature this
aop/src/main/java/io/micronaut/aop/util/KotlinInvocationContextUtils.java
Outdated
Show resolved
Hide resolved
aop/src/main/java/io/micronaut/aop/util/KotlinInvocationContextUtils.java
Outdated
Show resolved
Hide resolved
I think there should be a better way to do interception instead of the current code that is almost the same everywhere: "check if publisher", "check if future" and now this will require adding "check if kotlin suspend". |
@dstepanov yes we need some generalized abstraction for handling all these cases I agree |
Extracted a helper class, looks much better now. |
Seem to be some checkstyle violations |
The only doubt I have about this is I am unsure if it is going into In general I think we need to move rxjava 2 to a module like the other frameworks and add a dependency on the modules that use it to RxJava 2 to make sure we have a road to move to rxjava 3 in the future |
4454fb3
to
8057a1d
Compare
I have removed RxJava 2 dependency, only one use case with publishers/futures that is not changed is Http client. |
@graemerocher I have made some refactoring to introduce reactive introduce call API, the core should have all interceptors refactored to use new API. Let me know what do you think. |
@dstepanov this is really awesome work and will definitely help avoid duplication! There are a few things I am unsure about:
|
@graemerocher To retry the method call the interceptor needs to be supplied |
ah ok that confused me, maybe it should be called |
Naming is not the best, for the users of the API it might not be very clear what is the interceptor chain etc. that's why |
@graemerocher How would you name |
@dstepanov maybe |
aop/src/main/java/io/micronaut/aop/util/ReactiveMethodInterceptorHelper.java
Outdated
Show resolved
Hide resolved
8875dc9
to
d4c2700
Compare
Refactored |
8d976ff
to
6dd71af
Compare
I think we should probably leave it like it is for now |
@dstepanov one final change. I think we should not include this as a method on |
@graemerocher something like |
yes something like that |
@graemerocher Renamed |
I was thinking maybe it would be better to replace null checks with instanceof, impl. would need to pass an instance that implements 2-3 interfaces.
The API is a bit strange because Java doesn't allow it to implement multiple interfaces for anonymous classes, but the performance should be better. Also I would add WDYT? |
@graemerocher Should I try to refactor it? |
@dstepanov I wonder if it is worth providing a worse API in the name of performance. |
Probably not |
@dstepanov I'd like to get this merged in pretty soon so I can look into coroutine support for the client. Overall it looks to be a big improvement over what we have currently. I am also concerned about the performance implication and I'd like to propose an alternative. What if instead of having a builder style, an object was returned I haven't dug into this logic very deeply so perhaps this wouldn't work for some reason, but it seems like it wouldn't complicate the usage of the API very much. |
@jameskleeh That's one of the ways to do it with the disadvantage of moving the switch outside to every interceptor and removing the strick check. It would be possible to completely remove the builder:
This PR also adds coroutine support for the http client: micronaut-core/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/server/suspend/SuspendClient.kt Lines 18 to 47 in 880b2f4
|
@dstepanov I see now that coroutines are handled as part of the completion stage handlers. Awesome! @graemerocher Can you give your thoughts? Perhaps we should have an idea of the performance implications before moving to a less friendly API. |
@jameskleeh only way to know the performance implications is benchmarking, however in general it is safe to assume a switch style would be much faster and more efficient in terms of memory consumption since there is less object creation to perform method dispatch. |
Should I make some changes? |
@dstepanov You can, however I think the code you showed above wouldn't result in increased performance:
I think it would need to be more like
If that seems doable and you can get to it within the next week or so, that would be fantastic. If not, I will make sure it gets taken care of before 2.1 |
86e54d9
to
1b5b692
Compare
…tlin suspend functions
@jameskleeh @graemerocher Please review |
That looks great to me. @jameskleeh ? |
@graemerocher Looks good to me. Really great contribution @dstepanov! |
@jameskleeh Thanks! You had a good suggestion for the API |
Added util classes to work with Kotlin's suspend functions in
MethodInterceptor
s and implemented to use it with@Retryable
.It should be possible to implement other interceptors like micronaut-projects/micronaut-cache#183.
Personally I'm not a Kotlin user just found it interesting.