Skip to content

Commit 0d548fd

Browse files
committed
Apply keepalive settings only when enableKeepAlive is true
This commit fixes an issue where we were applying keep-alive settings even when we had disabled the feature. Closes: gh-383 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent 5efb403 commit 0d548fd

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

spring-grpc-client-spring-boot-autoconfigure/src/main/java/org/springframework/boot/grpc/client/autoconfigure/ClientPropertiesChannelBuilderCustomizer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @param <T> the type of the builder
3838
* @author David Syer
3939
* @author Chris Bono
40+
* @author Andrey Litvitski
4041
*/
4142
class ClientPropertiesChannelBuilderCustomizer<T extends ManagedChannelBuilder<T>>
4243
implements GrpcChannelBuilderCustomizer<T> {
@@ -55,12 +56,14 @@ public void customize(String target, T builder) {
5556
if (targetAllowsLoadBalancer(target)) {
5657
mapper.from(channel.getDefaultLoadBalancingPolicy()).to(builder::defaultLoadBalancingPolicy);
5758
}
59+
if (channel.isEnableKeepAlive()) {
60+
mapper.from(channel.getKeepAliveTime()).to(durationProperty(builder::keepAliveTime));
61+
mapper.from(channel.getKeepAliveTimeout()).to(durationProperty(builder::keepAliveTimeout));
62+
mapper.from(channel.isKeepAliveWithoutCalls()).to(builder::keepAliveWithoutCalls);
63+
}
5864
mapper.from(channel.getMaxInboundMessageSize()).asInt(DataSize::toBytes).to(builder::maxInboundMessageSize);
5965
mapper.from(channel.getMaxInboundMetadataSize()).asInt(DataSize::toBytes).to(builder::maxInboundMetadataSize);
60-
mapper.from(channel.getKeepAliveTime()).to(durationProperty(builder::keepAliveTime));
61-
mapper.from(channel.getKeepAliveTimeout()).to(durationProperty(builder::keepAliveTimeout));
6266
mapper.from(channel.getIdleTimeout()).to(durationProperty(builder::idleTimeout));
63-
mapper.from(channel.isKeepAliveWithoutCalls()).to(builder::keepAliveWithoutCalls);
6467
Map<String, Object> defaultServiceConfig = channel.extractServiceConfig();
6568
if (channel.getHealth().isEnabled()) {
6669
String serviceNameToCheck = (channel.getHealth().getServiceName() != null)

spring-grpc-client-spring-boot-autoconfigure/src/test/java/org/springframework/boot/grpc/client/autoconfigure/GrpcClientAutoConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.boot.grpc.client.autoconfigure;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyBoolean;
22+
import static org.mockito.ArgumentMatchers.anyLong;
2023
import static org.mockito.ArgumentMatchers.anyMap;
2124
import static org.mockito.BDDMockito.then;
2225
import static org.mockito.Mockito.inOrder;
@@ -63,6 +66,7 @@
6366
* Tests for {@link GrpcClientAutoConfiguration}.
6467
*
6568
* @author Chris Bono
69+
* @author Andrey Litvitski
6670
*/
6771
@SuppressWarnings({ "unchecked" })
6872
class GrpcClientAutoConfigurationTests {
@@ -246,6 +250,21 @@ void userDefinedCustomizerCanRunBeforeAndAfterClientPropsCustomizer() {
246250
});
247251
}
248252

253+
@Test
254+
void keepAliveSettingsNotAppliedWhenDisabled() {
255+
this.contextRunner()
256+
.withPropertyValues("spring.grpc.client.default-channel.enable-keep-alive=false")
257+
.run((context) -> {
258+
var customizer = context.getBean("clientPropertiesChannelCustomizer",
259+
GrpcChannelBuilderCustomizer.class);
260+
ManagedChannelBuilder<?> builder = Mockito.mock();
261+
customizer.customize("default", builder);
262+
then(builder).should(never()).keepAliveTime(anyLong(), any());
263+
then(builder).should(never()).keepAliveTimeout(anyLong(), any());
264+
then(builder).should(never()).keepAliveWithoutCalls(anyBoolean());
265+
});
266+
}
267+
249268
@Test
250269
void clientScanConfigurationAutoConfiguredAsExpected() {
251270
this.contextRunner().run((context) -> assertThat(context).hasSingleBean(ClientScanConfiguration.class));

0 commit comments

Comments
 (0)