Skip to content

Commit bcfa159

Browse files
authored
fix: remove duplicate autodial from startup (#2289)
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.
1 parent 10ea197 commit bcfa159

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,6 @@ export class AutoDial implements Startable {
101101
}
102102

103103
start (): void {
104-
this.autoDialInterval = setTimeout(() => {
105-
this.autoDial()
106-
.catch(err => {
107-
this.log.error('error while autodialing', err)
108-
})
109-
}, this.autoDialIntervalMs)
110104
this.started = true
111105
}
112106

@@ -126,28 +120,27 @@ export class AutoDial implements Startable {
126120
}
127121

128122
async autoDial (): Promise<void> {
129-
if (!this.started) {
123+
if (!this.started || this.running) {
130124
return
131125
}
132126

133127
const connections = this.connectionManager.getConnectionsMap()
134128
const numConnections = connections.size
135129

136-
// Already has enough connections
130+
// already have enough connections
137131
if (numConnections >= this.minConnections) {
138132
if (this.minConnections > 0) {
139133
this.log.trace('have enough connections %d/%d', numConnections, this.minConnections)
140134
}
135+
136+
// no need to schedule next autodial as it will be run when on
137+
// connection:close event
141138
return
142139
}
143140

144141
if (this.queue.size > this.autoDialMaxQueueLength) {
145142
this.log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
146-
return
147-
}
148-
149-
if (this.running) {
150-
this.log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
143+
this.sheduleNextAutodial()
151144
return
152145
}
153146

@@ -162,12 +155,12 @@ export class AutoDial implements Startable {
162155
.filter(Boolean)
163156
)
164157

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

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

273266
this.running = false
267+
this.sheduleNextAutodial()
268+
}
274269

275-
if (this.started) {
276-
this.autoDialInterval = setTimeout(() => {
277-
this.autoDial()
278-
.catch(err => {
279-
this.log.error('error while autodialing', err)
280-
})
281-
}, this.autoDialIntervalMs)
270+
private sheduleNextAutodial (): void {
271+
if (!this.started) {
272+
return
282273
}
274+
275+
this.autoDialInterval = setTimeout(() => {
276+
this.autoDial()
277+
.catch(err => {
278+
this.log.error('error while autodialing', err)
279+
})
280+
}, this.autoDialIntervalMs)
283281
}
284282
}

0 commit comments

Comments
 (0)