Skip to content

Commit

Permalink
fix: remove duplicate autodial from startup (#2289)
Browse files Browse the repository at this point in the history
There's no need to set a timeout for autodialing during `.start`
since we autodial during `.afterStart` which will then set the
timeout for the next dial if one is required.

We guard on running twice so this isn't a problem, it's just
unnecessary.
  • Loading branch information
achingbrain committed Dec 2, 2023
1 parent 10ea197 commit bcfa159
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions packages/libp2p/src/connection-manager/auto-dial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ export class AutoDial implements Startable {
}

start (): void {
this.autoDialInterval = setTimeout(() => {
this.autoDial()
.catch(err => {
this.log.error('error while autodialing', err)
})
}, this.autoDialIntervalMs)
this.started = true
}

Expand All @@ -126,28 +120,27 @@ export class AutoDial implements Startable {
}

async autoDial (): Promise<void> {
if (!this.started) {
if (!this.started || this.running) {
return
}

const connections = this.connectionManager.getConnectionsMap()
const numConnections = connections.size

// Already has enough connections
// already have enough connections
if (numConnections >= this.minConnections) {
if (this.minConnections > 0) {
this.log.trace('have enough connections %d/%d', numConnections, this.minConnections)
}

// no need to schedule next autodial as it will be run when on
// connection:close event
return
}

if (this.queue.size > this.autoDialMaxQueueLength) {
this.log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
return
}

if (this.running) {
this.log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
this.sheduleNextAutodial()
return
}

Expand All @@ -162,12 +155,12 @@ export class AutoDial implements Startable {
.filter(Boolean)
)

// Sort peers on whether we know protocols or public keys for them
// sort peers on whether we know protocols or public keys for them
const peers = await this.peerStore.all({
filters: [
// Remove some peers
// remove some peers
(peer) => {
// Remove peers without addresses
// remove peers without addresses
if (peer.addresses.length === 0) {
this.log.trace('not autodialing %p because they have no addresses', peer.id)
return false
Expand Down Expand Up @@ -200,7 +193,7 @@ export class AutoDial implements Startable {
// dialled in a different order each time
const shuffledPeers = peers.sort(() => Math.random() > 0.5 ? 1 : -1)

// Sort shuffled peers by tag value
// sort shuffled peers by tag value
const peerValues = new PeerMap<number>()
for (const peer of shuffledPeers) {
if (peerValues.has(peer.id)) {
Expand Down Expand Up @@ -271,14 +264,19 @@ export class AutoDial implements Startable {
}

this.running = false
this.sheduleNextAutodial()
}

if (this.started) {
this.autoDialInterval = setTimeout(() => {
this.autoDial()
.catch(err => {
this.log.error('error while autodialing', err)
})
}, this.autoDialIntervalMs)
private sheduleNextAutodial (): void {
if (!this.started) {
return
}

this.autoDialInterval = setTimeout(() => {
this.autoDial()
.catch(err => {
this.log.error('error while autodialing', err)
})
}, this.autoDialIntervalMs)
}
}

0 comments on commit bcfa159

Please sign in to comment.