From b3349854b6ad084d51183fe17618e5da47b393e9 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:01:41 +0100 Subject: [PATCH 1/2] Expose serverConnections and clientConnections --- README.md | 6 ++++++ index.js | 16 ++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e1cc34bb..57830a5a 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,12 @@ See the [`PeerInfo`](https://github.com/holepunchto/hyperswarm/blob/v3/README.md #### `swarm.dht` A [`hyperdht`](https://github.com/holepunchto/hyperdht) instance. Useful if you want lower-level control over Hyperswarm's networking. +### `swarm.clientConnections` +The amount of open connections from client peers connecting with you. + +### `swarm.serverConnections` +THe amount of open connections to serving peers you connected with. + #### `swarm.on('connection', (socket, peerInfo) => {})` Emitted whenever the swarm connects to a new peer. diff --git a/index.js b/index.js index 58f107d5..a18b6dc0 100644 --- a/index.js +++ b/index.js @@ -69,8 +69,8 @@ module.exports = class Hyperswarm extends EventEmitter { this._flushTick = 0 this._drainingQueue = false - this._clientConnections = 0 - this._serverConnections = 0 + this.clientConnections = 0 + this.serverConnections = 0 this._firewall = firewall this.dht.on('network-change', this._handleNetworkChange.bind(this)) @@ -115,7 +115,7 @@ module.exports = class Hyperswarm extends EventEmitter { } _flushAllMaybe () { - if (this.connecting > 0 || (this._allConnections.size < this.maxPeers && this._clientConnections < this.maxClientConnections)) { + if (this.connecting > 0 || (this._allConnections.size < this.maxPeers && this.clientConnections < this.maxClientConnections)) { return false } @@ -132,7 +132,7 @@ module.exports = class Hyperswarm extends EventEmitter { !this.suspended && this.connecting < this.maxParallel && this._allConnections.size < this.maxPeers && - this._clientConnections < this.maxClientConnections + this.clientConnections < this.maxClientConnections } _shouldRequeue (peerInfo) { @@ -168,14 +168,14 @@ module.exports = class Hyperswarm extends EventEmitter { this._allConnections.add(conn) this.connecting++ - this._clientConnections++ + this.clientConnections++ let opened = false conn.on('close', () => { if (!opened) this._connectDone() this.connections.delete(conn) this._allConnections.delete(conn) - this._clientConnections-- + this.clientConnections-- peerInfo._disconnected() peerInfo.waiting = this._shouldRequeue(peerInfo) && this._timer.add(peerInfo) @@ -293,12 +293,12 @@ module.exports = class Hyperswarm extends EventEmitter { this.connections.add(conn) this._allConnections.add(conn) - this._serverConnections++ + this.serverConnections++ conn.on('close', () => { this.connections.delete(conn) this._allConnections.delete(conn) - this._serverConnections-- + this.serverConnections-- this._maybeDeletePeer(peerInfo) From f0ca6992a2f8e49d8f8f322cf4f62e1891bb69c6 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:31:18 +0100 Subject: [PATCH 2/2] Fix clientConnections and serverConnections readme description --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 57830a5a..218e2c91 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,10 @@ See the [`PeerInfo`](https://github.com/holepunchto/hyperswarm/blob/v3/README.md A [`hyperdht`](https://github.com/holepunchto/hyperdht) instance. Useful if you want lower-level control over Hyperswarm's networking. ### `swarm.clientConnections` -The amount of open connections from client peers connecting with you. +The amount of open connections you initiated as a client. ### `swarm.serverConnections` -THe amount of open connections to serving peers you connected with. +THe amount of open connections where you are the server (and the other peer connected to you). #### `swarm.on('connection', (socket, peerInfo) => {})` Emitted whenever the swarm connects to a new peer.