Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import javax.net.ssl.SSLParameters;

import com.bencodez.simpleapi.servercomm.codec.JsonEnvelope;
import com.bencodez.simpleapi.servercomm.codec.JsonEnvelopeCodec;

Expand Down Expand Up @@ -37,31 +39,44 @@ public abstract class RedisHandler {
private static final long RECONNECT_MAX_MS = 30000L;

public RedisHandler(String host, int port, String username, String password, int dbIndex) {
this(host, port, username, password, dbIndex, false);
}

public RedisHandler(String host, int port, String username, String password, int dbIndex, boolean ssl) {
Objects.requireNonNull(host, "host");
this.endpoint = new HostAndPort(host, port);
this.clientConfig = buildClientConfig(username, password, dbIndex, ssl);

JedisPoolConfig publisherPoolConfig = new JedisPoolConfig();
publisherPoolConfig.setTestOnBorrow(true);
this.publisherPool = new JedisPool(publisherPoolConfig, endpoint, clientConfig);
this.publisherExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(PUBLISH_QUEUE_CAPACITY), runnable -> {
Thread thread = new Thread(runnable, "RedisPublishThread-" + endpoint);
thread.setDaemon(true);
return thread;
}, new ThreadPoolExecutor.AbortPolicy());
}

static DefaultJedisClientConfig buildClientConfig(String username, String password, int dbIndex, boolean ssl) {
DefaultJedisClientConfig.Builder cfg = DefaultJedisClientConfig.builder()
.database(dbIndex)
.ssl(ssl)
.connectionTimeoutMillis(2000)
.socketTimeoutMillis(2000);
if (ssl) {
SSLParameters sslParameters = new SSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
cfg.sslParameters(sslParameters);
}

if (username != null && !username.isEmpty()) {
cfg.user(username);
}
if (password != null && !password.isEmpty()) {
cfg.password(password);
}

this.clientConfig = cfg.build();
JedisPoolConfig publisherPoolConfig = new JedisPoolConfig();
publisherPoolConfig.setTestOnBorrow(true);
this.publisherPool = new JedisPool(publisherPoolConfig, endpoint, clientConfig);
this.publisherExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(PUBLISH_QUEUE_CAPACITY), runnable -> {
Thread thread = new Thread(runnable, "RedisPublishThread-" + endpoint);
thread.setDaemon(true);
return thread;
}, new ThreadPoolExecutor.AbortPolicy());
return cfg.build();
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bencodez.simpleapi.servercomm.redis;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class RedisHandlerClientConfigTest {

@Test
void tlsCanBeEnabled() {
var config = RedisHandler.buildClientConfig("", "", 0, true);

assertTrue(config.isSsl());
assertEquals("HTTPS", config.getSslParameters().getEndpointIdentificationAlgorithm());
}

@Test
void tlsRemainsDisabledByDefault() {
assertFalse(RedisHandler.buildClientConfig("", "", 0, false).isSsl());
}
}
Loading