Skip to content

Commit

Permalink
Allow scripts to specify ClientOptions if desired
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed May 22, 2024
1 parent d692ed2 commit 0dfc51b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
19 changes: 16 additions & 3 deletions src/main/java/dev/magicmq/pyspigot/manager/redis/RedisManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.magicmq.pyspigot.manager.script.Script;
import dev.magicmq.pyspigot.manager.script.ScriptManager;
import io.lettuce.core.ClientOptions;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -23,21 +24,33 @@ private RedisManager() {
}

/**
* Initialize a new {@link ScriptRedisClient} with a connection to a remote redis server with the specified ip, port, and password. The connection to the remote redis server will be opened automatically when the client is created.
* Initialize a new {@link ScriptRedisClient} with a connection to a remote redis server with the specified ip, port, and password, using the default client options. The connection to the remote redis server will be opened automatically when the client is created.
* @param ip The IP of the redis server to connect to
* @param port The port of the redis server to connect to
* @param password The password for the redis server
* @return A {@link ScriptRedisClient} representing a client that is connected to the remote redis server
*/
public ScriptRedisClient openRedisClient(String ip, String port, String password) {
return openRedisClient(ip, port, password, null);
}

/**
* Initialize a new {@link ScriptRedisClient} with a connection to a remote redis server with the specified ip, port, and password, using the specified {@link io.lettuce.core.ClientOptions}. The connection to the remote redis server will be opened automatically when the client is created.
* <p>
* <b>Note:</b> This should be called from scripts only!
* @param ip The IP of the redis server to connect to
* @param port The port of the redis server to connect to
* @param password The password for the redis server
* @param clientOptions The {@link io.lettuce.core.ClientOptions} that should be used for the {@link io.lettuce.core.RedisClient}
* @return A {@link ScriptRedisClient} representing a client that is connected to the remote redis server
*/
public ScriptRedisClient openRedisClient(String ip, String port, String password) {
public ScriptRedisClient openRedisClient(String ip, String port, String password, ClientOptions clientOptions) {
Script script = ScriptManager.get().getScriptFromCallStack();
if (script == null)
throw new RuntimeException("No script found when initializing new redis client");

String uri = URLEncoder.encode("redis://" + password + "@" + ip + ":" + port + "/0", StandardCharsets.UTF_8);
ScriptRedisClient client = new ScriptRedisClient(script, uri);
ScriptRedisClient client = new ScriptRedisClient(script, uri, clientOptions);
activeClients.add(client);
if (!client.open()) {
script.getLogger().log(Level.SEVERE, "Redis client was not opened!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ScriptRedisClient {
private final Script script;
private final String uri;

private ClientOptions clientOptions;
private RedisClient client;
private StatefulRedisPubSubConnection<String, String> connection;
private List<ScriptPubSubListener> syncListeners;
Expand All @@ -28,11 +29,13 @@ public class ScriptRedisClient {
*
* @param script The script to which this ScriptRedisClient belongs
* @param uri The uri that specifies the ip, port, and password for connection to the remote redis server
* @param clientOptions The {@link io.lettuce.core.ClientOptions} that should be used for the RedisClient
*/
public ScriptRedisClient(Script script, String uri) {
public ScriptRedisClient(Script script, String uri, ClientOptions clientOptions) {
this.script = script;
this.uri = uri;

this.clientOptions = clientOptions;
this.syncListeners = new ArrayList<>();
this.asyncListeners = new ArrayList<>();
}
Expand All @@ -43,10 +46,13 @@ public ScriptRedisClient(Script script, String uri) {
*/
public boolean open() {
client = RedisClient.create(uri);
client.setOptions(ClientOptions.builder()
.autoReconnect(true)
.pingBeforeActivateConnection(true)
.build());
if (clientOptions != null)
client.setOptions(clientOptions);
else
client.setOptions(ClientOptions.builder()
.autoReconnect(true)
.pingBeforeActivateConnection(true)
.build());
client.getResources().eventBus().get().subscribe(event -> {
//TODO Log errors
});
Expand Down

0 comments on commit 0dfc51b

Please sign in to comment.