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 2 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
32 changes: 26 additions & 6 deletions src/classes/HiFiMixerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ export class HiFiMixerSession {
});

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;
for (const key of mixerPeerKeys) {
let peerData = this._mixerPeerKeyToStateCacheDict[key];
if (peerData.hashedVisitID === hashedVisitID) {
if (peerData.providedUserID) {
deletedUserData.providedUserID = peerData.providedUserID;
}
// TODO: remove the entry from the peer state cache -- is this OK?
//delete this._mixerPeerKeyToStateCacheDict[mixerPeerKey];
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.

While looking over the code I noticed this for loop was performing double map lookups in _mixerPeerKeyToStateCacheDict in the common case and triple lookups in the uncommon. This offends my sensibilities for high performance code and so I was compelled to fix it.

Copy link
Author

Choose a reason for hiding this comment

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

Oh yeah, also notice that I did uncomment the code for the old TODO. Yes, it is a good idea to actually purge old peer data from the cache when it gets deleted.

break;
}
}
Expand Down Expand Up @@ -699,6 +699,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 +1232,25 @@ export class HiFiMixerSession {
this.mixerInfo = {
"connected": false,
};
}

/**
* Clears all cached peer data and submits all hashedVisitIDs as "deleted"
*/
private _clearPeerData(): void {
if (this.onUsersDisconnected && this._mixerPeerKeyToStateCacheDict.length > 0) {
let allDeletedUserData: Array<ReceivedHiFiAudioAPIData> = [];
for (const peerData of this._mixerPeerKeyToStateCacheDict) {
let deletedUserData = new ReceivedHiFiAudioAPIData({
hashedVisitID: peerData.hashedVisitID,
});
if (peerData.providedUserID) {
deletedUserData.providedUserID = peerData.providedUserID;
}
allDeletedUserData.push(deletedUserData);
}
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.