Skip to content

Commit bfcae43

Browse files
authored
Do heartbeat and leave calls in sequence (#237)
In some circumstances we do two contradicting calls in almost the same time. One of them is a call to heartbeat endpoint and the other is to leave endpoint. The reason why those call could happen in almost the same time is because heartbeat calls are done periodically on separate thread and leave calls are happening due to user actions. This means that sooner or later this situation will happen and pubnub presence system will generate join event and leave event (usually in this order), but then another timeout event when presence timeout expires. To prevent it from happening I've decided to limit the number of concurrent calls to only those two endpoints to just one. This is done on the okhttp client level. Because those two calls can be ordered only two ways we can provide an analysis of why this change might work. Let's first concentrate on situation when heartbeat call is already happening and unsubscribe method is being called: * leave call will go to the internal queue of calls of okhttp client due to limit of concurrent calls * unsubscribe method internally will try to cancel the heartbeat call and schedule timer with another heartbeat call (but with different set of channels) * heartbeat finishes (either normally, or by being cancelled), this usually do not generate any events, but it might generate join event * http leave call can finally proceed, which generates leave event * because the calls have been done sequentially there's no timeout event In case when leave call is already ongoing and the schedule heartbeat timer is trying to make heartbeat request: * heartbeat request goes to client's internal queue to wait on leave to be finished * in the meantime usubscribe method internally cancels the timer and cancels the heartbeat request before it has time to start * leave request ends, and leave event is being generated * there's no timeout event, and the presence didn't have chance to be extended
1 parent 36dbfc5 commit bfcae43

File tree

15 files changed

+105
-66
lines changed

15 files changed

+105
-66
lines changed

.pubnub.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: java
2-
version: 6.0.0
2+
version: 6.0.1
33
schema: 1
44
scm: github.com/pubnub/java
55
files:
6-
- build/libs/pubnub-gson-6.0.0-all.jar
6+
- build/libs/pubnub-gson-6.0.1-all.jar
77
sdks:
88
-
99
type: library
@@ -23,8 +23,8 @@ sdks:
2323
-
2424
distribution-type: library
2525
distribution-repository: GitHub
26-
package-name: pubnub-gson-6.0.0
27-
location: https://github.com/pubnub/java/releases/download/v6.0.0/pubnub-gson-6.0.0-all.jar
26+
package-name: pubnub-gson-6.0.1
27+
location: https://github.com/pubnub/java/releases/download/v6.0.1/pubnub-gson-6.0.1-all.jar
2828
supported-platforms:
2929
supported-operating-systems:
3030
Android:
@@ -135,8 +135,8 @@ sdks:
135135
-
136136
distribution-type: library
137137
distribution-repository: maven
138-
package-name: pubnub-gson-6.0.0
139-
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/6.0.0/pubnub-gson-6.0.0.jar
138+
package-name: pubnub-gson-6.0.1
139+
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/6.0.1/pubnub-gson-6.0.1.jar
140140
supported-platforms:
141141
supported-operating-systems:
142142
Android:
@@ -234,6 +234,11 @@ sdks:
234234
is-required: Required
235235

236236
changelog:
237+
- date: 2022-05-09
238+
version: v6.0.1
239+
changes:
240+
- type: bug
241+
text: "Do heartbeat and leave calls sequentially to prevent race condition between these two calls and 'phantom' join after leave."
237242
- date: 2022-01-12
238243
version: v6.0.0
239244
changes:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v6.0.1
2+
May 09 2022
3+
4+
#### Fixed
5+
- Do heartbeat and leave calls sequentially to prevent race condition between these two calls and 'phantom' join after leave.
6+
17
## v6.0.0
28
January 12 2022
39

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.0.0</version>
25+
<version>6.0.1</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.0.0'
31+
implementation 'com.pubnub:pubnub-gson:6.0.1'
3232
```
3333

3434
2. Configure your keys:

build.gradle

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

15-
version = '6.0.0'
15+
version = '6.0.1'
1616

1717
description = """"""
1818

config/findbugs/excludeFilter.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@
6565
<Field name="updated"/>
6666
<Bug pattern="UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD"/>
6767
</Match>
68+
<Match>
69+
<Class name="com.pubnub.api.PubNubException"/>
70+
<Field name="affectedCall"/>
71+
<Bug pattern="URF_UNREAD_FIELD"/>
72+
</Match>
6873
</FindBugsFilter>

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

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

107-
private static final String SDK_VERSION = "6.0.0";
107+
private static final String SDK_VERSION = "6.0.1";
108108
private final ListenerManager listenerManager;
109109
private final StateManager stateManager;
110110

src/main/java/com/pubnub/api/endpoints/presence/GetState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected Call<Envelope<JsonElement>> doWork(Map<String, String> params) {
6969

7070
String selectedUUID = uuid != null ? uuid : this.getPubnub().getConfiguration().getUuid();
7171

72-
return this.getRetrofit().getPresenceService().getState(
72+
return this.getRetrofit().getExtendedPresenceService().getState(
7373
this.getPubnub().getConfiguration().getSubscribeKey(), channelCSV, selectedUUID, params);
7474
}
7575

src/main/java/com/pubnub/api/endpoints/presence/HereNow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ protected Call<Envelope<JsonElement>> doWork(Map<String, String> params) {
9191
}
9292

9393
if (channels.size() > 0 || channelGroups.size() > 0) {
94-
return this.getRetrofit().getPresenceService().hereNow(this.getPubnub().getConfiguration().getSubscribeKey(), channelCSV, params);
94+
return this.getRetrofit().getExtendedPresenceService().hereNow(this.getPubnub().getConfiguration().getSubscribeKey(), channelCSV, params);
9595
} else {
96-
return this.getRetrofit().getPresenceService().globalHereNow(this.getPubnub().getConfiguration().getSubscribeKey(), params);
96+
return this.getRetrofit().getExtendedPresenceService().globalHereNow(this.getPubnub().getConfiguration().getSubscribeKey(), params);
9797
}
9898
}
9999

src/main/java/com/pubnub/api/endpoints/presence/SetState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected Call<Envelope<JsonElement>> doWork(Map<String, String> params) throws
104104

105105
String channelCSV = channels.size() > 0 ? PubNubUtil.joinString(channels, ",") : ",";
106106

107-
return this.getRetrofit().getPresenceService().setState(
107+
return this.getRetrofit().getExtendedPresenceService().setState(
108108
this.getPubnub().getConfiguration().getSubscribeKey(), channelCSV, selectedUUID, params);
109109
}
110110

src/main/java/com/pubnub/api/endpoints/presence/WhereNow.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected void validateParams() throws PubNubException {
5151

5252
@Override
5353
protected Call<Envelope<WhereNowPayload>> doWork(Map<String, String> params) {
54-
return this.getRetrofit().getPresenceService().whereNow(this.getPubnub().getConfiguration().getSubscribeKey(),
54+
return this.getRetrofit().getExtendedPresenceService().whereNow(this.getPubnub().getConfiguration().getSubscribeKey(),
5555
this.uuid != null ? this.uuid : this.getPubnub().getConfiguration().getUuid(), params);
5656
}
5757

@@ -77,5 +77,4 @@ protected PNOperationType getOperationType() {
7777
protected boolean isAuthRequired() {
7878
return true;
7979
}
80-
8180
}

0 commit comments

Comments
 (0)