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

feat: add libp2p.connections getter #522

Merged
merged 3 commits into from
Dec 16, 2019
Merged
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
36 changes: 31 additions & 5 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ const libp2p = await Libp2p.create(options)
await libp2p.stop()
```

### connections

A Getter that returns a Map of the current Connections libp2p has to other peers.

`libp2p.connections`

#### Returns

| Type | Description |
|------|-------------|
| `Map<string, Array<Connection>>` | A map of [`PeerId`][peer-id] strings to [`Connection`][connection] Arrays |

#### Example

```js
for (const [peerId, connections] of libp2p.connections) {
for (const connection of connections) {
console.log(peerId, connection.remoteAddr.toString())
// Logs the PeerId string and the observed remote multiaddr of each Connection
}
}
```

### dial

Dials to another peer in the network and establishes the connection.
Expand All @@ -191,15 +214,15 @@ Dials to another peer in the network and establishes the connection.

| Name | Type | Description |
|------|------|-------------|
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId](https://github.com/libp2p/js-peer-id), [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
| [options] | `Object` | dial options |
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<Connection>` | Promise resolves with the [Connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) instance |
| `Promise<Connection>` | Promise resolves with the [Connection][connection] instance |

#### Example

Expand All @@ -226,7 +249,7 @@ Dials to another peer in the network and selects a protocol to communicate with

| Name | Type | Description |
|------|------|-------------|
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId](https://github.com/libp2p/js-peer-id), [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
| protocols | `String|Array<String>` | A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made. (e.g '/ipfs/bitswap/1.1.0') |
| [options] | `Object` | dial options |
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
Expand Down Expand Up @@ -259,7 +282,7 @@ Attempts to gracefully close an open connection to the given peer. If the connec

| Name | Type | Description |
|------|------|-------------|
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId](https://github.com/libp2p/js-peer-id), [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to hang up |
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to hang up |

#### Returns

Expand Down Expand Up @@ -355,7 +378,7 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e

| Name | Type | Description |
|------|------|-------------|
| peerId | [`PeerId`](https://github.com/libp2p/js-peer-id) | ID of the peer to find |
| peerId | [`PeerId`][peer-id] | ID of the peer to find |
| options | `Object` | operation options |
| options.timeout | `number` | maximum time the query should run |

Expand Down Expand Up @@ -773,3 +796,6 @@ console.log(peerStats.toJSON())
- `['60000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 1 minute interval.
- `['300000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 5 minute interval.
- `['900000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 15 minute interval.

[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
[peer-id]: https://github.com/libp2p/js-peer-id
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ class Libp2p extends EventEmitter {
return this._isStarted
}

/**
* Gets a Map of the current connections. The keys are the stringified
* `PeerId` of the peer. The value is an array of Connections to that peer.
* @returns {Map<string, Connection[]>}
*/
get connections () {
return this.registrar.connections
}

/**
* Dials to the provided peer. If successful, the `PeerInfo` of the
* peer will be added to the nodes `peerStore`
Expand Down Expand Up @@ -288,12 +297,13 @@ class Libp2p extends EventEmitter {
/**
* Disconnects all connections to the given `peer`
*
* @param {PeerId} peer The PeerId to close connections to
* @param {PeerInfo|PeerId|multiaddr|string} peer the peer to close connections to
* @returns {Promise<void>}
*/
hangUp (peer) {
const peerInfo = getPeerInfo(peer, this.peerStore)
return Promise.all(
this.registrar.connections.get(peer.toString()).map(connection => {
this.registrar.connections.get(peerInfo.id.toString()).map(connection => {
return connection.close()
})
)
Expand Down
17 changes: 17 additions & 0 deletions test/dialing/direct.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ describe('Dialing (direct, TCP)', () => {
expect(connection.stat.timeline.close).to.exist()
})

it('should be able to use hangup by address string to close connections', async () => {
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})

const connection = await libp2p.dial(`${remoteAddr.toString()}/p2p/${remotePeerInfo.id.toString()}`)
expect(connection).to.exist()
expect(connection.stat.timeline.close).to.not.exist()
await libp2p.hangUp(connection.remotePeer)
expect(connection.stat.timeline.close).to.exist()
})

it('should use the protectors when provided for connecting', async () => {
const protector = new Protector(swarmKeyBuffer)
libp2p = new Libp2p({
Expand Down
4 changes: 2 additions & 2 deletions test/registrar/registrar.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ describe('registrar on dial', () => {
}))

await libp2p.dial(remoteAddr)
expect(libp2p.registrar.connections.size).to.equal(1)
expect(libp2p.connections.size).to.equal(1)

sinon.spy(libp2p.registrar, 'close')

await libp2p.stop()
expect(libp2p.registrar.close.callCount).to.equal(1)
expect(libp2p.registrar.connections.size).to.equal(0)
expect(libp2p.connections.size).to.equal(0)
})
})