Skip to content

Commit

Permalink
add redis MAX_CONNECTION_ATTEMPTS
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed May 7, 2022
1 parent 76237fa commit d4b6931
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@
import java.util.Properties;
import java.util.Set;

import static com.tvd12.ezydata.redis.setting.EzyRedisSettings.MAX_CONNECTION_ATTEMPTS;

public class EzyRedisProxyFactory extends EzyLoggable {

protected final EzyRedisSettings settings;
protected final EzyEntityCodec entityCodec;
protected final EzyRedisClientPool clientPool;
protected final int maxConnectionAttempts;

protected EzyRedisProxyFactory(Builder builder) {
this.settings = builder.settings;
this.entityCodec = builder.entityCodec;
this.clientPool = builder.clientPool;
this.maxConnectionAttempts = builder.maxConnectionAttempts;
}

public static Builder builder() {
Expand All @@ -63,6 +67,9 @@ private EzyRedisClient getRedisClient() {
try {
return clientPool.getClient();
} catch (Exception e) {
if (retryCount >= maxConnectionAttempts) {
throw e;
}
logger.error(
"can not get redis client, retry count: {}",
(++retryCount),
Expand All @@ -78,6 +85,7 @@ public static class Builder implements EzyBuilder<EzyRedisProxyFactory> {
protected EzyReflection reflection;
protected Set<String> packagesToScan;
protected Properties properties;
protected int maxConnectionAttempts;
protected EzyRedisSettings settings;
protected EzyEntityCodec entityCodec;
protected EzyRedisClientPool clientPool;
Expand Down Expand Up @@ -148,13 +156,21 @@ public EzyRedisProxyFactory build() {
if (packagesToScan.size() > 0) {
this.reflection = new EzyReflectionProxy(packagesToScan);
}
this.extractProperties();
this.prepareMapNameTranslator();
this.prepareSettings();
this.prepareEntityCodec();
this.prepareClientPool();
return new EzyRedisProxyFactory(this);
}

private void extractProperties() {
this.maxConnectionAttempts = (int) properties.getOrDefault(
MAX_CONNECTION_ATTEMPTS,
0
);
}

private void prepareSettings() {
if (settings != null) {
return;
Expand Down Expand Up @@ -242,7 +258,9 @@ private void prepareClientPool() {
protected void prepareMapNameTranslator() {
if (mapNameTranslator == null) {
EzyNamingCase namingCase =
EzyNamingCase.of(properties.getProperty(EzyRedisSettings.MAP_NAMING_CASE));
EzyNamingCase.of(
properties.getProperty(EzyRedisSettings.MAP_NAMING_CASE)
);
String ignoredSuffix =
properties.getProperty(EzyRedisSettings.MAP_NAMING_IGNORED_SUFFIX);
mapNameTranslator(namingCase, ignoredSuffix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface EzyRedisSettings {
String MAPS = "redis.maps";
String MAP_NAMING_CASE = "redis.map_naming.case";
String MAP_NAMING_IGNORED_SUFFIX = "redis.map_naming.ignored_suffix";
String MAX_CONNECTION_ATTEMPTS = "redis.max_connection_attempts";
String CHANNELS = "redis.channels";
String MAP_KEY_TYPE = "key_type";
String MAP_VALUE_TYPE = "value_type";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import com.tvd12.test.util.RandomUtil;
import org.testng.annotations.Test;

import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static com.tvd12.ezydata.redis.setting.EzyRedisSettings.MAX_CONNECTION_ATTEMPTS;
import static org.mockito.Mockito.*;

public class EzyRedisProxyFactoryTest {
Expand Down Expand Up @@ -85,7 +87,10 @@ public void getRedisClientRetryTest() {
}
throw new IllegalStateException("just test");
});
Properties properties = new Properties();
properties.put(MAX_CONNECTION_ATTEMPTS, Integer.MAX_VALUE);
EzyRedisProxyFactory factory = new EzyRedisProxyFactory.Builder()
.properties(properties)
.settingsBuilder(new EzyRedisSettingsBuilder())
.clientPool(clientPool)
.build();
Expand All @@ -96,6 +101,28 @@ public void getRedisClientRetryTest() {
verify(clientPool, times(2)).getClient();
}

@Test
public void getRedisClientRetryButMaxConnectionAttemptsWasNotSetTest() {
// given
EzyRedisClientPool clientPool = mock(EzyRedisClientPool.class);

Exception exception = new IllegalStateException("just test");
when(clientPool.getClient()).thenAnswer(it -> {
throw exception;
});
EzyRedisProxyFactory factory = new EzyRedisProxyFactory.Builder()
.settingsBuilder(new EzyRedisSettingsBuilder())
.clientPool(clientPool)
.build();

// when
Throwable e = Asserts.assertThrows(factory::newRedisProxy);

// then
Asserts.assertEquals(e, exception);
verify(clientPool, times(1)).getClient();
}

@Test
public void getRedisClientInterruptTest() {
// given
Expand All @@ -104,7 +131,10 @@ public void getRedisClientInterruptTest() {
when(clientPool.getClient()).thenAnswer(it -> {
throw new IllegalStateException("just test");
});
Properties properties = new Properties();
properties.put(MAX_CONNECTION_ATTEMPTS, Integer.MAX_VALUE);
EzyRedisProxyFactory factory = new EzyRedisProxyFactory.Builder()
.properties(properties)
.settingsBuilder(new EzyRedisSettingsBuilder())
.clientPool(clientPool)
.build();
Expand Down

0 comments on commit d4b6931

Please sign in to comment.