Skip to content

Commit

Permalink
Merge pull request #1948 from bcgov/feature/TCVP-3037-refresh-oracle-…
Browse files Browse the repository at this point in the history
…data-api-connection-on-redis-restart

TCVP-3037: Refresh Oracle Data Api Connection on Redis Restart
  • Loading branch information
pbolduc committed Sep 3, 2024
2 parents 8e0b99c + e770960 commit 528fc05
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/backend/oracle-data-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>

<!-- JPA dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.bc.gov.open.jag.tco.oracledataapi.config;

import java.time.Duration;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -9,9 +11,13 @@
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.util.CollectionUtils;

import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;

@Configuration
public class RedisConfig {

Expand Down Expand Up @@ -45,7 +51,22 @@ else if (redisProperties.getCluster() != null && !CollectionUtils.isEmpty(redisP
if(redisProperties.getCluster().getMaxRedirects() != null)
config.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());

return new LettuceConnectionFactory(config);
// Enable adaptive topology refreshing
ClusterClientOptions clientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(ClusterTopologyRefreshOptions.builder()
// The MOVED_REDIRECT trigger causes a topology refresh when the client receives a MOVED redirect from a Redis server.
// The PERSISTENT_RECONNECTS trigger causes a topology refresh when the client cannot reconnect to a Redis server for a certain period of time.
.enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
// If a topology refresh is triggered, the client will wait for this period of time before triggering another topology refresh.
.adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) // default is 30 seconds
.build())
.build();

LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.clientOptions(clientOptions)
.build();

return new LettuceConnectionFactory(config, clientConfig);
}
// Standalone mode (for local development)
else {
Expand Down

0 comments on commit 528fc05

Please sign in to comment.