Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

}
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 {

}
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) {

Copy link
Copy Markdown
Contributor

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/DisposableBean callbacks (destroy() was not called after the call completed in my test): the callbacks registered here have to be run when the call ends. A Context listener fires on completion and cancellation alike, which also saves the two overrides in the interceptor.

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);
}
}
}

}

}
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);
}

}
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);
}
}

}
Loading
Loading