View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.recovery;
15  
16  public class RecoveryCoordinator {
17  
18      public final static long BACKOFF_COEFFICIENT_MIN = 20;
19      public final static long BACKOFF_MULTIPLIER = 4;
20      static long BACKOFF_COEFFICIENT_MAX = 327680; // BACKOFF_COEFFICIENT_MIN * 4^7
21  
22      private long backOffCoefficient = BACKOFF_COEFFICIENT_MIN;
23  
24      private static long UNSET = -1;
25      // tests can set the time directly independently of system clock
26      private long currentTime = UNSET;
27      private long next;
28  
29      public RecoveryCoordinator() {
30          next = getCurrentTime() + getBackoffCoefficient();
31      }
32  
33      public RecoveryCoordinator(long currentTime) {
34          this.currentTime = currentTime;
35          next = getCurrentTime() + getBackoffCoefficient();
36      }
37  
38      public boolean isTooSoon() {
39          long now = getCurrentTime();
40          if (now > next) {
41              next = now + getBackoffCoefficient();
42              return false;
43          } else {
44              return true;
45          }
46      }
47  
48      void setCurrentTime(long forcedTime) {
49          currentTime = forcedTime;
50      }
51  
52      private long getCurrentTime() {
53          if (currentTime != UNSET) {
54              return currentTime;
55          }
56          return System.currentTimeMillis();
57      }
58  
59      private long getBackoffCoefficient() {
60          long currentCoeff = backOffCoefficient;
61          if (backOffCoefficient < BACKOFF_COEFFICIENT_MAX) {
62              backOffCoefficient *= BACKOFF_MULTIPLIER;
63          }
64          return currentCoeff;
65      }
66  }