Skip to content

Commit a37f282

Browse files
committed
feat(p2p): I added a connection rotation and limited max connections
Implement connection rotation to periodically close oldest connections when exceeding half of MAX_CONNECTIONS. Also enforce MAX_CONNECTIONS limit by rejecting new connections when limit is reached (32). This improves network stability and resource management. I also changed max relay hops from 3 to 2. With the connection rotation I think these more conservative numbers should suffice and save on resources.
1 parent 5613c2e commit a37f282

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/config/constants.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ const POW_PREFIX = "0000";
1515

1616
const MAX_PEERS = parseInt(process.env.MAX_PEERS) || 10000;
1717
const MAX_MESSAGE_SIZE = 2048;
18-
const MAX_RELAY_HOPS = 3;
18+
const MAX_RELAY_HOPS = 2;
19+
const MAX_CONNECTIONS = 32;
1920

2021
const HEARTBEAT_INTERVAL = 5000;
22+
const CONNECTION_ROTATION_INTERVAL = 30000;
2123
const PEER_TIMEOUT = 15000;
2224
const BROADCAST_THROTTLE = 1000;
2325
const DIAGNOSTICS_INTERVAL = 10000;
@@ -30,7 +32,9 @@ module.exports = {
3032
MAX_PEERS,
3133
MAX_MESSAGE_SIZE,
3234
MAX_RELAY_HOPS,
35+
MAX_CONNECTIONS,
3336
HEARTBEAT_INTERVAL,
37+
CONNECTION_ROTATION_INTERVAL,
3438
PEER_TIMEOUT,
3539
BROADCAST_THROTTLE,
3640
DIAGNOSTICS_INTERVAL,

src/p2p/swarm.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Hyperswarm = require("hyperswarm");
22
const { signMessage } = require("../core/security");
3-
const { TOPIC, TOPIC_NAME, HEARTBEAT_INTERVAL } = require("../config/constants");
3+
const { TOPIC, TOPIC_NAME, HEARTBEAT_INTERVAL, MAX_CONNECTIONS, CONNECTION_ROTATION_INTERVAL } = require("../config/constants");
44

55
class SwarmManager {
66
constructor(identity, peerManager, diagnostics, messageHandler, relayFn, broadcastFn) {
@@ -13,6 +13,7 @@ class SwarmManager {
1313

1414
this.swarm = new Hyperswarm();
1515
this.heartbeatInterval = null;
16+
this.rotationInterval = null;
1617
}
1718

1819
async start() {
@@ -22,9 +23,17 @@ class SwarmManager {
2223
await discovery.flushed();
2324

2425
this.startHeartbeat();
26+
this.startRotation();
2527
}
2628

2729
handleConnection(socket) {
30+
if (this.swarm.connections.size > MAX_CONNECTIONS) {
31+
socket.destroy();
32+
return;
33+
}
34+
35+
socket.connectedAt = Date.now();
36+
2837
const sig = signMessage(`seq:${this.peerManager.getSeq()}`, this.identity.privateKey);
2938
const hello = JSON.stringify({
3039
type: "HEARTBEAT",
@@ -88,6 +97,23 @@ class SwarmManager {
8897
}, HEARTBEAT_INTERVAL);
8998
}
9099

100+
startRotation() {
101+
this.rotationInterval = setInterval(() => {
102+
if (this.swarm.connections.size < MAX_CONNECTIONS / 2) return;
103+
104+
let oldest = null;
105+
for (const socket of this.swarm.connections) {
106+
if (!oldest || socket.connectedAt < oldest.connectedAt) {
107+
oldest = socket;
108+
}
109+
}
110+
111+
if (oldest) {
112+
oldest.destroy();
113+
}
114+
}, CONNECTION_ROTATION_INTERVAL);
115+
}
116+
91117
shutdown() {
92118
const sig = signMessage(`type:LEAVE:${this.identity.id}`, this.identity.privateKey);
93119
const goodbye = JSON.stringify({
@@ -105,6 +131,10 @@ class SwarmManager {
105131
clearInterval(this.heartbeatInterval);
106132
}
107133

134+
if (this.rotationInterval) {
135+
clearInterval(this.rotationInterval);
136+
}
137+
108138
setTimeout(() => {
109139
process.exit(0);
110140
}, 500);

0 commit comments

Comments
 (0)