Skip to content

Commit 047259e

Browse files
committed
refactor(shared-worker): re-adjust "backup" timer on early heartbeat
Re-adjust "backup" heartbeat timer when one of the clients request sent too early after previous request.
1 parent a4a139c commit 047259e

6 files changed

Lines changed: 16 additions & 15 deletions

File tree

dist/web/pubnub.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10800,7 +10800,6 @@
1080010800
return;
1080110801
if (this.parentSetsCount > 0) {
1080210802
// Creating from whole payload (not only for published messages).
10803-
console.dir(event.data);
1080410803
const fingerprint = messageFingerprint(event.data);
1080510804
if (this.handledUpdates.includes(fingerprint)) {
1080610805
this.state.client.logger.trace(this.subscriptionType, `Message (${fingerprint}) already handled by ${this.id}. Ignoring.`);

dist/web/pubnub.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web/pubnub.worker.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,13 +934,12 @@
934934
if (!outOfOrder && !failedPreviousRequest && currentTimestamp < expectedTimestamp) {
935935
// Check whether it is too soon to send request or not.
936936
const leeway = minimumHeartbeatInterval * 0.05 * 1000;
937-
if (minimumHeartbeatInterval - leeway <= 3) {
938-
// Leeway can't be applied if actual interval between heartbeat requests is smaller
939-
// than 3 seconds which derived from the server's threshold.
937+
// Leeway can't be applied if actual interval between heartbeat requests is smaller
938+
// than 3 seconds which derived from the server's threshold.
939+
if (minimumHeartbeatInterval - leeway <= 3 || expectedTimestamp - currentTimestamp > leeway) {
940+
startHeartbeatTimer(client, true);
940941
return undefined;
941942
}
942-
else if (expectedTimestamp - currentTimestamp > leeway)
943-
return undefined;
944943
}
945944
}
946945
delete hbRequestsBySubscriptionKey[heartbeatRequestKey].response;
@@ -1684,10 +1683,12 @@
16841683
if (adjust && !heartbeat.loop)
16851684
return;
16861685
let targetInterval = heartbeatInterval;
1687-
if (adjust && heartbeat.loop && targetInterval !== heartbeat.loop.heartbeatInterval) {
1686+
if (adjust && heartbeat.loop) {
16881687
const activeTime = (Date.now() - heartbeat.loop.startTimestamp) / 1000;
16891688
if (activeTime < targetInterval)
16901689
targetInterval -= activeTime;
1690+
if (targetInterval === heartbeat.loop.heartbeatInterval)
1691+
targetInterval += 0.05;
16911692
}
16921693
stopHeartbeatTimer(client);
16931694
if (targetInterval <= 0)

dist/web/pubnub.worker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/entities/subscription.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export class Subscription extends SubscriptionBase {
143143

144144
if (this.parentSetsCount > 0) {
145145
// Creating from whole payload (not only for published messages).
146-
console.dir(event.data);
147146
const fingerprint = messageFingerprint(event.data);
148147
if (this.handledUpdates.includes(fingerprint)) {
149148
this.state.client.logger.trace(

src/transport/subscription-worker/subscription-worker.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,11 +1548,12 @@ const heartbeatTransportRequestFromEvent = (
15481548
// Check whether it is too soon to send request or not.
15491549
const leeway = minimumHeartbeatInterval * 0.05 * 1000;
15501550

1551-
if (minimumHeartbeatInterval - leeway <= 3) {
1552-
// Leeway can't be applied if actual interval between heartbeat requests is smaller
1553-
// than 3 seconds which derived from the server's threshold.
1551+
// Leeway can't be applied if actual interval between heartbeat requests is smaller
1552+
// than 3 seconds which derived from the server's threshold.
1553+
if (minimumHeartbeatInterval - leeway <= 3 || expectedTimestamp - currentTimestamp > leeway) {
1554+
startHeartbeatTimer(client, true);
15541555
return undefined;
1555-
} else if (expectedTimestamp - currentTimestamp > leeway) return undefined;
1556+
}
15561557
}
15571558
}
15581559

@@ -2394,9 +2395,10 @@ const startHeartbeatTimer = (client: PubNubClientState, adjust: boolean = false)
23942395
if (adjust && !heartbeat.loop) return;
23952396

23962397
let targetInterval = heartbeatInterval;
2397-
if (adjust && heartbeat.loop && targetInterval !== heartbeat.loop.heartbeatInterval) {
2398+
if (adjust && heartbeat.loop) {
23982399
const activeTime = (Date.now() - heartbeat.loop.startTimestamp) / 1000;
23992400
if (activeTime < targetInterval) targetInterval -= activeTime;
2401+
if (targetInterval === heartbeat.loop.heartbeatInterval) targetInterval += 0.05;
24002402
}
24012403

24022404
stopHeartbeatTimer(client);

0 commit comments

Comments
 (0)