Skip to content

Commit e4ec49a

Browse files
Modified PNReconnectionPolicy (#297)
* Modified PNReconnectionPolicy Added random value 0,001-0,999s to delay between retry both for Linear and Exponential reconnection policies. * PubNub SDK v6.4.5 release. --------- Co-authored-by: PubNub Release Bot <[email protected]>
1 parent 94ae18e commit e4ec49a

File tree

11 files changed

+71
-36
lines changed

11 files changed

+71
-36
lines changed

.pubnub.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: java
2-
version: 6.4.4
2+
version: 6.4.5
33
schema: 1
44
scm: github.com/pubnub/java
55
files:
6-
- build/libs/pubnub-gson-6.4.4-all.jar
6+
- build/libs/pubnub-gson-6.4.5-all.jar
77
sdks:
88
-
99
type: library
@@ -23,8 +23,8 @@ sdks:
2323
-
2424
distribution-type: library
2525
distribution-repository: maven
26-
package-name: pubnub-gson-6.4.4
27-
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/6.4.4/pubnub-gson-6.4.4.jar
26+
package-name: pubnub-gson-6.4.5
27+
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/6.4.5/pubnub-gson-6.4.5.jar
2828
supported-platforms:
2929
supported-operating-systems:
3030
Android:
@@ -115,6 +115,13 @@ sdks:
115115
is-required: Required
116116

117117
changelog:
118+
- date: 2023-12-18
119+
version: v6.4.5
120+
changes:
121+
- type: bug
122+
text: "Added reading message type from fetch messages response."
123+
- type: bug
124+
text: "Added random value 0.001-0.999s to delay between retries both for Linear and Exponential reconnection policies."
118125
- date: 2023-11-30
119126
version: v6.4.4
120127
changes:

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v6.4.5
2+
December 18 2023
3+
4+
#### Fixed
5+
- Added reading message type from fetch messages response.
6+
- Added random value 0.001-0.999s to delay between retries both for Linear and Exponential reconnection policies.
7+
18
## v6.4.4
29
November 30 2023
310

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ You will need the publish and subscribe keys to authenticate your app. Get your
2222
<dependency>
2323
<groupId>com.pubnub</groupId>
2424
<artifactId>pubnub-gson</artifactId>
25-
<version>6.4.4</version>
25+
<version>6.4.5</version>
2626
</dependency>
2727
```
2828

2929
* for Gradle, add the following dependency in your `gradle.build`:
3030
```groovy
31-
implementation 'com.pubnub:pubnub-gson:6.4.4'
31+
implementation 'com.pubnub:pubnub-gson:6.4.5'
3232
```
3333

3434
2. Configure your keys:

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111
group = 'com.pubnub'
1212

13-
version = '6.4.4'
13+
version = '6.4.5'
1414

1515
description = """"""
1616

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SONATYPE_HOST=DEFAULT
33
SONATYPE_AUTOMATIC_RELEASE=true
44
GROUP=com.pubnub
55
POM_ARTIFACT_ID=pubnub-gson
6-
VERSION_NAME=6.4.4
6+
VERSION_NAME=6.4.5
77
POM_PACKAGING=jar
88

99
POM_NAME=PubNub Java SDK

src/main/java/com/pubnub/api/PNConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class PNConfiguration {
3838
private static final int SUBSCRIBE_TIMEOUT = 310;
3939
private static final int CONNECT_TIMEOUT = 5;
4040
private static final int FILE_MESSAGE_PUBLISH_RETRY_LIMIT = 5;
41+
private static final int MAXIMUM_RECONNECTION_RETRIES_DEFAULT = -1; // infinite
4142

4243
@Getter
4344
private SSLSocketFactory sslSocketFactory;
@@ -201,7 +202,8 @@ public void setUserId(@NotNull UserId userId) {
201202
private PNReconnectionPolicy reconnectionPolicy;
202203

203204
/**
204-
* Set how many times the reconneciton manager will try to connect before giving app
205+
* Set how many times the reconnection manager will try to connect before giving up.
206+
* Default is -1 which means to retry infinitely.
205207
*/
206208
@Setter
207209
private int maximumReconnectionRetries;
@@ -284,7 +286,7 @@ public PNConfiguration(@NotNull UserId userId) throws PubNubException {
284286

285287
startSubscriberThread = true;
286288

287-
maximumReconnectionRetries = -1;
289+
maximumReconnectionRetries = MAXIMUM_RECONNECTION_RETRIES_DEFAULT;
288290

289291
dedupOnSubscribe = false;
290292
suppressLeaveEvents = false;

src/main/java/com/pubnub/api/PubNub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class PubNub {
105105
private static final int TIMESTAMP_DIVIDER = 1000;
106106
private static final int MAX_SEQUENCE = 65535;
107107

108-
private static final String SDK_VERSION = "6.4.4";
108+
private static final String SDK_VERSION = "6.4.5";
109109
private final ListenerManager listenerManager;
110110
private final StateManager stateManager;
111111

src/main/java/com/pubnub/api/managers/DelayedReconnectionManager.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
import com.pubnub.api.enums.PNReconnectionPolicy;
66
import lombok.extern.slf4j.Slf4j;
77

8+
import java.util.Random;
89
import java.util.Timer;
910
import java.util.TimerTask;
1011

1112
@Slf4j
1213
public class DelayedReconnectionManager {
13-
private static final int DELAY_SECONDS = 3;
14-
private static final int MILLISECONDS = 1000;
14+
private static final int BASE_DELAY_MILLISECONDS = 2000;
15+
private static final int BOUND = 1000;
1516

1617
private final PNReconnectionPolicy pnReconnectionPolicy;
1718
private ReconnectionCallback callback;
1819
private PubNub pubnub;
20+
private final Random random = new Random();
1921

2022
/**
2123
* Timer for heartbeat operations.
@@ -34,12 +36,13 @@ public void scheduleDelayedReconnection() {
3436
}
3537

3638
timer = new Timer("Delayed Reconnection Manager timer", true);
39+
int effectiveDelayInMilliSeconds = (int) (BASE_DELAY_MILLISECONDS + getRandomDelayInMilliSeconds());
3740
timer.schedule(new TimerTask() {
3841
@Override
3942
public void run() {
4043
callTime();
4144
}
42-
}, DELAY_SECONDS * MILLISECONDS);
45+
}, effectiveDelayInMilliSeconds);
4346
}
4447

4548
public void setReconnectionListener(ReconnectionCallback reconnectionCallback) {
@@ -61,6 +64,10 @@ private boolean isReconnectionPolicyUndefined() {
6164
return false;
6265
}
6366

67+
private int getRandomDelayInMilliSeconds() {
68+
return random.nextInt(BOUND);
69+
}
70+
6471
private void callTime() {
6572
stop();
6673
callback.onReconnection();

src/main/java/com/pubnub/api/managers/ReconnectionManager.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
import org.jetbrains.annotations.NotNull;
1111

1212
import java.util.Calendar;
13+
import java.util.Random;
1314
import java.util.Timer;
1415
import java.util.TimerTask;
1516

1617

1718
@Slf4j
1819
public class ReconnectionManager {
1920

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;
2223
private static final int MAX_EXPONENTIAL_BACKOFF = 32;
2324

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;
2528

2629
private ReconnectionCallback callback;
2730
private PubNub pubnub;
@@ -31,6 +34,7 @@ public class ReconnectionManager {
3134

3235
private PNReconnectionPolicy pnReconnectionPolicy;
3336
private int maxConnectionRetries;
37+
private final Random random = new Random();
3438

3539
/**
3640
* Timer for heartbeat operations.
@@ -55,18 +59,18 @@ public void startPolling() {
5559
exponentialMultiplier = 1;
5660
failedCalls = 0;
5761

58-
registerHeartbeatTimer();
62+
registerRetryTimer();
5963
}
6064

61-
private void registerHeartbeatTimer() {
65+
private void registerRetryTimer() {
6266
// make sure only one timer is running at a time.
6367
stopHeartbeatTimer();
6468

6569
if (isReconnectionPolicyUndefined()) {
6670
return;
6771
}
6872

69-
if (maxConnectionRetries != -1 && failedCalls >= maxConnectionRetries) { // _what's -1?
73+
if (!maxConnectionIsSetToInfinite() && failedCalls >= maxConnectionRetries) {
7074
callback.onMaxReconnectionExhaustion();
7175
return;
7276
}
@@ -78,11 +82,15 @@ private void registerHeartbeatTimer() {
7882
public void run() {
7983
callTime();
8084
}
81-
}, getNextInterval() * MILLISECONDS);
85+
}, getNextIntervalInMilliSeconds());
8286
}
8387

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;
8694
failedCalls++;
8795

8896
if (pnReconnectionPolicy == PNReconnectionPolicy.EXPONENTIAL) {
@@ -91,20 +99,24 @@ int getNextInterval() {
9199
if (timerInterval > MAX_EXPONENTIAL_BACKOFF) {
92100
timerInterval = MIN_EXPONENTIAL_BACKOFF;
93101
exponentialMultiplier = 1;
94-
log.debug("timerInterval > MAXEXPONENTIALBACKOFF at: " + Calendar.getInstance().getTime().toString());
102+
log.debug("timerInterval > MAXEXPONENTIALBACKOFF at: " + Calendar.getInstance().getTime());
95103
} else if (timerInterval < 1) {
96104
timerInterval = MIN_EXPONENTIAL_BACKOFF;
97105
}
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());
99108
}
100109

101110
if (pnReconnectionPolicy == PNReconnectionPolicy.LINEAR) {
102-
timerInterval = LINEAR_INTERVAL;
111+
timerInterval = (int) (BASE_LINEAR_INTERVAL_IN_MILLISECONDS + getRandomDelayInMilliSeconds());
103112
}
104-
105113
return timerInterval;
106114
}
107115

116+
private int getRandomDelayInMilliSeconds() {
117+
return random.nextInt(BOUND);
118+
}
119+
108120
private void stopHeartbeatTimer() {
109121
if (timer != null) {
110122
timer.cancel();
@@ -121,7 +133,7 @@ public void onResponse(PNTimeResult result, @NotNull PNStatus status) {
121133
callback.onReconnection();
122134
} else {
123135
log.debug("callTime() at: " + Calendar.getInstance().getTime().toString());
124-
registerHeartbeatTimer();
136+
registerRetryTimer();
125137
}
126138
}
127139
});

src/test/java/com/pubnub/api/PubNubTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void getVersionAndTimeStamp() {
100100
pubnub = new PubNub(pnConfiguration);
101101
String version = pubnub.getVersion();
102102
int timeStamp = pubnub.getTimestamp();
103-
Assert.assertEquals("6.4.4", version);
103+
Assert.assertEquals("6.4.5", version);
104104
Assert.assertTrue(timeStamp > 0);
105105
}
106106

0 commit comments

Comments
 (0)