Skip to content

Commit

Permalink
Merge pull request #142 from swisspost/develop
Browse files Browse the repository at this point in the history
merge to master to trigger release
  • Loading branch information
dominik-cnx authored Oct 13, 2023
2 parents e68a8a6 + e0eb76d commit 3310cf9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
41 changes: 24 additions & 17 deletions src/main/java/org/swisspush/reststorage/DefaultRedisProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import io.vertx.redis.client.RedisConnection;
import io.vertx.redis.client.RedisClientType;
import io.vertx.redis.client.RedisOptions;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.redis.RedisProvider;
import org.swisspush.reststorage.util.ModuleConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -95,19 +96,19 @@ private Future<RedisAPI> connectToRedis() {
.setMaxPoolSize(redisMaxPoolSize)
.setMaxPoolWaiting(redisMaxPoolWaitingSize)
.setPoolRecycleTimeout(redisPoolRecycleTimeoutMs)
.setMaxWaitingHandlers(redisMaxPipelineWaitingSize);
if (configuration.isRedisClustered()) {
redisOptions.setType(RedisClientType.CLUSTER);
redisOptions.addConnectionString(createConnectString());
} else {
redisOptions.setConnectionString(createConnectString());
}
.setMaxWaitingHandlers(redisMaxPipelineWaitingSize)
.setType(configuration.getRedisClientType());

createConnectStrings().forEach(redisOptions::addConnectionString);
redis = Redis.createClient(vertx, redisOptions);

redis.connect().onSuccess(conn -> {
log.info("Successfully connected to redis");
client = conn;
client.close();

if (configuration.getRedisClientType() == RedisClientType.STANDALONE) {
client.close();
}

// make sure the client is reconnected on error
// eg, the underlying TCP connection is closed but the client side doesn't know it yet
Expand Down Expand Up @@ -138,16 +139,22 @@ private Future<RedisAPI> connectToRedis() {
return promise.future();
}

private String createConnectString() {
StringBuilder connectionStringBuilder = new StringBuilder();
connectionStringBuilder.append(configuration.isRedisEnableTls() ? "rediss://" : "redis://");
String redisUser = configuration.getRedisUser();
private List<String> createConnectStrings() {
String redisPassword = configuration.getRedisPassword();
if (StringUtils.isNotEmpty(redisUser) && StringUtils.isNotEmpty(redisPassword)) {
connectionStringBuilder.append(configuration.getRedisUser()).append(":").append(redisPassword).append("@");
String redisUser = configuration.getRedisUser();
StringBuilder connectionStringPrefixBuilder = new StringBuilder();
connectionStringPrefixBuilder.append(configuration.isRedisEnableTls() ? "rediss://" : "redis://");
if (redisUser != null && !redisUser.isEmpty()) {
connectionStringPrefixBuilder.append(redisUser).append(":").append((redisPassword == null ? "" : redisPassword)).append("@");
}
List<String> connectionString = new ArrayList<>();
String connectionStringPrefix = connectionStringPrefixBuilder.toString();
for (int i = 0; i < configuration.getRedisHosts().size(); i++) {
String host = configuration.getRedisHosts().get(i);
int port = configuration.getRedisPorts().get(i);
connectionString.add(connectionStringPrefix + host + ":" + port);
}
connectionStringBuilder.append(configuration.getRedisHost()).append(":").append(configuration.getRedisPort());
return connectionStringBuilder.toString();
return connectionString;
}

private void attemptReconnect(int retry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.swisspush.reststorage.util;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.vertx.core.json.JsonObject;
import io.vertx.redis.client.RedisClientType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Utility class to configure the RestStorageModule.
*
* @author https://github.com/mcweba [Marc-Andre Weber]
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class ModuleConfiguration {

private static final Logger log = LoggerFactory.getLogger(ModuleConfiguration.class);
Expand All @@ -29,10 +34,11 @@ public enum StorageType {
private String prefix = "";
private String storageAddress = "resource-storage";
private Map<String, String> editorConfig = null;
private String redisHost = "localhost";
private int redisPort = 6379;
private List<String> redisHosts = Collections.singletonList("localhost");
private List<Integer> redisPorts = Collections.singletonList(6379);
private boolean redisEnableTls;
private boolean redisClustered;
private RedisClientType redisClientType = RedisClientType.STANDALONE;

/**
* @deprecated Instance authentication is considered as legacy. With Redis from 6.x on the ACL authentication method should be used.
*/
Expand Down Expand Up @@ -116,12 +122,22 @@ public ModuleConfiguration editorConfig(Map<String, String> editorConfig) {
}

public ModuleConfiguration redisHost(String redisHost) {
this.redisHost = redisHost;
this.redisHosts = Collections.singletonList(redisHost);
return this;
}

public ModuleConfiguration redisPort(int redisPort) {
this.redisPort = redisPort;
this.redisPorts = Collections.singletonList(redisPort);
return this;
}

public ModuleConfiguration redisHosts(List<String> redisHosts) {
this.redisHosts = redisHosts;
return this;
}

public ModuleConfiguration redisPorts(List<Integer> redisPorts) {
this.redisPorts = redisPorts;
return this;
}

Expand All @@ -130,8 +146,8 @@ public ModuleConfiguration redisEnableTls(boolean redisEnableTls) {
return this;
}

public ModuleConfiguration redisClustered(boolean redisClustered) {
this.redisClustered = redisClustered;
public ModuleConfiguration redisClientType(RedisClientType redisClientType) {
this.redisClientType = redisClientType;
return this;
}

Expand Down Expand Up @@ -287,11 +303,18 @@ public Map<String, String> getEditorConfig() {
}

public String getRedisHost() {
return redisHost;
return redisHosts.get(0);
}

public List<String> getRedisHosts() {
return redisHosts;
}

public int getRedisPort() {
return redisPort;
return redisPorts.get(0);
}
public List<Integer> getRedisPorts() {
return redisPorts;
}

public int getRedisReconnectAttempts() {
Expand All @@ -314,10 +337,6 @@ public boolean isRedisEnableTls() {
return redisEnableTls;
}

public boolean isRedisClustered() {
return redisClustered;
}

public String getRedisAuth() {
return redisAuth;
}
Expand All @@ -329,7 +348,9 @@ public String getRedisPassword() {
public String getRedisUser() {
return redisUser;
}

public RedisClientType getRedisClientType() {
return redisClientType;
}
public String getExpirablePrefix() {
return expirablePrefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Collections;
import java.util.HashMap;

import static org.swisspush.reststorage.util.ModuleConfiguration.StorageType;
Expand Down Expand Up @@ -272,8 +273,8 @@ public void testGetOverriddenFromJsonObject(TestContext testContext){
json.put("prefix", "newprefix");
json.put("storageAddress", "newStorageAddress");
json.put("editorConfig", new JsonObject().put("myKey", "myValue"));
json.put("redisHost", "newredishost");
json.put("redisPort", 4321);
json.put("redisHosts", Collections.singletonList("newredishost"));
json.put("redisPorts", Collections.singletonList(4321));
json.put("maxRedisWaitingHandlers", 4096);
json.put("expirablePrefix", "newExpirablePrefix");
json.put("resourcesPrefix", "newResourcesPrefix");
Expand Down

0 comments on commit 3310cf9

Please sign in to comment.