feat: add libp2p.connections getter (#522)

* fix: make hangup accept what the API says it does

* feat: add libp2p.connections getter

* chore: fix typo
This commit is contained in:
Jacob Heun 2019-12-16 19:24:35 +01:00 committed by GitHub
parent 24c3ce6f8d
commit 6ca19c5ef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 9 deletions

View File

@ -181,6 +181,29 @@ const libp2p = await Libp2p.create(options)
await libp2p.stop() 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 ### dial
Dials to another peer in the network and establishes the connection. Dials to another peer in the network and establishes the connection.
@ -191,7 +214,7 @@ Dials to another peer in the network and establishes the connection.
| Name | Type | Description | | 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] | `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 | | [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 |
@ -199,7 +222,7 @@ Dials to another peer in the network and establishes the connection.
| Type | Description | | 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 #### Example
@ -226,7 +249,7 @@ Dials to another peer in the network and selects a protocol to communicate with
| Name | Type | Description | | 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') | | 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] | `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 | | [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 |
@ -259,7 +282,7 @@ Attempts to gracefully close an open connection to the given peer. If the connec
| Name | Type | Description | | 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 #### Returns
@ -355,7 +378,7 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e
| Name | Type | Description | | 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 | `Object` | operation options |
| options.timeout | `number` | maximum time the query should run | | options.timeout | `number` | maximum time the query should run |
@ -773,3 +796,6 @@ console.log(peerStats.toJSON())
- `['60000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 1 minute interval. - `['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. - `['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. - `['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

View File

@ -240,6 +240,15 @@ class Libp2p extends EventEmitter {
return this._isStarted 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 * Dials to the provided peer. If successful, the `PeerInfo` of the
* peer will be added to the nodes `peerStore` * peer will be added to the nodes `peerStore`
@ -288,12 +297,13 @@ class Libp2p extends EventEmitter {
/** /**
* Disconnects all connections to the given `peer` * 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>} * @returns {Promise<void>}
*/ */
hangUp (peer) { hangUp (peer) {
const peerInfo = getPeerInfo(peer, this.peerStore)
return Promise.all( return Promise.all(
this.registrar.connections.get(peer.toString()).map(connection => { this.registrar.connections.get(peerInfo.id.toString()).map(connection => {
return connection.close() return connection.close()
}) })
) )

View File

@ -296,6 +296,23 @@ describe('Dialing (direct, TCP)', () => {
expect(connection.stat.timeline.close).to.exist() 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 () => { it('should use the protectors when provided for connecting', async () => {
const protector = new Protector(swarmKeyBuffer) const protector = new Protector(swarmKeyBuffer)
libp2p = new Libp2p({ libp2p = new Libp2p({

View File

@ -61,12 +61,12 @@ describe('registrar on dial', () => {
})) }))
await libp2p.dial(remoteAddr) await libp2p.dial(remoteAddr)
expect(libp2p.registrar.connections.size).to.equal(1) expect(libp2p.connections.size).to.equal(1)
sinon.spy(libp2p.registrar, 'close') sinon.spy(libp2p.registrar, 'close')
await libp2p.stop() await libp2p.stop()
expect(libp2p.registrar.close.callCount).to.equal(1) expect(libp2p.registrar.close.callCount).to.equal(1)
expect(libp2p.registrar.connections.size).to.equal(0) expect(libp2p.connections.size).to.equal(0)
}) })
}) })