Skip to content

Commit

Permalink
And onPeerClose event to Peer class.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Oct 8, 2023
1 parent 52d88d0 commit 220b440
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class HybridLoader {
private readonly playback: Playback;
private lastQueueProcessingTimeStamp?: number;
private readonly segmentAvgDuration: number;
private readonly randomHttpDownloadInterval: number;
private randomHttpDownloadInterval!: number;
private readonly logger: { engine: debug.Debugger; loader: debug.Debugger };

constructor(
Expand Down Expand Up @@ -54,15 +54,25 @@ export class HybridLoader {
this.settings
);

this.logger = {
loader: debug(`core:hybrid-loader-${activeStream.type}`),
engine: debug(`core:hybrid-loader-${activeStream.type}-engine`),
};
const loader = debug(`core:hybrid-loader-${activeStream.type}`);
const engine = debug(`core:hybrid-loader-${activeStream.type}-engine`);
loader.color = "coral";
engine.color = "orange";
this.logger = { loader, engine };

this.randomHttpDownloadInterval = window.setInterval(
this.loadRandomThroughHttp.bind(this),
1500
);
// this.randomHttpDownloadInterval = window.setInterval(
// this.loadRandomThroughHttp.bind(this),
// 1500
// );
this.setIntervalLoading();
}

private setIntervalLoading() {
const randomTimeout = (Math.random() * 2 + 1) * 1000;
this.randomHttpDownloadInterval = window.setTimeout(() => {
this.loadRandomThroughHttp();
this.setIntervalLoading();
}, randomTimeout);
}

// api method for engines
Expand Down Expand Up @@ -254,7 +264,7 @@ export class HybridLoader {
});
if (!queue.length) return;
const peersAmount = connectedPeersAmount + 1;
const probability = Math.min(queue.length / peersAmount, 1) / 2;
const probability = Math.min(queue.length / peersAmount, 1);
const shouldLoad = Math.random() < probability;

if (!shouldLoad) return;
Expand Down
6 changes: 6 additions & 0 deletions packages/p2p-media-loader-core/src/p2p-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class P2PLoader {
candidate,
{
onPeerConnected: this.onPeerConnected.bind(this),
onPeerClosed: this.onPeerClosed.bind(this),
onSegmentRequested: this.onSegmentRequested.bind(this),
},
this.settings
Expand Down Expand Up @@ -156,6 +157,11 @@ export class P2PLoader {
peer.sendSegmentsAnnouncement(this.announcement);
}

private onPeerClosed(peer: Peer) {
this.logger(`peer closed: ${peer.localId}`);
this.peers.delete(peer.id);
}

private updateAndBroadcastAnnouncement = () => {
this.updateSegmentAnnouncement();
this.broadcastSegmentAnnouncement();
Expand Down
2 changes: 2 additions & 0 deletions packages/p2p-media-loader-core/src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PeerRequestError } from "./errors";

type PeerEventHandlers = {
onPeerConnected: (peer: Peer) => void;
onPeerClosed: (peer: Peer) => void;
onSegmentRequested: (peer: Peer, segmentId: string) => void;
};

Expand Down Expand Up @@ -63,6 +64,7 @@ export class Peer {
if (this.connection === candidate) {
this.connection = undefined;
this.cancelSegmentRequest("peer-closed");
this.eventHandlers.onPeerClosed(this);
}
});
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down

0 comments on commit 220b440

Please sign in to comment.