Skip to content

Commit

Permalink
Merge pull request #40484 from SOOHYUN-LIM
Browse files Browse the repository at this point in the history
* gh-40484:
  Polish "Add a customizer for Lettuce's ClientOptions"
  Add a customizer for Lettuce's ClientOptions

Closes gh-40484
  • Loading branch information
wilkinsona committed Jun 27, 2024
2 parents 4f6860f + 1dfb4c9 commit 895fbd7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand All @@ -23,7 +23,9 @@
* Callback interface that can be implemented by beans wishing to customize the
* {@link LettuceClientConfiguration} through a {@link LettuceClientConfigurationBuilder
* LettuceClientConfiguration.LettuceClientConfigurationBuilder} whilst retaining default
* auto-configuration.
* auto-configuration. To customize only the
* {@link LettuceClientConfiguration#getClientOptions() client options} of the
* configuration, use {@link LettuceClientOptionsBuilderCustomizer} instead.
*
* @author Mark Paluch
* @since 2.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2012-2024 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.boot.autoconfigure.data.redis;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.ClientOptions.Builder;

import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;

/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link ClientOptions} of the {@link LettuceClientConfiguration} through a
* {@link Builder} whilst retaining default auto-configuration. To customize the entire
* configuration, use {@link LettuceClientConfigurationBuilderCustomizer} instead.
*
* @author Soohyun Lim
* @since 3.4.0
*/
@FunctionalInterface
public interface LettuceClientOptionsBuilderCustomizer {

/**
* Customize the {@link Builder}.
* @param clientOptionsBuilder the builder to customize
*/
void customize(Builder clientOptionsBuilder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,34 @@ DefaultClientResources lettuceClientResources(ObjectProvider<ClientResourcesBuil
@ConditionalOnMissingBean(RedisConnectionFactory.class)
@ConditionalOnThreading(Threading.PLATFORM)
LettuceConnectionFactory redisConnectionFactory(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> clientConfigurationBuilderCustomizers,
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientOptionsBuilderCustomizers,
ClientResources clientResources) {
return createConnectionFactory(builderCustomizers, clientResources);
return createConnectionFactory(clientConfigurationBuilderCustomizers, clientOptionsBuilderCustomizers,
clientResources);
}

@Bean
@ConditionalOnMissingBean(RedisConnectionFactory.class)
@ConditionalOnThreading(Threading.VIRTUAL)
LettuceConnectionFactory redisConnectionFactoryVirtualThreads(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> clientConfigurationBuilderCustomizers,
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientOptionsBuilderCustomizers,
ClientResources clientResources) {
LettuceConnectionFactory factory = createConnectionFactory(builderCustomizers, clientResources);
LettuceConnectionFactory factory = createConnectionFactory(clientConfigurationBuilderCustomizers,
clientOptionsBuilderCustomizers, clientResources);
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("redis-");
executor.setVirtualThreads(true);
factory.setExecutor(executor);
return factory;
}

private LettuceConnectionFactory createConnectionFactory(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> clientConfigurationBuilderCustomizers,
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientOptionsBuilderCustomizers,
ClientResources clientResources) {
LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(builderCustomizers, clientResources,
getProperties().getLettuce().getPool());
LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(clientConfigurationBuilderCustomizers,
clientOptionsBuilderCustomizers, clientResources, getProperties().getLettuce().getPool());
return createLettuceConnectionFactory(clientConfig);
}

Expand All @@ -125,16 +130,17 @@ private LettuceConnectionFactory createLettuceConnectionFactory(LettuceClientCon
}

private LettuceClientConfiguration getLettuceClientConfiguration(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> clientConfigurationBuilderCustomizers,
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientOptionsBuilderCustomizers,
ClientResources clientResources, Pool pool) {
LettuceClientConfigurationBuilder builder = createBuilder(pool);
applyProperties(builder);
if (StringUtils.hasText(getProperties().getUrl())) {
customizeConfigurationFromUrl(builder);
}
builder.clientOptions(createClientOptions());
builder.clientOptions(createClientOptions(clientOptionsBuilderCustomizers));
builder.clientResources(clientResources);
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
clientConfigurationBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}

Expand Down Expand Up @@ -163,7 +169,8 @@ private void applyProperties(LettuceClientConfiguration.LettuceClientConfigurati
}
}

private ClientOptions createClientOptions() {
private ClientOptions createClientOptions(
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientConfigurationBuilderCustomizers) {
ClientOptions.Builder builder = initializeClientOptionsBuilder();
Duration connectTimeout = getProperties().getConnectTimeout();
if (connectTimeout != null) {
Expand All @@ -183,7 +190,9 @@ private ClientOptions createClientOptions() {
}
builder.sslOptions(sslOptionsBuilder.build());
}
return builder.timeoutOptions(TimeoutOptions.enabled()).build();
builder.timeoutOptions(TimeoutOptions.enabled());
clientConfigurationBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}

private ClientOptions.Builder initializeClientOptionsBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ void testCustomizeRedisConfiguration() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.isUseSsl()).isTrue();
assertThat(cf.getClientConfiguration().getClientOptions())
.hasValueSatisfying((options) -> assertThat(options.isAutoReconnect()).isFalse());
});
}

Expand Down Expand Up @@ -638,6 +640,11 @@ LettuceClientConfigurationBuilderCustomizer customizer() {
return LettuceClientConfigurationBuilder::useSsl;
}

@Bean
LettuceClientOptionsBuilderCustomizer clientOptionsBuilderCustomizer() {
return (builder) -> builder.autoReconnect(false);
}

}

@Configuration(proxyBeanMethods = false)
Expand Down

0 comments on commit 895fbd7

Please sign in to comment.