mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-25 23:11:35 +00:00
fix: report dialer metrics (#1377)
Converts the dialer to a component so it can access metrics
This commit is contained in:
@ -9,7 +9,8 @@ import { DialAction, DialRequest } from '../../src/connection-manager/dialer/dia
|
||||
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks'
|
||||
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
||||
import { Multiaddr } from '@multiformats/multiaddr'
|
||||
import { Dialer } from '../../src/connection-manager/dialer/index.js'
|
||||
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'
|
||||
import { Components } from '@libp2p/components'
|
||||
const error = new Error('dial failure')
|
||||
|
||||
describe('Dial Request', () => {
|
||||
@ -23,7 +24,7 @@ describe('Dial Request', () => {
|
||||
}
|
||||
const dialAction: DialAction = async (num) => await actions[num.toString()]()
|
||||
const controller = new AbortController()
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(new Components(), {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||
@ -53,7 +54,7 @@ describe('Dial Request', () => {
|
||||
}
|
||||
const dialAction: DialAction = async (num) => await actions[num.toString()]()
|
||||
const controller = new AbortController()
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(new Components(), {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||
@ -98,7 +99,7 @@ describe('Dial Request', () => {
|
||||
const dialAction: DialAction = async (num) => await actions[num.toString()]()
|
||||
const addrs = Object.keys(actions)
|
||||
const controller = new AbortController()
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(new Components(), {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||
@ -138,7 +139,7 @@ describe('Dial Request', () => {
|
||||
|
||||
const dialAction: DialAction = async (num) => await actions[num.toString()]()
|
||||
const controller = new AbortController()
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(new Components(), {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||
@ -184,7 +185,7 @@ describe('Dial Request', () => {
|
||||
const dialAction: DialAction = async (num) => await actions[num.toString()]()
|
||||
const addrs = Object.keys(actions)
|
||||
const controller = new AbortController()
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(new Components(), {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
const dialerReleaseTokenSpy = sinon.spy(dialer, 'releaseToken')
|
||||
@ -237,7 +238,7 @@ describe('Dial Request', () => {
|
||||
|
||||
const dialRequest = new DialRequest({
|
||||
addrs: Object.keys(actions).map(str => new Multiaddr(str)),
|
||||
dialer: new Dialer({
|
||||
dialer: new DefaultDialer(new Components(), {
|
||||
maxParallelDials: 3
|
||||
}),
|
||||
dialAction: async (ma, opts) => {
|
||||
|
@ -17,7 +17,7 @@ import { Connection, isConnection } from '@libp2p/interface-connection'
|
||||
import { AbortError } from '@libp2p/interfaces/errors'
|
||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||
import { MemoryDatastore } from 'datastore-core/memory'
|
||||
import { Dialer } from '../../src/connection-manager/dialer/index.js'
|
||||
import { DefaultDialer } from '../../src/connection-manager/dialer/index.js'
|
||||
import { DefaultAddressManager } from '../../src/address-manager/index.js'
|
||||
import { PersistentPeerStore } from '@libp2p/peer-store'
|
||||
import { DefaultTransportManager } from '../../src/transport-manager.js'
|
||||
@ -95,8 +95,7 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should be able to connect to a remote node via its multiaddr', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const connection = await dialer.dial(remoteAddr)
|
||||
expect(connection).to.exist()
|
||||
@ -104,8 +103,7 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should fail to connect to an unsupported multiaddr', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
await expect(dialer.dial(unsupportedAddr))
|
||||
.to.eventually.be.rejectedWith(Error)
|
||||
@ -113,8 +111,7 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should fail to connect if peer has no known addresses', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
const peerId = await createFromJSON(Peers[1])
|
||||
|
||||
await expect(dialer.dial(peerId))
|
||||
@ -125,8 +122,7 @@ describe('Dialing (direct, TCP)', () => {
|
||||
it('should be able to connect to a given peer id', async () => {
|
||||
await localComponents.getPeerStore().addressBook.set(remoteComponents.getPeerId(), remoteTM.getAddrs())
|
||||
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const connection = await dialer.dial(remoteComponents.getPeerId())
|
||||
expect(connection).to.exist()
|
||||
@ -136,8 +132,7 @@ describe('Dialing (direct, TCP)', () => {
|
||||
it('should fail to connect to a given peer with unsupported addresses', async () => {
|
||||
await localComponents.getPeerStore().addressBook.add(remoteComponents.getPeerId(), [unsupportedAddr])
|
||||
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
await expect(dialer.dial(remoteComponents.getPeerId()))
|
||||
.to.eventually.be.rejectedWith(Error)
|
||||
@ -150,8 +145,7 @@ describe('Dialing (direct, TCP)', () => {
|
||||
const peerId = await createFromJSON(Peers[1])
|
||||
await localComponents.getPeerStore().addressBook.add(peerId, [...remoteAddrs, unsupportedAddr])
|
||||
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
sinon.spy(localTM, 'dial')
|
||||
const connection = await dialer.dial(peerId)
|
||||
@ -162,10 +156,9 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should abort dials on queue task timeout', async () => {
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
dialTimeout: 50
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
sinon.stub(localTM, 'dial').callsFake(async (addr, options = {}) => {
|
||||
expect(options.signal).to.exist()
|
||||
@ -191,10 +184,9 @@ describe('Dialing (direct, TCP)', () => {
|
||||
|
||||
await localComponents.getPeerStore().addressBook.add(peerId, addrs)
|
||||
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
expect(dialer.tokens).to.have.lengthOf(2)
|
||||
|
||||
|
@ -13,7 +13,7 @@ import { AbortError } from '@libp2p/interfaces/errors'
|
||||
import { MemoryDatastore } from 'datastore-core/memory'
|
||||
import { codes as ErrorCodes } from '../../src/errors.js'
|
||||
import * as Constants from '../../src/constants.js'
|
||||
import { Dialer, DialTarget } from '../../src/connection-manager/dialer/index.js'
|
||||
import { DefaultDialer, DialTarget } from '../../src/connection-manager/dialer/index.js'
|
||||
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
||||
import { PersistentPeerStore } from '@libp2p/peer-store'
|
||||
import { DefaultTransportManager } from '../../src/transport-manager.js'
|
||||
@ -72,8 +72,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should limit the number of tokens it provides', () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const maxPerPeer = Constants.MAX_PER_PEER_DIALS
|
||||
expect(dialer.tokens).to.have.lengthOf(Constants.MAX_PARALLEL_DIALS)
|
||||
@ -83,10 +82,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should not return tokens if none are left', () => {
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
maxDialsPerPeer: Infinity
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
const maxTokens = dialer.tokens.length
|
||||
|
||||
@ -97,8 +95,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should NOT be able to return a token twice', () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const tokens = dialer.getTokens(1)
|
||||
expect(tokens).to.have.length(1)
|
||||
@ -109,8 +106,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should be able to connect to a remote node via its multiaddr', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||
await localComponents.getPeerStore().addressBook.set(remotePeerId, [remoteAddr])
|
||||
@ -121,8 +117,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should fail to connect to an unsupported multiaddr', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
await expect(dialer.dial(unsupportedAddr.encapsulate(`/p2p/${remoteComponents.getPeerId().toString()}`)))
|
||||
.to.eventually.be.rejectedWith(Error)
|
||||
@ -130,8 +125,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should be able to connect to a given peer', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||
await localComponents.getPeerStore().addressBook.set(remotePeerId, [remoteAddr])
|
||||
@ -142,8 +136,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should fail to connect to a given peer with unsupported addresses', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||
await localComponents.getPeerStore().addressBook.set(remotePeerId, [unsupportedAddr])
|
||||
@ -154,10 +147,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should abort dials on queue task timeout', async () => {
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
dialTimeout: 50
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||
await localComponents.getPeerStore().addressBook.set(remotePeerId, [remoteAddr])
|
||||
@ -177,10 +169,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should throw when a peer advertises more than the allowed number of peers', async () => {
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
maxAddrsToDial: 10
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||
await localComponents.getPeerStore().addressBook.set(remotePeerId, Array.from({ length: 11 }, (_, i) => new Multiaddr(`/ip4/127.0.0.1/tcp/1500${i}/ws/p2p/12D3KooWHFKTMzwerBtsVmtz4ZZEQy2heafxzWw6wNn5PPYkBxJ5`)))
|
||||
@ -200,11 +191,10 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
const publicAddressesFirstSpy = sinon.spy(publicAddressesFirst)
|
||||
const localTMDialStub = sinon.stub(localTM, 'dial').callsFake(async (ma) => mockConnection(mockMultiaddrConnection(mockDuplex(), peerIdFromString(ma.getPeerId() ?? ''))))
|
||||
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
addressSorter: publicAddressesFirstSpy,
|
||||
maxParallelDials: 3
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
// Inject data in the AddressBook
|
||||
await localComponents.getPeerStore().addressBook.add(remoteComponents.getPeerId(), peerMultiaddrs)
|
||||
@ -229,10 +219,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
]
|
||||
const remotePeerId = peerIdFromString(remoteAddr.getPeerId() ?? '')
|
||||
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
// Inject data in the AddressBook
|
||||
await localComponents.getPeerStore().addressBook.add(remotePeerId, addrs)
|
||||
@ -268,10 +257,9 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
new Multiaddr('/ip4/0.0.0.0/tcp/8001/ws'),
|
||||
new Multiaddr('/ip4/0.0.0.0/tcp/8002/ws')
|
||||
]
|
||||
const dialer = new Dialer({
|
||||
const dialer = new DefaultDialer(localComponents, {
|
||||
maxParallelDials: 2
|
||||
})
|
||||
dialer.init(localComponents)
|
||||
|
||||
// Inject data in the AddressBook
|
||||
await localComponents.getPeerStore().addressBook.add(remoteComponents.getPeerId(), addrs)
|
||||
@ -309,8 +297,7 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should cancel pending dial targets before proceeding', async () => {
|
||||
const dialer = new Dialer()
|
||||
dialer.init(localComponents)
|
||||
const dialer = new DefaultDialer(localComponents)
|
||||
|
||||
sinon.stub(dialer, '_createDialTarget').callsFake(async () => {
|
||||
const deferredDial = pDefer<DialTarget>()
|
||||
@ -364,8 +351,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
|
||||
]
|
||||
})
|
||||
|
||||
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
||||
const dialer = connectionManager.dialer
|
||||
const dialer = libp2p.components.getDialer()
|
||||
|
||||
expect(dialer).to.exist()
|
||||
expect(dialer).to.have.property('tokens').with.lengthOf(Constants.MAX_PARALLEL_DIALS)
|
||||
@ -395,8 +381,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
|
||||
}
|
||||
libp2p = await createLibp2pNode(config)
|
||||
|
||||
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
||||
const dialer = connectionManager.dialer
|
||||
const dialer = libp2p.components.getDialer()
|
||||
|
||||
expect(dialer).to.exist()
|
||||
expect(dialer).to.have.property('tokens').with.lengthOf(config.connectionManager.maxParallelDials)
|
||||
@ -420,8 +405,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
|
||||
]
|
||||
})
|
||||
|
||||
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
||||
const dialerDialSpy = sinon.spy(connectionManager.dialer, 'dial')
|
||||
const dialerDialSpy = sinon.spy(libp2p.components.getDialer(), 'dial')
|
||||
const addressBookAddSpy = sinon.spy(libp2p.components.getPeerStore().addressBook, 'add')
|
||||
|
||||
await libp2p.start()
|
||||
@ -543,8 +527,8 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
|
||||
]
|
||||
})
|
||||
|
||||
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
||||
sinon.stub(connectionManager.dialer, '_createDialTarget').callsFake(async () => {
|
||||
const dialer = libp2p.components.getDialer() as DefaultDialer
|
||||
sinon.stub(dialer, '_createDialTarget').callsFake(async () => {
|
||||
const deferredDial = pDefer<DialTarget>()
|
||||
return await deferredDial.promise
|
||||
})
|
||||
@ -582,8 +566,8 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
|
||||
|
||||
await libp2p.start()
|
||||
|
||||
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
||||
const dialerDestroyStub = sinon.spy(connectionManager.dialer, 'stop')
|
||||
const dialer = libp2p.components.getDialer() as DefaultDialer
|
||||
const dialerDestroyStub = sinon.spy(dialer, 'stop')
|
||||
|
||||
await libp2p.stop()
|
||||
|
||||
|
Reference in New Issue
Block a user