Fix redis close (#3353)

This commit is contained in:
Luck 2022-03-27 16:50:31 +01:00
parent 5647b2e5c6
commit 2e0a4d71a5
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -48,8 +48,9 @@ public class RedisMessenger implements Messenger {
private final LuckPermsPlugin plugin; private final LuckPermsPlugin plugin;
private final IncomingMessageConsumer consumer; private final IncomingMessageConsumer consumer;
private JedisPool jedisPool; private /* final */ JedisPool jedisPool;
private Subscription sub; private /* final */ Subscription sub;
private boolean closing = false;
public RedisMessenger(LuckPermsPlugin plugin, IncomingMessageConsumer consumer) { public RedisMessenger(LuckPermsPlugin plugin, IncomingMessageConsumer consumer) {
this.plugin = plugin; this.plugin = plugin;
@ -82,6 +83,7 @@ public class RedisMessenger implements Messenger {
@Override @Override
public void close() { public void close() {
this.closing = true;
this.sub.unsubscribe(); this.sub.unsubscribe();
this.jedisPool.destroy(); this.jedisPool.destroy();
} }
@ -90,16 +92,21 @@ public class RedisMessenger implements Messenger {
@Override @Override
public void run() { public void run() {
boolean wasBroken = false; boolean first = true;
while (!Thread.interrupted() && !RedisMessenger.this.jedisPool.isClosed()) { while (!RedisMessenger.this.closing && !Thread.interrupted() && !RedisMessenger.this.jedisPool.isClosed()) {
try (Jedis jedis = RedisMessenger.this.jedisPool.getResource()) { try (Jedis jedis = RedisMessenger.this.jedisPool.getResource()) {
if (wasBroken) { if (first) {
first = false;
} else {
RedisMessenger.this.plugin.getLogger().info("Redis pubsub connection re-established"); RedisMessenger.this.plugin.getLogger().info("Redis pubsub connection re-established");
wasBroken = false;
} }
jedis.subscribe(this, CHANNEL);
jedis.subscribe(this, CHANNEL); // blocking call
} catch (Exception e) { } catch (Exception e) {
wasBroken = true; if (RedisMessenger.this.closing) {
return;
}
RedisMessenger.this.plugin.getLogger().warn("Redis pubsub connection dropped, trying to re-open the connection", e); RedisMessenger.this.plugin.getLogger().warn("Redis pubsub connection dropped, trying to re-open the connection", e);
try { try {
unsubscribe(); unsubscribe();