2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-23 04:28:01 +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,10 +54,10 @@ public class RedisBungee extends Plugin implements Listener {
} }
public int getCount() { public int getCount() {
int c = plugin.getProxy().getOnlineCount();
if (pool != null) {
Jedis rsc = pool.getResource(); Jedis rsc = pool.getResource();
int c = 0;
try { try {
c = plugin.getProxy().getOnlineCount();
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"))
@ -64,6 +66,7 @@ public class RedisBungee extends Plugin implements Listener {
} finally { } finally {
pool.returnResource(rsc); pool.returnResource(rsc);
} }
}
return c; return c;
} }
@ -249,12 +252,29 @@ 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) {
if (pool != null) {
Jedis rsc = pool.getResource(); Jedis rsc = pool.getResource();
try { try {
if (rsc.hexists("player:" + event.getConnection().getName(), "server")) { if (rsc.hexists("player:" + event.getConnection().getName(), "server")) {
@ -265,9 +285,11 @@ public class RedisBungee extends Plugin implements Listener {
pool.returnResource(rsc); pool.returnResource(rsc);
} }
} }
}
@EventHandler @EventHandler
public void onPlayerConnect(final PostLoginEvent event) { public void onPlayerConnect(final PostLoginEvent event) {
if (pool != null) {
Jedis rsc = pool.getResource(); Jedis rsc = pool.getResource();
try { try {
rsc.sadd("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName()); rsc.sadd("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName());
@ -275,6 +297,7 @@ public class RedisBungee extends Plugin implements Listener {
} finally { } finally {
pool.returnResource(rsc); 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
// out to not be needed at all, since ServerConnectedEvent is called anyway. // out to not be needed at all, since ServerConnectedEvent is called anyway.
@ -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