1
2
3
4
5
6
7
8
9
10
11
12
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;
21
22 private long backOffCoefficient = BACKOFF_COEFFICIENT_MIN;
23
24 private static long UNSET = -1;
25
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 }