Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose serverConnections and clientConnections #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 you initiated as a client.

### `swarm.serverConnections`
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.

Expand Down
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down