10
10
import org .jetbrains .annotations .NotNull ;
11
11
12
12
import java .util .Calendar ;
13
+ import java .util .Random ;
13
14
import java .util .Timer ;
14
15
import java .util .TimerTask ;
15
16
16
17
17
18
@ Slf4j
18
19
public class ReconnectionManager {
19
20
20
- private static final int LINEAR_INTERVAL = 3 ;
21
- private static final int MIN_EXPONENTIAL_BACKOFF = 1 ;
21
+ private static final int BASE_LINEAR_INTERVAL_IN_MILLISECONDS = 3000 ;
22
+ private static final int MIN_EXPONENTIAL_BACKOFF = 2 ;
22
23
private static final int MAX_EXPONENTIAL_BACKOFF = 32 ;
23
24
24
- private static final int MILLISECONDS = 1000 ;
25
+ private static final int BOUND = 1000 ;
26
+ private static final int MILLISECONDS = BOUND ;
27
+ private static final int MAXIMUM_RECONNECTION_RETRIES_DEFAULT = 10 ;
25
28
26
29
private ReconnectionCallback callback ;
27
30
private PubNub pubnub ;
@@ -31,6 +34,7 @@ public class ReconnectionManager {
31
34
32
35
private PNReconnectionPolicy pnReconnectionPolicy ;
33
36
private int maxConnectionRetries ;
37
+ private final Random random = new Random ();
34
38
35
39
/**
36
40
* Timer for heartbeat operations.
@@ -55,18 +59,18 @@ public void startPolling() {
55
59
exponentialMultiplier = 1 ;
56
60
failedCalls = 0 ;
57
61
58
- registerHeartbeatTimer ();
62
+ registerRetryTimer ();
59
63
}
60
64
61
- private void registerHeartbeatTimer () {
65
+ private void registerRetryTimer () {
62
66
// make sure only one timer is running at a time.
63
67
stopHeartbeatTimer ();
64
68
65
69
if (isReconnectionPolicyUndefined ()) {
66
70
return ;
67
71
}
68
72
69
- if (maxConnectionRetries != - 1 && failedCalls >= maxConnectionRetries ) { // _what's -1?
73
+ if (! maxConnectionIsSetToInfinite () && failedCalls >= maxConnectionRetries ) {
70
74
callback .onMaxReconnectionExhaustion ();
71
75
return ;
72
76
}
@@ -78,11 +82,15 @@ private void registerHeartbeatTimer() {
78
82
public void run () {
79
83
callTime ();
80
84
}
81
- }, getNextInterval () * MILLISECONDS );
85
+ }, getNextIntervalInMilliSeconds () );
82
86
}
83
87
84
- int getNextInterval () {
85
- int timerInterval = LINEAR_INTERVAL ;
88
+ private boolean maxConnectionIsSetToInfinite () {
89
+ return maxConnectionRetries == -1 ;
90
+ }
91
+
92
+ int getNextIntervalInMilliSeconds () {
93
+ int timerInterval = 0 ;
86
94
failedCalls ++;
87
95
88
96
if (pnReconnectionPolicy == PNReconnectionPolicy .EXPONENTIAL ) {
@@ -91,20 +99,24 @@ int getNextInterval() {
91
99
if (timerInterval > MAX_EXPONENTIAL_BACKOFF ) {
92
100
timerInterval = MIN_EXPONENTIAL_BACKOFF ;
93
101
exponentialMultiplier = 1 ;
94
- log .debug ("timerInterval > MAXEXPONENTIALBACKOFF at: " + Calendar .getInstance ().getTime (). toString () );
102
+ log .debug ("timerInterval > MAXEXPONENTIALBACKOFF at: " + Calendar .getInstance ().getTime ());
95
103
} else if (timerInterval < 1 ) {
96
104
timerInterval = MIN_EXPONENTIAL_BACKOFF ;
97
105
}
98
- log .debug ("timerInterval = " + timerInterval + " at: " + Calendar .getInstance ().getTime ().toString ());
106
+ timerInterval = (int ) ((timerInterval * MILLISECONDS ) + getRandomDelayInMilliSeconds ());
107
+ log .debug ("timerInterval = " + timerInterval + "ms at: " + Calendar .getInstance ().getTime ());
99
108
}
100
109
101
110
if (pnReconnectionPolicy == PNReconnectionPolicy .LINEAR ) {
102
- timerInterval = LINEAR_INTERVAL ;
111
+ timerInterval = ( int ) ( BASE_LINEAR_INTERVAL_IN_MILLISECONDS + getRandomDelayInMilliSeconds ()) ;
103
112
}
104
-
105
113
return timerInterval ;
106
114
}
107
115
116
+ private int getRandomDelayInMilliSeconds () {
117
+ return random .nextInt (BOUND );
118
+ }
119
+
108
120
private void stopHeartbeatTimer () {
109
121
if (timer != null ) {
110
122
timer .cancel ();
@@ -121,7 +133,7 @@ public void onResponse(PNTimeResult result, @NotNull PNStatus status) {
121
133
callback .onReconnection ();
122
134
} else {
123
135
log .debug ("callTime() at: " + Calendar .getInstance ().getTime ().toString ());
124
- registerHeartbeatTimer ();
136
+ registerRetryTimer ();
125
137
}
126
138
}
127
139
});
0 commit comments