Skip to content

Commit

Permalink
Redis sentinel should respect database in url also
Browse files Browse the repository at this point in the history
Database property should be ignored if url is used.

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
  • Loading branch information
quaff committed Jan 23, 2025
1 parent 5f71576 commit 4d3af33
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public Standalone getStandalone() {
if (this.properties.getUrl() != null) {
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
(connectionInfo.getDatabase() != null) ? connectionInfo.getDatabase()
: this.properties.getDatabase());
connectionInfo.getDatabase());
}
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
}
Expand All @@ -77,7 +76,7 @@ public Sentinel getSentinel() {

@Override
public int getDatabase() {
return PropertiesRedisConnectionDetails.this.properties.getDatabase();
return getStandalone().getDatabase();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ static final class ConnectionInfo {

private final String password;

private final Integer database;
private final int database;

private ConnectionInfo(URI uri, boolean useSsl, String username, String password, Integer database) {
private ConnectionInfo(URI uri, boolean useSsl, String username, String password, int database) {
this.uri = uri;
this.useSsl = useSsl;
this.username = username;
Expand All @@ -217,7 +217,7 @@ String getPassword() {
return this.password;
}

Integer getDatabase() {
int getDatabase() {
return this.database;
}

Expand All @@ -242,7 +242,7 @@ static ConnectionInfo of(String url) {
password = candidate;
}
}
Integer database = null;
int database = 0;
if (StringUtils.hasText(uri.getPath())) {
String[] pathSplit = uri.getPath().split("/", 2);
if (pathSplit.length > 1 && !pathSplit[1].isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void standaloneIsConfiguredFromUrlWithoutDatabase() {
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
assertThat(standalone.getHost()).isEqualTo("example.com");
assertThat(standalone.getPort()).isEqualTo(1234);
assertThat(standalone.getDatabase()).isEqualTo(5);
assertThat(standalone.getDatabase()).isEqualTo(0);
}

@Test
Expand Down Expand Up @@ -144,9 +144,22 @@ void sentinelIsConfigured() {
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
this.properties.setSentinel(sentinel);
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(5);
}

@Test
void sentinelDatabaseIsConfiguredFromUrl() {
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
this.properties.setSentinel(sentinel);
this.properties.setUrl("redis://example.com:1234/9999");
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(9999);
}

}

0 comments on commit 4d3af33

Please sign in to comment.