Skip to content

Commit

Permalink
feat: add libp2p.connections getter (#522)
Browse files Browse the repository at this point in the history
* fix: make hangup accept what the API says it does

* feat: add libp2p.connections getter

* chore: fix typo
  • Loading branch information
jacobheun committed Dec 16, 2019
1 parent 24c3ce6 commit 6ca19c5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
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)
})
})

0 comments on commit 6ca19c5

Please sign in to comment.