fix: add timeout for incoming connections and build-in protocols (#1292)

Ensure that we don't wait forever for upgrading an inbound connection
to occur.

Note that transports should return an AbortableSource when passed an
AbortSignal so outbound connections to not need the same fix.

Also adds default timeouts for the ping, fetch, and identify protocols.
This commit is contained in:
Alex Potsides
2022-07-14 13:35:12 +00:00
committed by GitHub
parent b1b91398e2
commit 750ed9c35f
20 changed files with 271 additions and 148 deletions

View File

@ -54,7 +54,8 @@ describe('Connection Manager', () => {
const connectionManager = new DefaultConnectionManager({
maxConnections: 1000,
minConnections: 50,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
connectionManager.init(new Components({ upgrader, peerStore }))
@ -89,7 +90,8 @@ describe('Connection Manager', () => {
const connectionManager = new DefaultConnectionManager({
maxConnections: 1000,
minConnections: 50,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
connectionManager.init(new Components({ upgrader, peerStore }))

View File

@ -10,6 +10,7 @@ import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/int
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { CustomEvent } from '@libp2p/interfaces/events'
import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags'
import pWaitFor from 'p-wait-for'
describe('Connection Manager', () => {
let libp2p: Libp2pNode
@ -163,6 +164,10 @@ describe('Connection Manager', () => {
await libp2p.stop()
await libp2p.start()
await pWaitFor(() => connectionManagerOpenConnectionSpy.called, {
interval: 100
})
expect(connectionManagerOpenConnectionSpy.called).to.be.true('Did not attempt to connect to important peer')
expect(connectionManagerOpenConnectionSpy.getCall(0).args[0].toString()).to.equal(peerId.toString(), 'Attempted to connect to the wrong peer')
})

View File

@ -74,7 +74,8 @@ describe('Dialing (direct, TCP)', () => {
localComponents.setConnectionManager(new DefaultConnectionManager({
maxConnections: 100,
minConnections: 50,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
}))
localTM = new DefaultTransportManager(localComponents)

View File

@ -52,7 +52,8 @@ describe('Dialing (direct, WebSockets)', () => {
localComponents.setConnectionManager(new DefaultConnectionManager({
maxConnections: 100,
minConnections: 50,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
}))
localTM = new DefaultTransportManager(localComponents)

View File

@ -19,7 +19,8 @@ import { MemoryDatastore } from 'datastore-core'
const defaultInit: FetchServiceInit = {
protocolPrefix: 'ipfs',
maxInboundStreams: 1,
maxOutboundStreams: 1
maxOutboundStreams: 1,
timeout: 1000
}
async function createComponents (index: number) {
@ -34,7 +35,8 @@ async function createComponents (index: number) {
connectionManager: new DefaultConnectionManager({
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
})

View File

@ -40,7 +40,8 @@ const defaultInit: IdentifyServiceInit = {
maxInboundStreams: 1,
maxOutboundStreams: 1,
maxPushIncomingStreams: 1,
maxPushOutgoingStreams: 1
maxPushOutgoingStreams: 1,
timeout: 1000
}
const protocols = [MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_PUSH]
@ -58,7 +59,8 @@ async function createComponents (index: number) {
connectionManager: new DefaultConnectionManager({
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
})
components.setAddressManager(new DefaultAddressManager(components, {

View File

@ -35,7 +35,8 @@ const defaultInit: IdentifyServiceInit = {
maxInboundStreams: 1,
maxOutboundStreams: 1,
maxPushIncomingStreams: 1,
maxPushOutgoingStreams: 1
maxPushOutgoingStreams: 1,
timeout: 1000
}
const protocols = [MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_PUSH]
@ -53,7 +54,8 @@ async function createComponents (index: number) {
connectionManager: new DefaultConnectionManager({
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
})
components.setAddressManager(new DefaultAddressManager(components, {

View File

@ -19,7 +19,8 @@ import { MemoryDatastore } from 'datastore-core'
const defaultInit: PingServiceInit = {
protocolPrefix: 'ipfs',
maxInboundStreams: 1,
maxOutboundStreams: 1
maxOutboundStreams: 1,
timeout: 1000
}
async function createComponents (index: number) {
@ -34,7 +35,8 @@ async function createComponents (index: number) {
connectionManager: new DefaultConnectionManager({
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
})

View File

@ -43,7 +43,8 @@ describe('registrar', () => {
connectionManager: new DefaultConnectionManager({
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})
})
registrar = new DefaultRegistrar(components)

View File

@ -66,7 +66,8 @@ describe('Upgrader', () => {
],
muxers: [
localMuxerFactory
]
],
inboundUpgradeTimeout: 1000
})
remoteComponents = new Components({
@ -80,7 +81,8 @@ describe('Upgrader', () => {
],
muxers: [
new Mplex()
]
],
inboundUpgradeTimeout: 1000
})
await localComponents.getRegistrar().handle('/echo/1.0.0', ({ stream }) => {
@ -137,13 +139,15 @@ describe('Upgrader', () => {
connectionEncryption: [
new Plaintext()
],
muxers: []
muxers: [],
inboundUpgradeTimeout: 1000
})
remoteUpgrader = new DefaultUpgrader(remoteComponents, {
connectionEncryption: [
new Plaintext()
],
muxers: []
muxers: [],
inboundUpgradeTimeout: 1000
})
const connections = await Promise.all([
@ -214,13 +218,15 @@ describe('Upgrader', () => {
connectionEncryption: [
new BoomCrypto()
],
muxers: []
muxers: [],
inboundUpgradeTimeout: 1000
})
remoteUpgrader = new DefaultUpgrader(remoteComponents, {
connectionEncryption: [
new BoomCrypto()
],
muxers: []
muxers: [],
inboundUpgradeTimeout: 1000
})
// Wait for the results of each side of the connection
@ -266,7 +272,8 @@ describe('Upgrader', () => {
],
muxers: [
new OtherMuxerFactory()
]
],
inboundUpgradeTimeout: 1000
})
remoteUpgrader = new DefaultUpgrader(remoteComponents, {
connectionEncryption: [
@ -274,7 +281,8 @@ describe('Upgrader', () => {
],
muxers: [
new Mplex()
]
],
inboundUpgradeTimeout: 1000
})
// Wait for the results of each side of the connection