CURATOR-460 Abstraction of  getUseSessionTimeoutMs and readability improvements


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/b6af8fda
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/b6af8fda
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/b6af8fda

Branch: refs/heads/CURATOR-460
Commit: b6af8fdabbb620d6f01ce9453efba1fe1ef60205
Parents: 9123e04
Author: javando <antonio.rafael...@gmail.com>
Authored: Thu Apr 5 23:56:32 2018 -0300
Committer: javando <antonio.rafael...@gmail.com>
Committed: Thu Apr 5 23:56:32 2018 -0300

----------------------------------------------------------------------
 .../framework/state/ConnectionStateManager.java | 24 ++++++++++++--------
 1 file changed, 15 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/b6af8fda/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
----------------------------------------------------------------------
diff --git 
a/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
 
b/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
index 609bfa0..b66ca7e 100644
--- 
a/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
+++ 
b/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
@@ -252,14 +252,14 @@ public class ConnectionStateManager implements Closeable
             try
             {
 
-                long localStartOfSuspendedEpoch;
+                int useSessionTimeoutMs;
+                long elapsedMs;
+
                 synchronized (this) {
-                    localStartOfSuspendedEpoch = this.startOfSuspendedEpoch;
+                    useSessionTimeoutMs = getUseSessionTimeoutMs();
+                    elapsedMs = this.startOfSuspendedEpoch == 0 ? 
useSessionTimeoutMs / 2 : System.currentTimeMillis() - 
this.startOfSuspendedEpoch;
                 }
-                int lastNegotiatedSessionTimeoutMs = 
client.getZookeeperClient().getLastNegotiatedSessionTimeoutMs();
-                int useSessionTimeoutMs = (lastNegotiatedSessionTimeoutMs > 0) 
? lastNegotiatedSessionTimeoutMs : sessionTimeoutMs;
-                useSessionTimeoutMs = sessionExpirationPercent > 0 && 
localStartOfSuspendedEpoch != 0 ? (useSessionTimeoutMs * 
sessionExpirationPercent) / 100 : useSessionTimeoutMs;
-                long elapsedMs = localStartOfSuspendedEpoch == 0 ? 
useSessionTimeoutMs / 2 : System.currentTimeMillis() - 
localStartOfSuspendedEpoch;
+
                 long pollMaxMs = useSessionTimeoutMs - elapsedMs;
 
                 final ConnectionState newState = eventQueue.poll(pollMaxMs, 
TimeUnit.MILLISECONDS);
@@ -305,9 +305,7 @@ public class ConnectionStateManager implements Closeable
         if ( (currentConnectionState == ConnectionState.SUSPENDED) && 
(startOfSuspendedEpoch != 0) )
         {
             long elapsedMs = System.currentTimeMillis() - 
startOfSuspendedEpoch;
-            int lastNegotiatedSessionTimeoutMs = 
client.getZookeeperClient().getLastNegotiatedSessionTimeoutMs();
-            int useSessionTimeoutMs = (lastNegotiatedSessionTimeoutMs > 0) ? 
lastNegotiatedSessionTimeoutMs : sessionTimeoutMs;
-            useSessionTimeoutMs = (useSessionTimeoutMs * 
sessionExpirationPercent) / 100;
+            int useSessionTimeoutMs = getUseSessionTimeoutMs();
             if ( elapsedMs >= useSessionTimeoutMs )
             {
                 log.warn(String.format("Session timeout has elapsed while 
SUSPENDED. Injecting a session expiration. Elapsed ms: %d. Adjusted session 
timeout ms: %d", elapsedMs, useSessionTimeoutMs));
@@ -340,4 +338,12 @@ public class ConnectionStateManager implements Closeable
         currentConnectionState = newConnectionState;
         startOfSuspendedEpoch = (currentConnectionState == 
ConnectionState.SUSPENDED) ? System.currentTimeMillis() : 0;
     }
+
+    private synchronized int getUseSessionTimeoutMs() {
+        int lastNegotiatedSessionTimeoutMs = 
client.getZookeeperClient().getLastNegotiatedSessionTimeoutMs();
+        int useSessionTimeoutMs = (lastNegotiatedSessionTimeoutMs > 0) ? 
lastNegotiatedSessionTimeoutMs : sessionTimeoutMs;
+        useSessionTimeoutMs = sessionExpirationPercent > 0 && 
startOfSuspendedEpoch != 0 ? (useSessionTimeoutMs * sessionExpirationPercent) / 
100 : useSessionTimeoutMs;
+        return useSessionTimeoutMs;
+    }
+
 }

Reply via email to