You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by GitBox <gi...@apache.org> on 2019/07/09 16:00:24 UTC

[GitHub] [sling-org-apache-sling-distribution-journal] tmaret commented on a change in pull request #8: SLING-8531 - Exponential backoff for retries

tmaret commented on a change in pull request #8: SLING-8531 - Exponential backoff for retries
URL: https://github.com/apache/sling-org-apache-sling-distribution-journal/pull/8#discussion_r301663064
 
 

 ##########
 File path: src/main/java/org/apache/sling/distribution/journal/impl/shared/ExponentialBackOff.java
 ##########
 @@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.journal.impl.shared;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Random;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Retry with exponential backoff.
+ * 
+ * Calls the checkCallback until it does not throw an Exception
+ */
+public class ExponentialBackOff implements Closeable {
+    private final Logger log = LoggerFactory.getLogger(this.getClass());
+
+    private final ScheduledExecutorService executor;
+    private final Runnable checkCallback;
+    
+    private Random random;
+    private int maxDelay;
+
+    private int currentMaxDelay;
+    
+    public ExponentialBackOff(int maxDelay, Runnable checkCallback) {
+        this.currentMaxDelay = 128;
+        this.maxDelay = maxDelay;
+        this.checkCallback = checkCallback;
+        this.executor = Executors.newScheduledThreadPool(1);
+        this.random = new Random();
+        scheduleCheck();
+    }
+
+    @Override
+    public void close() throws IOException {
+        this.executor.shutdown();
+    }
+
+    private void scheduleCheck() {
 
 Review comment:
   This will keep the same schedule for every retry. The initial retry may be really low and thus the checks will issue a constant and heavy load on the checked system.
   
   I think we should support the standard exponential backoff strategy, where the retries are delayed by exponentially increasing delays (up to a limit).

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


With regards,
Apache Git Services