2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-22 20:28:00 +00:00

Better guard against failed redis connection attempts. Also reformat code.

This commit is contained in:
Tux 2013-12-04 16:54:51 -05:00
parent 8c824cc397
commit 080dab084a
4 changed files with 54 additions and 29 deletions

View File

@ -58,15 +58,18 @@
<relocations> <relocations>
<relocation> <relocation>
<pattern>redis.clients.jedis</pattern> <pattern>redis.clients.jedis</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis</shadedPattern> <shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis
</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>redis.clients.util</pattern> <pattern>redis.clients.util</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil</shadedPattern> <shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil
</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>org.apache.commons</pattern> <pattern>org.apache.commons</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commons</shadedPattern> <shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commons
</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
</configuration> </configuration>

View File

@ -18,11 +18,13 @@ import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import redis.clients.jedis.*; import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.exceptions.JedisException;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
/** /**
* The RedisBungee plugin. * The RedisBungee plugin.
@ -52,17 +54,18 @@ public class RedisBungee extends Plugin implements Listener {
} }
public int getCount() { public int getCount() {
Jedis rsc = pool.getResource(); int c = plugin.getProxy().getOnlineCount();
int c = 0; if (pool != null) {
try { Jedis rsc = pool.getResource();
c = plugin.getProxy().getOnlineCount(); try {
for (String i : getConfiguration().getLinkedServers()) { for (String i : getConfiguration().getLinkedServers()) {
if (i.equals(configuration.getServerId())) continue; if (i.equals(configuration.getServerId())) continue;
if (rsc.exists("server:" + i + ":playerCount")) if (rsc.exists("server:" + i + ":playerCount"))
c += Integer.valueOf(rsc.get("server:" + i + ":playerCount")); c += Integer.valueOf(rsc.get("server:" + i + ":playerCount"));
}
} finally {
pool.returnResource(rsc);
} }
} finally {
pool.returnResource(rsc);
} }
return c; return c;
} }
@ -249,31 +252,51 @@ public class RedisBungee extends Plugin implements Listener {
if (redisServer != null) { if (redisServer != null) {
if (!redisServer.equals("")) { if (!redisServer.equals("")) {
pool = new JedisPool(new JedisPoolConfig(), redisServer, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword); pool = new JedisPool(new JedisPoolConfig(), redisServer, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword);
// Test the connection
Jedis rsc = pool.getResource();
try {
rsc.exists(String.valueOf(System.currentTimeMillis()));
getLogger().log(Level.INFO, "Successfully connected to Redis.");
} catch (JedisConnectionException e) {
pool.returnBrokenResource(rsc);
getLogger().log(Level.WARNING, "Failed to connect to your Redis server! RedisBungee will still work, albeit with reduced functionality.", e);
pool.destroy();
pool = null;
rsc = null;
} finally {
if (rsc != null && pool != null) {
pool.returnResource(rsc);
}
}
} }
} }
} }
@EventHandler @EventHandler
public void onPreLogin(PreLoginEvent event) { public void onPreLogin(PreLoginEvent event) {
Jedis rsc = pool.getResource(); if (pool != null) {
try { Jedis rsc = pool.getResource();
if (rsc.hexists("player:" + event.getConnection().getName(), "server")) { try {
event.setCancelled(true); if (rsc.hexists("player:" + event.getConnection().getName(), "server")) {
event.setCancelReason("You are already logged on to this server."); event.setCancelled(true);
event.setCancelReason("You are already logged on to this server.");
}
} finally {
pool.returnResource(rsc);
} }
} finally {
pool.returnResource(rsc);
} }
} }
@EventHandler @EventHandler
public void onPlayerConnect(final PostLoginEvent event) { public void onPlayerConnect(final PostLoginEvent event) {
Jedis rsc = pool.getResource(); if (pool != null) {
try { Jedis rsc = pool.getResource();
rsc.sadd("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName()); try {
rsc.hset("player:" + event.getPlayer().getName(), "online", "0"); rsc.sadd("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName());
} finally { rsc.hset("player:" + event.getPlayer().getName(), "online", "0");
pool.returnResource(rsc); } finally {
pool.returnResource(rsc);
}
} }
// I used to have a task that eagerly waited for the user to be connected. // I used to have a task that eagerly waited for the user to be connected.
// Well, upon further inspection of BungeeCord's source code, this turned // Well, upon further inspection of BungeeCord's source code, this turned
@ -329,7 +352,6 @@ public class RedisBungee extends Plugin implements Listener {
} }
private class PubSubListener extends Thread { private class PubSubListener extends Thread {
private Jedis rsc; private Jedis rsc;
private JedisPubSubHandler jpsh; private JedisPubSubHandler jpsh;

View File

@ -15,7 +15,7 @@ import java.util.Collection;
/** /**
* This class is the CommandSender that RedisBungee uses to dispatch commands to BungeeCord. * This class is the CommandSender that RedisBungee uses to dispatch commands to BungeeCord.
* * <p/>
* It inherits all permissions of the console command sender. Sending messages and modifying permissions are no-ops. * It inherits all permissions of the console command sender. Sending messages and modifying permissions are no-ops.
* *
* @author tuxed * @author tuxed

View File

@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit;
/** /**
* This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen. * This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen.
* * <p/>
* All classes use the {@link RedisBungeeAPI}. * All classes use the {@link RedisBungeeAPI}.
* *
* @author tuxed * @author tuxed