Skip to content

Add gRPC request scope support (#392) - #436

Open
fernandoripol wants to merge 4 commits into
spring-projects:mainfrom
fernandoripol:feature/grpc-request-scope
Open

fernandoripol wants to merge 4 commits into
spring-projects:mainfrom
fernandoripol:feature/grpc-request-scope

Conversation

@fernandoripol

Copy link
Copy Markdown

This PR adds support for a custom gRPC request scope (@Scope("grpcRequest")) to the Spring gRPC framework, resolving issue #392.

Changes:

  • GrpcRequestScope: Implements the Spring Scope interface using ThreadLocal to manage request-scoped beans lifecycle.
  • GrpcRequestScopeServerInterceptor: A gRPC ServerInterceptor that cleans up the thread-local context upon call completion or cancellation.
  • GrpcRequestScopeAutoConfiguration: Auto-configuration class to register both the scope and its server interceptor automatically.

@fernandoripol
fernandoripol force-pushed the feature/grpc-request-scope branch from 5ab5166 to df19612 Compare October 4, 2026 01:41
@dsyer

dsyer commented Oct 9, 2026

Copy link
Copy Markdown
Member

We don't have auto-configuration in this project so the naming is wrong. I also think it would be better to stick those bean definitions in an ImportBeanDefinitionRegistrar and attach that to a new annotation (@EnableGrpcScope). Then we would expect to have a @GrpcRequestScope annotation that can go on user's bean definitions. Some tests would be nice. Thanks for the effort so far though.

@fernandoripol

Copy link
Copy Markdown
Author

Hi @dsyer! Thanks for the valuable feedback. I've updated the PR to remove the old auto-configuration and implemented your suggestion using the @EnableGrpcScope annotation along with the ImportBeanDefinitionRegistrar. Ready for another review!

@fernandoripol
fernandoripol force-pushed the feature/grpc-request-scope branch from 5fed8f4 to bc4ad85 Compare October 12, 2026 16:49
@dsyer

dsyer commented Oct 13, 2026

Copy link
Copy Markdown
Member

That's good, thanks. We'd still like an annotation for bean definitions (it would have to go in a different package since the name will clash). Have a look at RequestScopeTests in spring-web to get some ideas about testing?

@fernandoripol

fernandoripol commented Oct 15, 2026 •

Copy link
Copy Markdown
Author

Hi @dsyer, I've updated the implementation to isolate the bean definition logic into the EnableGrpcRequestScope annotation (along with the registrar) and added the requested integration tests modeled after Spring Web's request scope tests. Ready for another review when you have a moment.

@dsyer

dsyer commented Oct 15, 2026

Copy link
Copy Markdown
Member

I'm sorry I don't understand. Why do we have 2 @Enable.. annotations now? You forgot to sign off your commits too.

Signed-off-by: Your Name <you@example.com>
Signed-off-by: Fernando Rodrigues Ripol <fernandorodriguesripol@gmail.com>
…onRegistrar

Signed-off-by: Fernando Ripol <fernandorodriguesripol@gmail.com>
Signed-off-by: Fernando Rodrigues Ripol <fernandorodriguesripol@gmail.com>
@fernandoripol
fernandoripol force-pushed the feature/grpc-request-scope branch from ee012e8 to 2c2a10c Compare October 15, 2026 17:01
Signed-off-by: Fernando Rodrigues Ripol <fernandorodriguesripol@gmail.com>
@fernandoripol

Copy link
Copy Markdown
Author

Hi @dsyer, thanks for the feedback! I've removed the duplicate @enable.. annotation, added the user-facing @GrpcRequestScope annotation in the annotation package, and ensured all commits are properly signed off. Ready for your review!

@dsyer

dsyer commented Oct 16, 2026

Copy link
Copy Markdown
Member

You deleted the test?

…rget and style

Signed-off-by: Fernando Rodrigues Ripol <fernandorodriguesripol@gmail.com>
@fernandoripol

Copy link
Copy Markdown
Author

Hi @dsyer,

Apologies for the previous mix-up! The test file was accidentally dropped during a commit cleanup.

I have restored and updated GrpcRequestScopeTests under org.springframework.grpc.server.annotation using a non-final target bean for CGLIB proxying. The build, checkstyle, and integration tests have all passed locally, and all commits are properly signed off. Ready for your review!

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.

2 participants