Randgalt commented on a change in pull request #304: [CURATOR-505] WIP - 
Circuit breaking connection state listener decorator
URL: https://github.com/apache/curator/pull/304#discussion_r256244084
 
 

 ##########
 File path: 
curator-framework/src/main/java/org/apache/curator/framework/state/CircuitBreaker.java
 ##########
 @@ -0,0 +1,79 @@
+package org.apache.curator.framework.state;
+
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.RetrySleeper;
+import java.util.Objects;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+// must be guarded by sync
+class CircuitBreaker
+{
+    private final RetryPolicy retryPolicy;
+    private final ScheduledExecutorService service;
+
+    private boolean isOpen = false;
+    private int retryCount = 0;
+    private long startNanos = 0;
+
+    CircuitBreaker(RetryPolicy retryPolicy, ScheduledExecutorService service)
+    {
+        this.retryPolicy = Objects.requireNonNull(retryPolicy, "retryPolicy 
cannot be null");
+        this.service = Objects.requireNonNull(service, "service cannot be 
null");
+    }
+
+    boolean isOpen()
+    {
+        return isOpen;
+    }
+
+    int getRetryCount()
+    {
+        return retryCount;
+    }
+
+    boolean tryToOpen(Runnable completion)
+    {
+        if ( isOpen )
+        {
+            return false;
+        }
+
+        isOpen = true;
+        retryCount = 0;
+        startNanos = System.nanoTime();
+        if ( tryToRetry(completion) )
+        {
+            return true;
+        }
+        close();
+        return false;
+    }
+
+    boolean tryToRetry(Runnable completion)
+    {
+        if ( !isOpen )
+        {
+            return false;
+        }
+
+        long[] sleepTimeNanos = new long[]{0L};
+        RetrySleeper retrySleeper = (time, unit) -> sleepTimeNanos[0] = 
unit.toNanos(time);
+        if ( retryPolicy.allowRetry(retryCount, System.nanoTime() - 
startNanos, retrySleeper) )
 
 Review comment:
   Doh - I misread your comment. I see the problem now and it's fixex.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to