Skip to content

Commit

Permalink
Polish "Add spring.data.redis.lettuce.read-from property"
Browse files Browse the repository at this point in the history
See gh-42588

Co-authored-by: Stephane Nicoll <[email protected]>
  • Loading branch information
philwebb and snicoll committed Jan 7, 2025
1 parent fd11598 commit e4bcda2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,25 @@ private void applyProperties(LettuceClientConfiguration.LettuceClientConfigurati
}
}

private static ReadFrom getReadFrom(String readFrom) {
private ReadFrom getReadFrom(String readFrom) {
int index = readFrom.indexOf(':');
if (index == -1) {
String name = readFrom.replaceAll("-", "");
return ReadFrom.valueOf(name);
return ReadFrom.valueOf(getCanonicalReadFromName(readFrom));
}
String name = readFrom.substring(0, index).replaceAll("-", "");
String name = getCanonicalReadFromName(readFrom.substring(0, index));
String value = readFrom.substring(index + 1);
return ReadFrom.valueOf(name + ":" + value);
}

private String getCanonicalReadFromName(String name) {
StringBuilder canonicalName = new StringBuilder(name.length());
name.chars()
.filter(Character::isLetterOrDigit)
.map(Character::toLowerCase)
.forEach((c) -> canonicalName.append((char) c));
return canonicalName.toString();
}

private ClientOptions createClientOptions(
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientConfigurationBuilderCustomizers) {
ClientOptions.Builder builder = initializeClientOptionsBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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 Down Expand Up @@ -122,7 +122,7 @@ void testOverrideRedisConfiguration() {
});
}

@ParameterizedTest
@ParameterizedTest(name = "{0}")
@MethodSource
void shouldConfigureLettuceReadFromProperty(String type, ReadFrom readFrom) {
this.contextRunner.withPropertyValues("spring.data.redis.lettuce.read-from:" + type).run((context) -> {
Expand All @@ -132,6 +132,14 @@ void shouldConfigureLettuceReadFromProperty(String type, ReadFrom readFrom) {
});
}

static Stream<Arguments> shouldConfigureLettuceReadFromProperty() {
return Stream.of(Arguments.of("any", ReadFrom.ANY), Arguments.of("any-replica", ReadFrom.ANY_REPLICA),
Arguments.of("lowest-latency", ReadFrom.LOWEST_LATENCY), Arguments.of("replica", ReadFrom.REPLICA),
Arguments.of("replica-preferred", ReadFrom.REPLICA_PREFERRED),
Arguments.of("upstream", ReadFrom.UPSTREAM),
Arguments.of("upstream-preferred", ReadFrom.UPSTREAM_PREFERRED));
}

@Test
void shouldConfigureLettuceRegexReadFromProperty() {
RedisClusterNode node1 = createRedisNode("redis-node-1.region-1.example.com");
Expand Down Expand Up @@ -688,14 +696,6 @@ private String getUserName(LettuceConnectionFactory factory) {
return ReflectionTestUtils.invokeMethod(factory, "getRedisUsername");
}

static Stream<Arguments> shouldConfigureLettuceReadFromProperty() {
return Stream.of(Arguments.of("any", ReadFrom.ANY), Arguments.of("any-replica", ReadFrom.ANY_REPLICA),
Arguments.of("lowest-latency", ReadFrom.LOWEST_LATENCY), Arguments.of("replica", ReadFrom.REPLICA),
Arguments.of("replica-preferred", ReadFrom.REPLICA_PREFERRED),
Arguments.of("upstream", ReadFrom.UPSTREAM),
Arguments.of("upstream-preferred", ReadFrom.UPSTREAM_PREFERRED));
}

private RedisClusterNode createRedisNode(String host) {
RedisClusterNode node = new RedisClusterNode();
node.setUri(RedisURI.Builder.redis(host).build());
Expand Down

0 comments on commit e4bcda2

Please sign in to comment.