Skip to content

Commit d948dc7

Browse files
committed
Refactored getting stats from tracker
1 parent b3c2452 commit d948dc7

4 files changed

Lines changed: 29 additions & 46 deletions

File tree

lib/fast-tracker.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const debug = DebugModule("wt-tracker:fast-tracker");
2323
const ldebug = ldebugConstructor(debug);
2424

2525
export class FastTracker implements Tracker {
26-
private swarms = new Map<string, Swarm>();
26+
private _swarms = new Map<string, Swarm>();
2727

2828
constructor(readonly settings: any = {}) {
2929
this.settings = {
@@ -33,6 +33,10 @@ export class FastTracker implements Tracker {
3333
};
3434
}
3535

36+
public get swarms(): ReadonlyMap<string, { peers: ReadonlyMap<string, PeerContext> }> {
37+
return this._swarms;
38+
}
39+
3640
public processMessage(json: any, peer: PeerContext) {
3741
const action: any = json.action;
3842

@@ -74,7 +78,7 @@ export class FastTracker implements Tracker {
7478
swarm.removePeer(peer);
7579
if (swarm.peers.size === 0) {
7680
ldebug(() => ["swarm removed (empty)", Buffer.from(swarm.infoHash).toString("hex")]);
77-
this.swarms.delete(swarm.infoHash);
81+
this._swarms.delete(swarm.infoHash);
7882
} else if (swarmContext.completed) {
7983
swarm.completedCount--;
8084
}
@@ -128,7 +132,7 @@ export class FastTracker implements Tracker {
128132
}
129133

130134
private addPeerToSwarm(peer: InternalPeerContext, infoHash: any) {
131-
let swarm = this.swarms.get(infoHash);
135+
let swarm = this._swarms.get(infoHash);
132136

133137
if (swarm === undefined) {
134138
if (typeof infoHash !== "string") {
@@ -137,7 +141,7 @@ export class FastTracker implements Tracker {
137141

138142
ldebug(() => ["announce: swarm created:", Buffer.from(infoHash).toString("hex")]);
139143
swarm = new Swarm(infoHash);
140-
this.swarms.set(infoHash, swarm);
144+
this._swarms.set(infoHash, swarm);
141145
}
142146

143147
ldebug(() => ["announce: peer", Buffer.from(peer.id!).toString("hex"), "added to swarm", Buffer.from(infoHash).toString("hex")]);
@@ -243,7 +247,7 @@ export class FastTracker implements Tracker {
243247

244248
if (swarm.peers.size === 0) {
245249
ldebug(() => ["stop event: swarm removed (empty)", Buffer.from(infoHash).toString("hex")]);
246-
this.swarms.delete(infoHash);
250+
this._swarms.delete(infoHash);
247251
}
248252
}
249253

@@ -252,7 +256,7 @@ export class FastTracker implements Tracker {
252256
const files: {[key: string]: { complete: number, incomplete: number, downloaded: number}} = {};
253257

254258
if (infoHash === undefined) {
255-
for (const swarm of this.swarms.values()) {
259+
for (const swarm of this._swarms.values()) {
256260
files[swarm.infoHash] = {
257261
complete: swarm.completedCount,
258262
incomplete: swarm.peersOrdered.length - swarm.completedCount,
@@ -261,7 +265,7 @@ export class FastTracker implements Tracker {
261265
}
262266
} else if (infoHash instanceof Array) {
263267
for (const singleInfoHash of infoHash) {
264-
const swarm = this.swarms.get(singleInfoHash);
268+
const swarm = this._swarms.get(singleInfoHash);
265269
if (swarm !== undefined) {
266270
files[singleInfoHash] = {
267271
complete: swarm.completedCount,
@@ -277,7 +281,7 @@ export class FastTracker implements Tracker {
277281
}
278282
}
279283
} else {
280-
const swarm = this.swarms.get(infoHash);
284+
const swarm = this._swarms.get(infoHash);
281285
if (swarm !== undefined) {
282286
files[infoHash] = {
283287
complete: swarm.completedCount,
@@ -298,17 +302,6 @@ export class FastTracker implements Tracker {
298302
files: files,
299303
});
300304
}
301-
302-
public get stats() {
303-
let peersCount = 0;
304-
for (const swarm of this.swarms.values()) {
305-
peersCount += swarm.peers.size;
306-
}
307-
return {
308-
torrentsCount: this.swarms.size,
309-
peersCount: peersCount,
310-
};
311-
}
312305
}
313306

314307
interface SwarmContext {

lib/run-uws-tracker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,16 @@ async function main() {
5757

5858
server.app
5959
.get("/stats.json", (response: HttpResponse, request: HttpRequest) => {
60+
const swarms = tracker.swarms;
61+
let peersCount = 0;
62+
for (const swarm of swarms.values()) {
63+
peersCount += swarm.peers.size;
64+
}
65+
6066
response.writeHeader("Content-Type", "application/json")
6167
.end(JSON.stringify({
62-
...tracker.stats,
68+
torrentsCount: swarms.size,
69+
peersCount: peersCount,
6370
...server.stats,
6471
}));
6572
})

lib/tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface PeerContext {
2020
}
2121

2222
export interface Tracker {
23-
readonly stats: { [x: string]: any; };
23+
readonly swarms: ReadonlyMap<string, { peers: ReadonlyMap<string, PeerContext> }>;
2424
readonly settings: any;
2525
processMessage(json: any, peer: PeerContext): void;
2626
disconnectPeer(peer: PeerContext): void;

test/simulation.test.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import { expect } from "chai";
2121
describe("simulation", () => {
2222
it("should pass random simulations", () => {
2323
const simulationsCount = 1000;
24-
const torrentsCount = 3;
25-
const peersCount = 300;
24+
const torrentsCount = 2;
25+
const peersCount = 200;
2626
const offersCount = 10;
2727
const sameIdPeersRatio = 0.1;
2828

@@ -93,30 +93,13 @@ describe("simulation", () => {
9393
doIteration();
9494
}
9595

96-
let activePeersCount = 0;
97-
const torrents = new Set<string>();
98-
99-
for (let i = 0; i < peers.length; i++) {
100-
const peer = peers[i];
101-
const peerData = peersData[i];
102-
103-
if (peerData.infoHash) {
104-
if ((peer as any).swarms.length !== 1) {
105-
continue;
106-
}
107-
108-
expect((peer as any).swarms[0].swarm.infoHash === peerData.infoHash, "the peer is in a wrong swarm");
109-
110-
if ((peer as any).swarms[0].swarm.peers.get(peerData.peerId) === peer) {
111-
activePeersCount++;
112-
torrents.add(peerData.infoHash);
113-
}
114-
} else {
115-
expect(((peer as any).swarms === undefined) || ((peer as any).swarms.length === 0), "the peer should not be in a swarm");
96+
for (const [swarmId, swarm] of tracker.swarms) {
97+
expect(swarm.peers.size > 0, "the swarm can't be empty");
98+
for (const peer of swarm.peers.values()) {
99+
const peerData = peersData.find(p => p.peerId === peer.id);
100+
expect(peerData === undefined, "the peer is missing");
101+
expect(peerData!.infoHash === swarmId, "the peer is in a wrong swarm");
116102
}
117103
}
118-
119-
expect(tracker.stats.torrentsCount === torrents.size);
120-
expect(tracker.stats.peersCount === activePeersCount);
121104
});
122105
});

0 commit comments

Comments
 (0)