Alex Potsides 978eb3676f
feat: async peerstore backed by datastores (#1058)
We have a peerstore that keeps all data for all observed peers in memory with no eviction.

This is fine when you don't discover many peers but when using the DHT you encounter a significant number of peers so our peer storage grows and grows over time.

We have a persistent peer store, but it just periodically writes peers into the datastore to be read at startup, still keeping them in memory.

It also means a restart doesn't give you any temporary reprieve from the memory leak as the previously observed peer data is read into memory at startup.

This change refactors the peerstore to use a datastore by default, reading and writing peer info as it arrives.  It can be configured with a MemoryDatastore if desired.

It was necessary to change the peerstore and *book interfaces to be asynchronous since the datastore api is asynchronous.

BREAKING CHANGE: `libp2p.handle`, `libp2p.registrar.register` and the peerstore methods have become async
2022-01-20 12:03:35 +00:00

133 lines
3.9 KiB
JavaScript

'use strict'
/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')
const peerUtils = require('../utils/creators/peer')
const mockConnection = require('../utils/mockConnection')
const baseOptions = require('../utils/base-options.browser')
describe('Connection Manager', () => {
let libp2p
afterEach(async () => {
sinon.restore()
libp2p && await libp2p.stop()
})
it('should be able to create without metrics', async () => {
[libp2p] = await peerUtils.createPeer({
config: {
modules: baseOptions.modules
},
started: false
})
const spy = sinon.spy(libp2p.connectionManager, 'start')
await libp2p.start()
expect(spy).to.have.property('callCount', 1)
expect(libp2p.connectionManager._metrics).to.not.exist()
})
it('should be able to create with metrics', async () => {
[libp2p] = await peerUtils.createPeer({
config: {
modules: baseOptions.modules,
metrics: {
enabled: true
}
},
started: false
})
const spy = sinon.spy(libp2p.connectionManager, 'start')
await libp2p.start()
expect(spy).to.have.property('callCount', 1)
expect(libp2p.connectionManager._libp2p.metrics).to.exist()
})
it('should close lowest value peer connection when the maximum has been reached', async () => {
const max = 5
;[libp2p] = await peerUtils.createPeer({
config: {
modules: baseOptions.modules,
connectionManager: {
maxConnections: max,
minConnections: 2
}
},
started: false
})
await libp2p.start()
sinon.spy(libp2p.connectionManager, '_maybeDisconnectOne')
// Add 1 too many connections
const spies = new Map()
await Promise.all([...new Array(max + 1)].map(async (_, index) => {
const connection = await mockConnection()
const spy = sinon.spy(connection, 'close')
// The connections have the same remote id, give them random ones
// so that we can verify the correct connection was closed
sinon.stub(connection.remotePeer, 'toB58String').returns(index)
const value = Math.random()
spies.set(value, spy)
libp2p.connectionManager.setPeerValue(connection.remotePeer, value)
await libp2p.connectionManager.onConnect(connection)
}))
// get the lowest value
const lowest = Array.from(spies.keys()).sort()[0]
const lowestSpy = spies.get(lowest)
expect(libp2p.connectionManager._maybeDisconnectOne).to.have.property('callCount', 1)
expect(lowestSpy).to.have.property('callCount', 1)
})
it('should close connection when the maximum has been reached even without peer values', async () => {
const max = 5
;[libp2p] = await peerUtils.createPeer({
config: {
modules: baseOptions.modules,
connectionManager: {
maxConnections: max,
minConnections: 0
}
},
started: false
})
await libp2p.start()
sinon.spy(libp2p.connectionManager, '_maybeDisconnectOne')
// Add 1 too many connections
const spy = sinon.spy()
await Promise.all([...new Array(max + 1)].map(async () => {
const connection = await mockConnection()
sinon.stub(connection, 'close').callsFake(async () => spy()) // eslint-disable-line
await libp2p.connectionManager.onConnect(connection)
}))
expect(libp2p.connectionManager._maybeDisconnectOne).to.have.property('callCount', 1)
expect(spy).to.have.property('callCount', 1)
})
it('should fail if the connection manager has mismatched connection limit options', async () => {
await expect(peerUtils.createPeer({
config: {
modules: baseOptions.modules,
connectionManager: {
maxConnections: 5,
minConnections: 6
}
},
started: false
})).to.eventually.rejected('maxConnections must be greater')
})
})