View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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 = 100;
19    static long BACKOFF_COEFFICIENT_MAX = 409600;  // BACKOFF_COEFFICIENT_MIN * 4^6
20    
21    private long backOffCoefficient = BACKOFF_COEFFICIENT_MIN;
22    
23    private static long UNSET = -1;
24    private long currentTime = UNSET;
25    long next = System.currentTimeMillis()+getBackoffCoefficient();
26    
27    public boolean isTooSoon() {
28      long now = getCurrentTime();
29      if(now > next) {
30        next = now + getBackoffCoefficient();
31        return false;
32      } else {
33        return true;
34      }
35    }
36    
37    void setCurrentTime(long forcedTime) {
38      currentTime = forcedTime;
39    }
40    
41    private long getCurrentTime() {
42      if(currentTime != UNSET) {
43        return currentTime;
44      }
45      return System.currentTimeMillis();
46    }
47    
48    private long getBackoffCoefficient() {
49      long currentCoeff = backOffCoefficient;
50      if(backOffCoefficient < BACKOFF_COEFFICIENT_MAX) {
51        backOffCoefficient*=4;
52      }
53      return currentCoeff;
54    }
55  }