@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
import { PeerMap , PeerSet } from '@libp2p/peer-collections'
3
3
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
4
import { PeerJobQueue } from '../utils/peer-job-queue.js'
5
- import { AUTO_DIAL_CONCURRENCY , AUTO_DIAL_INTERVAL , AUTO_DIAL_MAX_QUEUE_LENGTH , AUTO_DIAL_PEER_RETRY_THRESHOLD , AUTO_DIAL_PRIORITY , LAST_DIAL_FAILURE_KEY , MIN_CONNECTIONS } from './constants.js'
5
+ import { AUTO_DIAL_CONCURRENCY , AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE , AUTO_DIAL_INTERVAL , AUTO_DIAL_MAX_QUEUE_LENGTH , AUTO_DIAL_PEER_RETRY_THRESHOLD , AUTO_DIAL_PRIORITY , LAST_DIAL_FAILURE_KEY , MIN_CONNECTIONS } from './constants.js'
6
6
import type { Libp2pEvents } from '@libp2p/interface'
7
7
import type { EventEmitter } from '@libp2p/interface/events'
8
8
import type { PeerStore } from '@libp2p/interface/peer-store'
@@ -18,6 +18,7 @@ interface AutoDialInit {
18
18
autoDialPriority ?: number
19
19
autoDialInterval ?: number
20
20
autoDialPeerRetryThreshold ?: number
21
+ autoDialDiscoveredPeersDebounce ?: number
21
22
}
22
23
23
24
interface AutoDialComponents {
@@ -32,7 +33,8 @@ const defaultOptions = {
32
33
autoDialConcurrency : AUTO_DIAL_CONCURRENCY ,
33
34
autoDialPriority : AUTO_DIAL_PRIORITY ,
34
35
autoDialInterval : AUTO_DIAL_INTERVAL ,
35
- autoDialPeerRetryThreshold : AUTO_DIAL_PEER_RETRY_THRESHOLD
36
+ autoDialPeerRetryThreshold : AUTO_DIAL_PEER_RETRY_THRESHOLD ,
37
+ autoDialDiscoveredPeersDebounce : AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE
36
38
}
37
39
38
40
export class AutoDial implements Startable {
@@ -44,6 +46,7 @@ export class AutoDial implements Startable {
44
46
private readonly autoDialIntervalMs : number
45
47
private readonly autoDialMaxQueueLength : number
46
48
private readonly autoDialPeerRetryThresholdMs : number
49
+ private readonly autoDialDiscoveredPeersDebounce : number
47
50
private autoDialInterval ?: ReturnType < typeof setInterval >
48
51
private started : boolean
49
52
private running : boolean
@@ -61,6 +64,7 @@ export class AutoDial implements Startable {
61
64
this . autoDialIntervalMs = init . autoDialInterval ?? defaultOptions . autoDialInterval
62
65
this . autoDialMaxQueueLength = init . maxQueueLength ?? defaultOptions . maxQueueLength
63
66
this . autoDialPeerRetryThresholdMs = init . autoDialPeerRetryThreshold ?? defaultOptions . autoDialPeerRetryThreshold
67
+ this . autoDialDiscoveredPeersDebounce = init . autoDialDiscoveredPeersDebounce ?? defaultOptions . autoDialDiscoveredPeersDebounce
64
68
this . started = false
65
69
this . running = false
66
70
this . queue = new PeerJobQueue ( {
@@ -77,6 +81,22 @@ export class AutoDial implements Startable {
77
81
log . error ( err )
78
82
} )
79
83
} )
84
+
85
+ // sometimes peers are discovered in quick succession so add a small
86
+ // debounce to ensure all eligible peers are autodialed
87
+ let debounce : ReturnType < typeof setTimeout >
88
+
89
+ // when new peers are discovered, dial them if we don't have
90
+ // enough connections
91
+ components . events . addEventListener ( 'peer:discovery' , ( ) => {
92
+ clearTimeout ( debounce )
93
+ debounce = setTimeout ( ( ) => {
94
+ this . autoDial ( )
95
+ . catch ( err => {
96
+ log . error ( err )
97
+ } )
98
+ } , this . autoDialDiscoveredPeersDebounce )
99
+ } )
80
100
}
81
101
82
102
isStarted ( ) : boolean {
0 commit comments