From 280e90e2972bd208e4d56b105befd0936942f5b6 Mon Sep 17 00:00:00 2001 From: William278 Date: Wed, 24 Jan 2024 23:00:41 +0000 Subject: [PATCH] refactor: use guard clause in thread unlock logic --- .../husksync/redis/RedisManager.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/net/william278/husksync/redis/RedisManager.java b/common/src/main/java/net/william278/husksync/redis/RedisManager.java index 577045e7..72b6f50a 100644 --- a/common/src/main/java/net/william278/husksync/redis/RedisManager.java +++ b/common/src/main/java/net/william278/husksync/redis/RedisManager.java @@ -117,27 +117,29 @@ public class RedisManager extends JedisPubSub { } } - private void onThreadUnlock(Throwable t) { - if (enabled) { - if (reconnected) { - plugin.log(Level.WARNING, - "Connection to the Redis server was lost. Attempting reconnection in 8 seconds...", t); - } - try { - this.unsubscribe(); - } catch (Throwable ignored) { - // empty catch - } + private void onThreadUnlock(@NotNull Throwable t) { + if (!enabled) { + return; + } - // Make an instant subscribe if occurs any error on initialization - if (!reconnected) { - reconnected = true; - } else { - try { - Thread.sleep(RECONNECTION_TIME); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } + if (reconnected) { + plugin.log(Level.WARNING, "Redis Server connection lost. Attempting reconnect in %ss..." + .formatted(RECONNECTION_TIME / 1000), t); + } + try { + this.unsubscribe(); + } catch (Throwable ignored) { + // empty catch + } + + // Make an instant subscribe if occurs any error on initialization + if (!reconnected) { + reconnected = true; + } else { + try { + Thread.sleep(RECONNECTION_TIME); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } } }