Skip to content

Commit 9d8ad76

Browse files
committed
Add new abstraction for GrpcServerExecutorProvider
1 parent 0863721 commit 9d8ad76

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcClientFactoryTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class GrpcClientFactoryTests {
4646

4747
GrpcClientFactoryTests() {
4848
Mockito.when(channelFactory.createChannel(Mockito.anyString(), Mockito.any()))
49-
.thenReturn(Mockito.mock(ManagedChannel.class));
49+
.thenReturn(Mockito.mock(ManagedChannel.class));
5050
context.registerBean(GrpcChannelFactory.class, () -> channelFactory);
5151
factory = new GrpcClientFactory();
5252
factory.setApplicationContext(context);
@@ -75,7 +75,7 @@ void testCustomStubFactory() {
7575
void testWithExplicitStubFactory() {
7676
context.registerBean(OtherStubFactory.class, () -> new OtherStubFactory());
7777
GrpcClientFactory.register(context, new GrpcClientRegistrationSpec("local", new Class[] { OtherStub.class })
78-
.factory(OtherStubFactory.class));
78+
.factory(OtherStubFactory.class));
7979
assertThat(factory.getClient("local", OtherStub.class, null)).isNotNull();
8080
}
8181

@@ -96,21 +96,19 @@ void testCoroutineStubFactory() {
9696
context.registerBean(CoroutineStubFactory.class, CoroutineStubFactory::new);
9797
GrpcClientFactory.register(context,
9898
GrpcClientRegistrationSpec.of("local")
99-
.factory(CoroutineStubFactory.class)
100-
.types(MyCoroutineStub.class));
99+
.factory(CoroutineStubFactory.class)
100+
.types(MyCoroutineStub.class));
101101
assertThat(factory.getClient("local", MyCoroutineStub.class, null)).isNotNull();
102102
}
103103

104104
@Test
105105
void testCoroutineStubFactoryAfterDefault() {
106106
context.registerBean(CoroutineStubFactory.class, CoroutineStubFactory::new);
107+
GrpcClientFactory.register(context, GrpcClientRegistrationSpec.of("local").types(MyStub.class));
107108
GrpcClientFactory.register(context,
108109
GrpcClientRegistrationSpec.of("local")
109-
.types(MyStub.class));
110-
GrpcClientFactory.register(context,
111-
GrpcClientRegistrationSpec.of("local")
112-
.factory(CoroutineStubFactory.class)
113-
.types(MyCoroutineStub.class));
110+
.factory(CoroutineStubFactory.class)
111+
.types(MyCoroutineStub.class));
114112
assertThat(factory.getClient("local", MyCoroutineStub.class, null)).isNotNull();
115113
assertThat(factory.getClient("local", MyStub.class, null)).isNotNull();
116114
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
2828
import org.springframework.context.annotation.Import;
29+
import org.springframework.core.Ordered;
2930
import org.springframework.grpc.autoconfigure.common.codec.GrpcCodecConfiguration;
3031
import org.springframework.grpc.server.ServerBuilderCustomizer;
3132
import org.springframework.grpc.server.exception.ReactiveStubBeanDefinitionRegistrar;
@@ -88,6 +89,34 @@ <T extends ServerBuilder<T>> ServerBuilderCustomizer<T> decompressionServerConfi
8889
return builder -> builder.decompressorRegistry(registry);
8990
}
9091

92+
@ConditionalOnBean(GrpcServerExecutorProvider.class)
93+
@Bean
94+
<T extends ServerBuilder<T>> ServerBuilderCustomizer<T> executorServerConfigurer(
95+
GrpcServerExecutorProvider provider) {
96+
return new ServerBuilderCustomizerImplementation<T>(provider);
97+
}
98+
99+
private final class ServerBuilderCustomizerImplementation<T extends ServerBuilder<T>>
100+
implements ServerBuilderCustomizer<T>, Ordered {
101+
102+
private final GrpcServerExecutorProvider provider;
103+
104+
private ServerBuilderCustomizerImplementation(GrpcServerExecutorProvider provider) {
105+
this.provider = provider;
106+
}
107+
108+
@Override
109+
public int getOrder() {
110+
return 0;
111+
}
112+
113+
@Override
114+
public void customize(T builder) {
115+
builder.executor(provider.getExecutor());
116+
}
117+
118+
}
119+
91120
@ConditionalOnClass(name = "com.salesforce.reactivegrpc.common.Function")
92121
@Configuration
93122
@Import(ReactiveStubBeanDefinitionRegistrar.class)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.grpc.autoconfigure.server;
17+
18+
import java.util.concurrent.Executor;
19+
20+
public interface GrpcServerExecutorProvider {
21+
22+
/**
23+
* Returns a {@link Executor} for the gRPC server, if it needs tio be customized.
24+
* @return the executor to use for the gRPC server
25+
*/
26+
Executor getExecutor();
27+
28+
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/security/GrpcSecurityAutoConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
import org.springframework.boot.autoconfigure.AutoConfiguration;
1919
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2122
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
2223
import org.springframework.context.ApplicationContext;
2324
import org.springframework.context.annotation.Bean;
2425
import org.springframework.context.annotation.Conditional;
2526
import org.springframework.context.annotation.Configuration;
2627
import org.springframework.context.annotation.Import;
2728
import org.springframework.grpc.autoconfigure.server.ConditionalOnGrpcServerEnabled;
29+
import org.springframework.grpc.autoconfigure.server.GrpcServerExecutorProvider;
2830
import org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration;
2931
import org.springframework.grpc.autoconfigure.server.exception.GrpcExceptionHandlerAutoConfiguration;
3032
import org.springframework.grpc.server.GlobalServerInterceptor;
31-
import org.springframework.grpc.server.ServerBuilderCustomizer;
3233
import org.springframework.grpc.server.exception.GrpcExceptionHandler;
3334
import org.springframework.grpc.server.security.GrpcSecurity;
3435
import org.springframework.grpc.server.security.SecurityContextServerInterceptor;
@@ -39,7 +40,6 @@
3940
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
4041
import org.springframework.security.web.SecurityFilterChain;
4142

42-
import io.grpc.ServerBuilder;
4343
import io.grpc.internal.GrpcUtil;
4444

4545
@ConditionalOnClass(ObjectPostProcessor.class)
@@ -91,9 +91,9 @@ public SecurityContextServerInterceptor securityContextInterceptor() {
9191
}
9292

9393
@Bean
94-
public <T extends ServerBuilder<T>> ServerBuilderCustomizer<T> securityContextExecutorCustomizer() {
95-
return (serverBuilder) -> serverBuilder
96-
.executor(new DelegatingSecurityContextExecutor(GrpcUtil.SHARED_CHANNEL_EXECUTOR.create()));
94+
@ConditionalOnMissingBean(GrpcServerExecutorProvider.class)
95+
public GrpcServerExecutorProvider grpcServerExecutorProvider() {
96+
return () -> new DelegatingSecurityContextExecutor(GrpcUtil.SHARED_CHANNEL_EXECUTOR.create());
9797
}
9898

9999
}

0 commit comments

Comments
 (0)