Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear peers on disconnect #168

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 55 additions & 34 deletions src/classes/HiFiMixerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class HiFiMixerSession {
*
* Thus, the Library user should never have to care about the `_mixerPeerKeyToStateCacheDict`.
*/
private _mixerPeerKeyToStateCacheDict: any;
private _mixerPeerKeyToStateCacheDict: { [id: string] : ReceivedHiFiAudioAPIData };

/**
* We will track whether or not the input stream is stereo, so that
Expand Down Expand Up @@ -358,39 +358,6 @@ export class HiFiMixerSession {
let unGZippedData = pako.ungzip(data, { to: 'string' });
let jsonData = JSON.parse(unGZippedData);

if (jsonData.deleted_visit_ids) {
let allDeletedUserData: Array<ReceivedHiFiAudioAPIData> = [];

let deletedVisitIDs = jsonData.deleted_visit_ids;
for (const deletedVisitID of deletedVisitIDs) {
let hashedVisitID = deletedVisitID;

let deletedUserData = new ReceivedHiFiAudioAPIData({
hashedVisitID: hashedVisitID
});

let mixerPeerKeys = Object.keys(this._mixerPeerKeyToStateCacheDict);
for (const mixerPeerKey of mixerPeerKeys) {
if (this._mixerPeerKeyToStateCacheDict[mixerPeerKey].hashedVisitID === hashedVisitID) {
if (this._mixerPeerKeyToStateCacheDict[mixerPeerKey].providedUserID) {
deletedUserData.providedUserID = this._mixerPeerKeyToStateCacheDict[mixerPeerKey].providedUserID;
}
// TODO: remove the entry from the peer state cache -- is this OK?
//delete this._mixerPeerKeyToStateCacheDict[mixerPeerKey];
break;
}
}

allDeletedUserData.push(deletedUserData);
}

// TODO: remove the entry from the peer state cache
this.concurrency -= allDeletedUserData.length;
if (this.onUsersDisconnected && allDeletedUserData.length > 0) {
this.onUsersDisconnected(allDeletedUserData);
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the handling of deleted_visit_ids down below the handling of peers. It is theoretically possible for a change to exist in peers for someone who was also deleted in the same frame, and when a change is not found in the map the peer is added to it. By handling the deletions last we can avoid re-adding the changed user after deletion.

if (jsonData.peers) {
let allNewUserData: Array<ReceivedHiFiAudioAPIData> = [];

Expand Down Expand Up @@ -536,6 +503,38 @@ export class HiFiMixerSession {
this.onUserDataUpdated(allNewUserData);
}
}

if (jsonData.deleted_visit_ids) {
let allDeletedUserData: Array<ReceivedHiFiAudioAPIData> = [];

let deletedVisitIDs = jsonData.deleted_visit_ids;
for (const deletedVisitID of deletedVisitIDs) {
let hashedVisitID = deletedVisitID;

let deletedUserData = new ReceivedHiFiAudioAPIData({
hashedVisitID: hashedVisitID
});

let mixerPeerKeys = Object.keys(this._mixerPeerKeyToStateCacheDict);
for (const key of mixerPeerKeys) {
let peerData = this._mixerPeerKeyToStateCacheDict[key];
if (peerData.hashedVisitID === hashedVisitID) {
if (peerData.providedUserID) {
deletedUserData.providedUserID = peerData.providedUserID;
}
delete this._mixerPeerKeyToStateCacheDict[key];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I'm correctly deleting the peer from the map on delete. Previously this operation was commented out with a TODO comment about it (see the above removed block).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

break;
}
}

allDeletedUserData.push(deletedUserData);
}

this.concurrency -= allDeletedUserData.length;
if (this.onUsersDisconnected && allDeletedUserData.length > 0) {
this.onUsersDisconnected(allDeletedUserData);
}
}

if (jsonData.instructions) {
for (const instruction of jsonData.instructions) {
Expand Down Expand Up @@ -699,6 +698,7 @@ export class HiFiMixerSession {
await close(this._raviSignalingConnection, "Signaling Connection", RaviSignalingStates.CLOSED);
await close(this._raviSession, "Session", RaviSessionStates.CLOSED);

this._clearPeerData();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the real fix for HIFI-847: clear the peer cache on disconnect.

this._resetMixerInfo();

await this._setMutedByAdmin(false, MuteReason.INTERNAL);
Expand Down Expand Up @@ -1231,6 +1231,27 @@ export class HiFiMixerSession {
this.mixerInfo = {
"connected": false,
};
}

/**
* Clears all cached peer data and submits all hashedVisitIDs as "deleted"
*/
private _clearPeerData(): void {
if (this.onUsersDisconnected) {
let allDeletedUserData: Array<ReceivedHiFiAudioAPIData> = [];
for (let [key , value] of Object.entries(this._mixerPeerKeyToStateCacheDict)) {
let deletedUserData = new ReceivedHiFiAudioAPIData({
hashedVisitID: value.hashedVisitID
});
if (value.providedUserID) {
deletedUserData.providedUserID = value.providedUserID;
}
allDeletedUserData.push(deletedUserData);
}
if (allDeletedUserData.length > 0) {
this.onUsersDisconnected(allDeletedUserData);
}
}
this._mixerPeerKeyToStateCacheDict = {};
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we aren't just clearing the peer cache here. If onUserDisconnect callback exists we also assemble allDeletedUserData and feed it to onUserDisconnect. This tells the Application to remove any stale peer data it happens to be using.