mirror of
https://github.com/lklynet/hypermind.git
synced 2026-05-03 09:30:36 +00:00
perf(peer-handling): optimize peer verification and cleanup intervals
- Cache peer public keys to avoid repeated key creation - Increase peer cleanup interval from 2.5s to 15s to reduce overhead - Adjust cleanup check interval from 500ms to 5s
This commit is contained in:
@@ -118,11 +118,18 @@ function handleMessage(msg, sourceSocket) {
|
||||
// 2. Verify Signature
|
||||
if (!sig) return;
|
||||
try {
|
||||
const key = crypto.createPublicKey({
|
||||
key: Buffer.from(id, "hex"),
|
||||
format: "der",
|
||||
type: "spki",
|
||||
});
|
||||
let key;
|
||||
const storedPeer = seenPeers.get(id);
|
||||
if (storedPeer && storedPeer.key) {
|
||||
key = storedPeer.key;
|
||||
} else {
|
||||
key = crypto.createPublicKey({
|
||||
key: Buffer.from(id, "hex"),
|
||||
format: "der",
|
||||
type: "spki",
|
||||
});
|
||||
}
|
||||
|
||||
const verified = crypto.verify(
|
||||
null,
|
||||
Buffer.from(`seq:${seq}`),
|
||||
@@ -130,36 +137,41 @@ function handleMessage(msg, sourceSocket) {
|
||||
Buffer.from(sig, "hex")
|
||||
);
|
||||
if (!verified) return; // Invalid Signature
|
||||
|
||||
// Store key for future use if not already stored
|
||||
if (!storedPeer || !storedPeer.key) {
|
||||
// We'll attach it when we update seenPeers below
|
||||
}
|
||||
|
||||
if (hops === 0) {
|
||||
sourceSocket.peerId = id;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
const stored = seenPeers.get(id);
|
||||
|
||||
let shouldUpdate = false;
|
||||
|
||||
if (!stored) {
|
||||
// New peer
|
||||
shouldUpdate = true;
|
||||
} else if (seq > stored.seq) {
|
||||
shouldUpdate = true;
|
||||
}
|
||||
|
||||
if (shouldUpdate) {
|
||||
const wasNew = !stored;
|
||||
seenPeers.set(id, { seq, lastSeen: now, key });
|
||||
|
||||
if (wasNew) broadcastUpdate();
|
||||
|
||||
if (hops < 3) {
|
||||
relayMessage({ ...msg, hops: hops + 1 }, sourceSocket);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hops === 0) {
|
||||
sourceSocket.peerId = id;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
const stored = seenPeers.get(id);
|
||||
|
||||
let shouldUpdate = false;
|
||||
|
||||
if (!stored) {
|
||||
// New peer
|
||||
shouldUpdate = true;
|
||||
} else if (seq > stored.seq) {
|
||||
shouldUpdate = true;
|
||||
}
|
||||
|
||||
if (shouldUpdate) {
|
||||
const wasNew = !stored;
|
||||
seenPeers.set(id, { seq, lastSeen: now });
|
||||
|
||||
if (wasNew) broadcastUpdate();
|
||||
|
||||
if (hops < 3) {
|
||||
relayMessage({ ...msg, hops: hops + 1 }, sourceSocket);
|
||||
}
|
||||
}
|
||||
} else if (msg.type === "LEAVE") {
|
||||
const { id, hops } = msg;
|
||||
if (seenPeers.has(id)) {
|
||||
@@ -207,14 +219,14 @@ setInterval(() => {
|
||||
const now = Date.now();
|
||||
let changed = false;
|
||||
for (const [id, data] of seenPeers) {
|
||||
if (now - data.lastSeen > 2500) {
|
||||
if (now - data.lastSeen > 15000) {
|
||||
seenPeers.delete(id);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) broadcastUpdate();
|
||||
}, 500);
|
||||
}, 5000);
|
||||
|
||||
// Graceful Shutdown
|
||||
function handleShutdown() {
|
||||
|
||||
Reference in New Issue
Block a user