Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove duplicate autodial from startup #2289

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

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 @@
}

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 hava enough connections
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
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()

Check warning on line 143 in packages/libp2p/src/connection-manager/auto-dial.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/connection-manager/auto-dial.ts#L143

Added line #L143 was not covered by tests
return
}

Expand All @@ -162,12 +155,12 @@
.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 @@
// 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 @@
}

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

Check warning on line 272 in packages/libp2p/src/connection-manager/auto-dial.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/connection-manager/auto-dial.ts#L272

Added line #L272 was not covered by tests
}

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

Check warning on line 278 in packages/libp2p/src/connection-manager/auto-dial.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/connection-manager/auto-dial.ts#L278

Added line #L278 was not covered by tests
})
}, this.autoDialIntervalMs)
}
}
Loading