Improve status logging

This commit is contained in:
2025-05-30 16:17:29 -07:00
parent 40da9191fe
commit 1c5ce87922

View File

@@ -6,6 +6,7 @@
import { generateID } from "IDUtils"; import { generateID } from "IDUtils";
import { log, logID } from "log"; import { log, logID } from "log";
import { App } from "./App";
// Use a broadcast channel to only have one peer manager for multiple tabs, // Use a broadcast channel to only have one peer manager for multiple tabs,
// then we won't need to have a session ID as all queries for a peerID will be coming from the same peer manager // then we won't need to have a session ID as all queries for a peerID will be coming from the same peer manager
@@ -53,6 +54,36 @@ export class PeerManager {
// } // }
// } // }
animals = ['shrew', 'jerboa', 'lemur', 'weasel', 'possum', 'possum', 'marmoset', 'planigale', 'mole', 'narwhal'];
adjectives = ['snazzy', 'whimsical', 'jazzy', 'bonkers', 'wobbly', 'spiffy', 'chirpy', 'zesty', 'bubbly', 'perky', 'sassy'];
snakes = ['mamba', 'cobra', 'python', 'viper', 'krait', 'sidewinder', 'constrictor', 'boa', 'asp', 'anaconda', 'krait']
hashIdToIndices(id: string) {
let indices = [];
for (let char of id) {
if (char !== '0' && char !== '-') {
indices.push(parseInt(char, 16));
if (indices.length == 2) {
break;
}
}
}
return [indices[0], indices[1]];
}
funkyName(id: string, listOne: string[], listTwo: string[]) {
let [one, two] = this.hashIdToIndices(id);
let first = listOne[one % listOne.length];
let second = listTwo[two % listTwo.length];
return { first, second }
}
getPeername(peerID:string) {
let { first: adjective, second: snake } = this.funkyName(peerID, this.adjectives, this.snakes);
let peername = `${adjective}_${snake}`
return peername;
}
websocketSend(message: any) { websocketSend(message: any) {
if (!this.websocket) { if (!this.websocket) {
throw new Error(); throw new Error();
@@ -259,7 +290,21 @@ export class PeerManager {
this.watchdogInterval = setInterval(() => { this.watchdogInterval = setInterval(() => {
if (!this.isBootstrapPeer && this.peers.size === 0) { let numActive = 0;
for (let [id, peer] of this.peers) {
if (id === this.bootstrapPeerID ||
peer.rtcPeer?.connectionState === "new" ||
peer.rtcPeer?.connectionState === "connecting"
) {
continue;
}
numActive++;
}
if (!this.isBootstrapPeer && numActive === 0) {
console.log.apply(null, log(`No peers connected, will attempt to reconnect in ${this.reconnectPeriod} seconds...`)); console.log.apply(null, log(`No peers connected, will attempt to reconnect in ${this.reconnectPeriod} seconds...`));
// Websocket reconnect // Websocket reconnect
@@ -274,11 +319,14 @@ export class PeerManager {
} }
let output = `local peerID:${logID(this.peerID)}` + "\n"; let output = `Current status:` + "\n" + `[${logID(this.peerID)}]${this.getPeername(this.peerID)}[local]` + "\n";
for (let [peerID, peer] of this.peers) { for (let [peerID, peer] of this.peers) {
output += `${logID(peerID)}: ${peer.rtcPeer?.connectionState}` + "\n"; output += `[${logID(peerID)}]${peer.rtcPeer?.connectionState}:${this.getPeername(peerID)}${(peerID === this.bootstrapPeerID) ? "[Bootstrap]":""}` + "\n";
} }
output += `numActivePeers: ${numActive}` + "\n";
console.log.apply(null, log(output)); console.log.apply(null, log(output));
}, this.reconnectPeriod * 1000); }, this.reconnectPeriod * 1000);
} }
@@ -845,7 +893,7 @@ class PeerConnection {
if (type === "initial_peers") { if (type === "initial_peers") {
for (let peerID of message.peers) { for (let peerID of message.peers) {
console.log(log("Connecting to initial peer ", peerID)); console.log.apply(null, log("Connecting to initial peer ", peerID));
this.peerManager.connectToPeer(peerID); this.peerManager.connectToPeer(peerID);
} }
} }