Skip to content

DATAREDIS-576 - Support client name for Redis connections using Lettuce. #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

REDIS_VERSION:=4.0.1
REDIS_VERSION:=4.0.2
SPRING_PROFILE?=ci

#######
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAREDIS-576-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* <li>Optional {@link HostnameVerifier}</li>
* <li>Whether to use connection-pooling</li>
* <li>Optional {@link GenericObjectPoolConfig}</li>
* <li>Optional client name</li>
* <li>Connect {@link Duration timeout}</li>
* <li>Read {@link Duration timeout}</li>
* </ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,71 +37,89 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
private final boolean startTls;
private final Optional<ClientResources> clientResources;
private final Optional<ClientOptions> clientOptions;
private final Optional<String> clientName;
private final Duration timeout;
private final Duration shutdownTimeout;

DefaultLettuceClientConfiguration(boolean useSsl, boolean verifyPeer, boolean startTls,
@Nullable ClientResources clientResources, @Nullable ClientOptions clientOptions, Duration timeout,
Duration shutdownTimeout) {
@Nullable ClientResources clientResources, @Nullable ClientOptions clientOptions, @Nullable String clientName,
Duration timeout, Duration shutdownTimeout) {

this.useSsl = useSsl;
this.verifyPeer = verifyPeer;
this.startTls = startTls;
this.clientResources = Optional.ofNullable(clientResources);
this.clientOptions = Optional.ofNullable(clientOptions);
this.clientName = Optional.ofNullable(clientName);
this.timeout = timeout;
this.shutdownTimeout = shutdownTimeout;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#useSsl()
*/
@Override
public boolean isUseSsl() {
return useSsl;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isVerifyPeer()
*/
@Override
public boolean isVerifyPeer() {
return verifyPeer;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isStartTls()
*/
@Override
public boolean isStartTls() {
return startTls;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientResources()
*/
@Override
public Optional<ClientResources> getClientResources() {
return clientResources;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientOptions()
*/
@Override
public Optional<ClientOptions> getClientOptions() {
return clientOptions;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
*/
@Override
public Optional<String> getClientName() {
return clientName;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getTimeout()
*/
@Override
public Duration getCommandTimeout() {
return timeout;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getShutdownTimeout()
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ public Optional<ClientOptions> getClientOptions() {
return clientConfiguration.getClientOptions();
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
*/
@Override
public Optional<String> getClientName() {
return clientConfiguration.getClientName();
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getCommandTimeout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* <li>Whether to use StartTLS</li>
* <li>Optional {@link ClientResources}</li>
* <li>Optional {@link ClientOptions}</li>
* <li>Optional client name</li>
* <li>Client {@link Duration timeout}</li>
* <li>Shutdown {@link Duration timeout}</li>
* </ul>
Expand Down Expand Up @@ -75,6 +76,12 @@ public interface LettuceClientConfiguration {
*/
Optional<ClientOptions> getClientOptions();

/**
* @return the optional client name to be set with {@code CLIENT SETNAME}.
* @since 2.1
*/
Optional<String> getClientName();

/**
* @return the timeout.
*/
Expand Down Expand Up @@ -109,6 +116,8 @@ static LettuceClientConfigurationBuilder builder() {
* <dd>none</dd>
* <dt>Client Resources</dt>
* <dd>none</dd>
* <dt>Client name</dt>
* <dd>none</dd>
* <dt>Connect Timeout</dt>
* <dd>60 Seconds</dd>
* <dt>Shutdown Timeout</dt>
Expand All @@ -132,6 +141,7 @@ class LettuceClientConfigurationBuilder {
boolean startTls;
@Nullable ClientResources clientResources;
@Nullable ClientOptions clientOptions;
@Nullable String clientName;
Duration timeout = Duration.ofSeconds(RedisURI.DEFAULT_TIMEOUT);
Duration shutdownTimeout = Duration.ofMillis(100);

Expand Down Expand Up @@ -178,6 +188,22 @@ public LettuceClientConfigurationBuilder clientOptions(ClientOptions clientOptio
return this;
}

/**
* Configure a {@code clientName} to be set with {@code CLIENT SETNAME}.
*
* @param clientName must not be {@literal null} or empty.
* @return {@literal this} builder.
* @throws IllegalArgumentException if clientName is {@literal null} or empty.
* @since 2.1
*/
public LettuceClientConfigurationBuilder clientName(String clientName) {

Assert.hasText(clientName, "Client name must not be null or empty!");

this.clientName = clientName;
return this;
}

/**
* Configure a command timeout.
*
Expand Down Expand Up @@ -216,7 +242,7 @@ public LettuceClientConfigurationBuilder shutdownTimeout(Duration shutdownTimeou
public LettuceClientConfiguration build() {

return new DefaultLettuceClientConfiguration(useSsl, verifyPeer, startTls, clientResources, clientOptions,
timeout, shutdownTimeout);
clientName, timeout, shutdownTimeout);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,36 @@ public void setDatabase(int index) {
standaloneConfig.setDatabase(index);
}

/**
* Returns the client name.
*
* @return the client name or {@literal null} if not set.
* @since 2.1
*/
@Nullable
public String getClientName() {
return clientConfiguration.getClientName().orElse(null);
}

/**
* Sets the client name used by this connection factory.
*
* @param clientName the client name. Can be {@literal null}.
* @since 2.1
* @deprecated configure the client name using {@link LettuceClientConfiguration}.
* @throws IllegalStateException if {@link LettuceClientConfiguration} is immutable.
*/
@Deprecated
public void setClientName(@Nullable String clientName) {
this.getMutableConfiguration().setClientName(clientName);
}

/**
* Returns the password used for authenticating with the Redis server.
*
* @return password for authentication.
* @return password for authentication or {@literal null} if not set.
*/
@Nullable
public String getPassword() {
return getRedisPassword().map(String::new).orElse(null);
}
Expand Down Expand Up @@ -849,6 +874,7 @@ private RedisURI getSentinelRedisURI() {
RedisURI redisUri = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);

getRedisPassword().toOptional().ifPresent(redisUri::setPassword);
clientConfiguration.getClientName().ifPresent(redisUri::setClientName);
redisUri.setTimeout(clientConfiguration.getCommandTimeout());

return redisUri;
Expand All @@ -859,6 +885,7 @@ private RedisURI createRedisURIAndApplySettings(String host, int port) {
RedisURI.Builder builder = RedisURI.Builder.redis(host, port);

getRedisPassword().toOptional().ifPresent(builder::withPassword);
clientConfiguration.getClientName().ifPresent(builder::withClientName);

builder.withSsl(clientConfiguration.isUseSsl());
builder.withVerifyPeer(clientConfiguration.isVerifyPeer());
Expand Down Expand Up @@ -890,93 +917,119 @@ private long getClientTimeout() {
* Mutable implementation of {@link LettuceClientConfiguration}.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
static class MutableLettuceClientConfiguration implements LettuceClientConfiguration {

private boolean useSsl;
private boolean verifyPeer = true;
private boolean startTls;
private @Nullable ClientResources clientResources;
private @Nullable String clientName;
private Duration timeout = Duration.ofSeconds(RedisURI.DEFAULT_TIMEOUT);
private Duration shutdownTimeout = Duration.ofMillis(100);

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isUseSsl()
*/
@Override
public boolean isUseSsl() {
return useSsl;
}

public void setUseSsl(boolean useSsl) {
void setUseSsl(boolean useSsl) {
this.useSsl = useSsl;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isVerifyPeer()
*/
@Override
public boolean isVerifyPeer() {
return verifyPeer;
}

public void setVerifyPeer(boolean verifyPeer) {
void setVerifyPeer(boolean verifyPeer) {
this.verifyPeer = verifyPeer;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isStartTls()
*/
@Override
public boolean isStartTls() {
return startTls;
}

public void setStartTls(boolean startTls) {
void setStartTls(boolean startTls) {
this.startTls = startTls;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientResources()
*/
@Override
public Optional<ClientResources> getClientResources() {
return Optional.ofNullable(clientResources);
}

public void setClientResources(ClientResources clientResources) {
void setClientResources(ClientResources clientResources) {
this.clientResources = clientResources;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientOptions()
*/
@Override
public Optional<ClientOptions> getClientOptions() {
return Optional.empty();
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
*/
@Override
public Optional<String> getClientName() {
return Optional.ofNullable(clientName);
}

/**
* @param clientName can be {@litearl null}.
* @since 2.1
*/
void setClientName(@Nullable String clientName) {
this.clientName = clientName;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getTimeout()
*/
@Override
public Duration getCommandTimeout() {
return timeout;
}

public void setTimeout(Duration timeout) {
void setTimeout(Duration timeout) {
this.timeout = timeout;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getShutdownTimeout()
*/
@Override
public Duration getShutdownTimeout() {
return shutdownTimeout;
}

public void setShutdownTimeout(Duration shutdownTimeout) {
void setShutdownTimeout(Duration shutdownTimeout) {
this.shutdownTimeout = shutdownTimeout;
}
}
Expand Down
Loading