Repository navigation
Add gRPC request scope support (#392) #436
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
Merged
dsyer
merged 6 commits into
spring-projects:main
from
fernandoripol:feature/grpc-request-scope
Oct 31, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1484c14
Add gRPC request scope support (#392)
fernandoripol 2c2a10c
Refactor request scope to use @EnableGrpcScope and ImportBeanDefiniti…
fernandoripol 2b79c4e
Add @GrpcRequestScope annotation for user bean definitions
fernandoripol 8434508
Restore GrpcRequestScope integration tests with correct bean proxy ta…
fernandoripol f9b11b7
Key the gRPC request scope on the call's Context
sharanggupta 3d9212c
Merge pull request #1 from sharanggupta/grpc-request-scope-offer
fernandoripol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...-grpc-core/src/main/java/org/springframework/grpc/server/annotation/GrpcRequestScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2024-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.grpc.server.annotation; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import org.springframework.context.annotation.Scope; | ||
| import org.springframework.context.annotation.ScopedProxyMode; | ||
| import org.springframework.core.annotation.AliasFor; | ||
|
|
||
| @Target({ ElementType.TYPE, ElementType.METHOD }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Scope("grpcRequest") | ||
| public @interface GrpcRequestScope { | ||
|
|
||
| @AliasFor(annotation = Scope.class) | ||
| ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS; | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
spring-grpc-core/src/main/java/org/springframework/grpc/server/scope/EnableGrpcScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 2024-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.grpc.server.scope; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import org.springframework.context.annotation.Import; | ||
|
|
||
| @Target(ElementType.TYPE) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Import(GrpcScopeRegistrar.class) | ||
| public @interface EnableGrpcScope { | ||
|
|
||
| } |
207 changes: 207 additions & 0 deletions
207
spring-grpc-core/src/main/java/org/springframework/grpc/server/scope/GrpcRequestScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| /* | ||
| * Copyright 2024-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.grpc.server.scope; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import org.springframework.beans.factory.ObjectFactory; | ||
| import org.springframework.beans.factory.config.Scope; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| import io.grpc.Context; | ||
|
|
||
| /** | ||
| * A {@link Scope} lasting for the duration of a gRPC call. The beans of a call are held | ||
| * in the call's {@link Context gRPC context}, so they are shared by every thread to which | ||
| * that context is propagated, and are destroyed when the context is cancelled, that is | ||
| * when the call completes or is cancelled. Beans are created on first use within a call; | ||
| * nothing can be looked up outside of a call or once the call has ended. | ||
| * | ||
| * @author Fernando Rodrigues Ripol | ||
| * @author Sharang Gupta | ||
| * @see GrpcRequestScopeServerInterceptor | ||
| * @see org.springframework.grpc.server.annotation.GrpcRequestScope | ||
| */ | ||
| public class GrpcRequestScope implements Scope { | ||
|
|
||
| /** | ||
| * The name of the scope. | ||
| */ | ||
| public static final String SCOPE_NAME = "grpcRequest"; | ||
|
|
||
| private static final Log logger = LogFactory.getLog(GrpcRequestScope.class); | ||
|
|
||
| private static final Context.Key<ScopedBeans> SCOPED_BEANS = Context.key("grpcRequestScope"); | ||
|
|
||
| /** | ||
| * Create the context of a new call, holding the beans of that call. They are | ||
| * destroyed when the context is cancelled. | ||
| * @param parent the current context | ||
| * @return the context to run the call in | ||
| */ | ||
| static Context newCallContext(Context parent) { | ||
| ScopedBeans scopedBeans = new ScopedBeans(); | ||
| Context context = parent.withValue(SCOPED_BEANS, scopedBeans); | ||
| context.addListener((cancelled) -> scopedBeans.destroy(), Runnable::run); | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public Object get(String name, ObjectFactory<?> objectFactory) { | ||
| return currentScopedBeans().get(name, objectFactory); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable Object remove(String name) { | ||
| return currentScopedBeans().remove(name); | ||
| } | ||
|
|
||
| @Override | ||
| public void registerDestructionCallback(String name, Runnable callback) { | ||
| currentScopedBeans().registerDestructionCallback(name, callback); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable Object resolveContextualObject(String key) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable String getConversationId() { | ||
| return null; | ||
| } | ||
|
|
||
| private static ScopedBeans currentScopedBeans() { | ||
| ScopedBeans scopedBeans = SCOPED_BEANS.get(); | ||
| Assert.state(scopedBeans != null, | ||
| () -> "No gRPC call is active in the current context: the " + SCOPE_NAME | ||
| + " scope is only available within a call intercepted by GrpcRequestScopeServerInterceptor" | ||
| + " and in threads to which the call's io.grpc.Context has been propagated"); | ||
| return scopedBeans; | ||
| } | ||
|
|
||
| /** | ||
| * The beans of one call. Creation is serialized per bean name, not per call, and | ||
| * nothing can be looked up once the call's beans have been destroyed. | ||
| */ | ||
| private static final class ScopedBeans { | ||
|
|
||
| private final ConcurrentMap<String, ScopedBean> beans = new ConcurrentHashMap<>(); | ||
|
|
||
| private volatile boolean destroyed; | ||
|
|
||
| Object get(String name, ObjectFactory<?> objectFactory) { | ||
| assertNotDestroyed(); | ||
| ScopedBean bean = this.beans.computeIfAbsent(name, (key) -> new ScopedBean(key, objectFactory)); | ||
| Object instance = bean.get(); | ||
| if (this.destroyed) { | ||
| // Lost the race against the end of the call: nothing may outlive it. | ||
| ScopedBean late = this.beans.remove(name); | ||
| if (late != null) { | ||
| late.destroy(); | ||
| } | ||
| assertNotDestroyed(); | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| @Nullable | ||
| Object remove(String name) { | ||
| ScopedBean bean = this.beans.remove(name); | ||
| return (bean != null) ? bean.getIfCreated() : null; | ||
| } | ||
|
|
||
| void registerDestructionCallback(String name, Runnable callback) { | ||
| ScopedBean bean = this.beans.get(name); | ||
| if (bean != null) { | ||
| bean.setDestructionCallback(callback); | ||
| } | ||
| } | ||
|
|
||
| void destroy() { | ||
| this.destroyed = true; | ||
| Iterator<ScopedBean> iterator = this.beans.values().iterator(); | ||
| while (iterator.hasNext()) { | ||
| ScopedBean bean = iterator.next(); | ||
| iterator.remove(); | ||
| bean.destroy(); | ||
| } | ||
| } | ||
|
|
||
| private void assertNotDestroyed() { | ||
| Assert.state(!this.destroyed, | ||
| () -> "The gRPC call has completed and its " + SCOPE_NAME + " scoped beans have been destroyed"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * One bean of a call, created on first access and destroyed at most once. | ||
| */ | ||
| private static final class ScopedBean { | ||
|
|
||
| private final String name; | ||
|
|
||
| private final ObjectFactory<?> objectFactory; | ||
|
|
||
| private @Nullable Object instance; | ||
|
|
||
| private @Nullable Runnable destructionCallback; | ||
|
|
||
| ScopedBean(String name, ObjectFactory<?> objectFactory) { | ||
| this.name = name; | ||
| this.objectFactory = objectFactory; | ||
| } | ||
|
|
||
| synchronized Object get() { | ||
| if (this.instance == null) { | ||
| this.instance = this.objectFactory.getObject(); | ||
| } | ||
| return this.instance; | ||
| } | ||
|
|
||
| synchronized @Nullable Object getIfCreated() { | ||
| return this.instance; | ||
| } | ||
|
|
||
| synchronized void setDestructionCallback(Runnable callback) { | ||
| this.destructionCallback = callback; | ||
| } | ||
|
|
||
| synchronized void destroy() { | ||
| Runnable callback = this.destructionCallback; | ||
| this.destructionCallback = null; | ||
| this.instance = null; | ||
| if (callback != null) { | ||
| try { | ||
| callback.run(); | ||
| } | ||
| catch (Throwable ex) { | ||
| logger.error("Destruction callback of gRPC request scoped bean '" + this.name + "' failed", ex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
57 changes: 57 additions & 0 deletions
57
...rc/main/java/org/springframework/grpc/server/scope/GrpcRequestScopeServerInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2024-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.grpc.server.scope; | ||
|
|
||
| import org.springframework.core.Ordered; | ||
| import org.springframework.grpc.server.GlobalServerInterceptor; | ||
|
|
||
| import io.grpc.Context; | ||
| import io.grpc.Contexts; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.ServerCall; | ||
| import io.grpc.ServerCallHandler; | ||
| import io.grpc.ServerInterceptor; | ||
|
|
||
| /** | ||
| * A global server interceptor that opens a new {@link GrpcRequestScope} for every call it | ||
| * intercepts, ahead of the other interceptors in the service chain, so that request | ||
| * scoped beans are available to them. The beans of the call are destroyed when the call | ||
| * completes or is cancelled. | ||
| * | ||
| * @author Fernando Rodrigues Ripol | ||
| * @author Sharang Gupta | ||
| * @see EnableGrpcScope | ||
| */ | ||
| @GlobalServerInterceptor | ||
| public class GrpcRequestScopeServerInterceptor implements ServerInterceptor, Ordered { | ||
|
|
||
| /** | ||
| * Runs before the other interceptors in the chain so that scoped beans are available | ||
| * to them. | ||
| */ | ||
| @Override | ||
| public int getOrder() { | ||
| return Ordered.HIGHEST_PRECEDENCE; | ||
| } | ||
|
|
||
| @Override | ||
| public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, | ||
| ServerCallHandler<ReqT, RespT> next) { | ||
| return Contexts.interceptCall(GrpcRequestScope.newCallContext(Context.current()), call, headers, next); | ||
| } | ||
|
|
||
| } |
57 changes: 57 additions & 0 deletions
57
spring-grpc-core/src/main/java/org/springframework/grpc/server/scope/GrpcScopeRegistrar.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2024-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.grpc.server.scope; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.springframework.beans.factory.config.BeanDefinition; | ||
| import org.springframework.beans.factory.config.CustomScopeConfigurer; | ||
| import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
| import org.springframework.beans.factory.support.RootBeanDefinition; | ||
| import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
| import org.springframework.core.type.AnnotationMetadata; | ||
|
|
||
| /** | ||
| * Registers the {@value GrpcRequestScope#SCOPE_NAME} scope with the bean factory and the | ||
| * global server interceptor that opens it for every call. | ||
| * | ||
| * @author Fernando Rodrigues Ripol | ||
| * @author Sharang Gupta | ||
| * @see EnableGrpcScope | ||
| */ | ||
| public class GrpcScopeRegistrar implements ImportBeanDefinitionRegistrar { | ||
|
|
||
| static final String SCOPE_CONFIGURER_BEAN_NAME = "grpcRequestScopeConfigurer"; | ||
|
|
||
| static final String INTERCEPTOR_BEAN_NAME = "grpcRequestScopeServerInterceptor"; | ||
|
|
||
| @Override | ||
| public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
| if (!registry.containsBeanDefinition(SCOPE_CONFIGURER_BEAN_NAME)) { | ||
| RootBeanDefinition configurer = new RootBeanDefinition(CustomScopeConfigurer.class); | ||
| configurer.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); | ||
| configurer.getPropertyValues().add("scopes", Map.of(GrpcRequestScope.SCOPE_NAME, new GrpcRequestScope())); | ||
| registry.registerBeanDefinition(SCOPE_CONFIGURER_BEAN_NAME, configurer); | ||
| } | ||
| if (!registry.containsBeanDefinition(INTERCEPTOR_BEAN_NAME)) { | ||
| RootBeanDefinition interceptor = new RootBeanDefinition(GrpcRequestScopeServerInterceptor.class); | ||
| interceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); | ||
| registry.registerBeanDefinition(INTERCEPTOR_BEAN_NAME, interceptor); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Scoped beans never get their
@PreDestroy/DisposableBeancallbacks (destroy()was not called after the call completed in my test): the callbacks registered here have to be run when the call ends. AContextlistener fires on completion and cancellation alike, which also saves the two overrides in the interceptor.