1
- import { logger } from '@libp2p/logger'
2
1
import { PeerMap , PeerSet } from '@libp2p/peer-collections'
3
2
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
3
import { PeerJobQueue } from '../utils/peer-job-queue.js'
5
4
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
- import type { Libp2pEvents } from '@libp2p/interface'
5
+ import type { Libp2pEvents , Logger , ComponentLogger } from '@libp2p/interface'
7
6
import type { TypedEventTarget } from '@libp2p/interface/events'
8
7
import type { PeerStore } from '@libp2p/interface/peer-store'
9
8
import type { Startable } from '@libp2p/interface/startable'
10
9
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
11
10
12
- const log = logger ( 'libp2p:connection-manager:auto-dial' )
13
-
14
11
interface AutoDialInit {
15
12
minConnections ?: number
16
13
maxQueueLength ?: number
@@ -25,6 +22,7 @@ interface AutoDialComponents {
25
22
connectionManager : ConnectionManager
26
23
peerStore : PeerStore
27
24
events : TypedEventTarget < Libp2pEvents >
25
+ logger : ComponentLogger
28
26
}
29
27
30
28
const defaultOptions = {
@@ -50,6 +48,7 @@ export class AutoDial implements Startable {
50
48
private autoDialInterval ?: ReturnType < typeof setInterval >
51
49
private started : boolean
52
50
private running : boolean
51
+ readonly #log: Logger
53
52
54
53
/**
55
54
* Proactively tries to connect to known peers stored in the PeerStore.
@@ -65,20 +64,21 @@ export class AutoDial implements Startable {
65
64
this . autoDialMaxQueueLength = init . maxQueueLength ?? defaultOptions . maxQueueLength
66
65
this . autoDialPeerRetryThresholdMs = init . autoDialPeerRetryThreshold ?? defaultOptions . autoDialPeerRetryThreshold
67
66
this . autoDialDiscoveredPeersDebounce = init . autoDialDiscoveredPeersDebounce ?? defaultOptions . autoDialDiscoveredPeersDebounce
67
+ this . #log = components . logger . forComponent ( 'libp2p:connection-manager:auto-dial' )
68
68
this . started = false
69
69
this . running = false
70
70
this . queue = new PeerJobQueue ( {
71
71
concurrency : init . autoDialConcurrency ?? defaultOptions . autoDialConcurrency
72
72
} )
73
73
this . queue . addListener ( 'error' , ( err ) => {
74
- log . error ( 'error during auto-dial' , err )
74
+ this . # log. error ( 'error during auto-dial' , err )
75
75
} )
76
76
77
77
// check the min connection limit whenever a peer disconnects
78
78
components . events . addEventListener ( 'connection:close' , ( ) => {
79
79
this . autoDial ( )
80
80
. catch ( err => {
81
- log . error ( err )
81
+ this . # log. error ( err )
82
82
} )
83
83
} )
84
84
@@ -93,7 +93,7 @@ export class AutoDial implements Startable {
93
93
debounce = setTimeout ( ( ) => {
94
94
this . autoDial ( )
95
95
. catch ( err => {
96
- log . error ( err )
96
+ this . # log. error ( err )
97
97
} )
98
98
} , this . autoDialDiscoveredPeersDebounce )
99
99
} )
@@ -107,7 +107,7 @@ export class AutoDial implements Startable {
107
107
this . autoDialInterval = setTimeout ( ( ) => {
108
108
this . autoDial ( )
109
109
. catch ( err => {
110
- log . error ( 'error while autodialing' , err )
110
+ this . # log. error ( 'error while autodialing' , err )
111
111
} )
112
112
} , this . autoDialIntervalMs )
113
113
this . started = true
@@ -116,7 +116,7 @@ export class AutoDial implements Startable {
116
116
afterStart ( ) : void {
117
117
this . autoDial ( )
118
118
. catch ( err => {
119
- log . error ( 'error while autodialing' , err )
119
+ this . # log. error ( 'error while autodialing' , err )
120
120
} )
121
121
}
122
122
@@ -139,24 +139,24 @@ export class AutoDial implements Startable {
139
139
// Already has enough connections
140
140
if ( numConnections >= this . minConnections ) {
141
141
if ( this . minConnections > 0 ) {
142
- log . trace ( 'have enough connections %d/%d' , numConnections , this . minConnections )
142
+ this . # log. trace ( 'have enough connections %d/%d' , numConnections , this . minConnections )
143
143
}
144
144
return
145
145
}
146
146
147
147
if ( this . queue . size > this . autoDialMaxQueueLength ) {
148
- log ( 'not enough connections %d/%d but auto dial queue is full' , numConnections , this . minConnections )
148
+ this . # log( 'not enough connections %d/%d but auto dial queue is full' , numConnections , this . minConnections )
149
149
return
150
150
}
151
151
152
152
if ( this . running ) {
153
- log ( 'not enough connections %d/%d - but skipping autodial as it is already running' , numConnections , this . minConnections )
153
+ this . # log( 'not enough connections %d/%d - but skipping autodial as it is already running' , numConnections , this . minConnections )
154
154
return
155
155
}
156
156
157
157
this . running = true
158
158
159
- log ( 'not enough connections %d/%d - will dial peers to increase the number of connections' , numConnections , this . minConnections )
159
+ this . # log( 'not enough connections %d/%d - will dial peers to increase the number of connections' , numConnections , this . minConnections )
160
160
161
161
const dialQueue = new PeerSet (
162
162
// @ts -expect-error boolean filter removes falsy peer IDs
@@ -172,25 +172,25 @@ export class AutoDial implements Startable {
172
172
( peer ) => {
173
173
// Remove peers without addresses
174
174
if ( peer . addresses . length === 0 ) {
175
- log . trace ( 'not autodialing %p because they have no addresses' , peer . id )
175
+ this . # log. trace ( 'not autodialing %p because they have no addresses' , peer . id )
176
176
return false
177
177
}
178
178
179
179
// remove peers we are already connected to
180
180
if ( connections . has ( peer . id ) ) {
181
- log . trace ( 'not autodialing %p because they are already connected' , peer . id )
181
+ this . # log. trace ( 'not autodialing %p because they are already connected' , peer . id )
182
182
return false
183
183
}
184
184
185
185
// remove peers we are already dialling
186
186
if ( dialQueue . has ( peer . id ) ) {
187
- log . trace ( 'not autodialing %p because they are already being dialed' , peer . id )
187
+ this . # log. trace ( 'not autodialing %p because they are already being dialed' , peer . id )
188
188
return false
189
189
}
190
190
191
191
// remove peers already in the autodial queue
192
192
if ( this . queue . hasJob ( peer . id ) ) {
193
- log . trace ( 'not autodialing %p because they are already being autodialed' , peer . id )
193
+ this . # log. trace ( 'not autodialing %p because they are already being autodialed' , peer . id )
194
194
return false
195
195
}
196
196
@@ -249,27 +249,27 @@ export class AutoDial implements Startable {
249
249
return Date . now ( ) - lastDialFailureTimestamp > this . autoDialPeerRetryThresholdMs
250
250
} )
251
251
252
- log ( 'selected %d/%d peers to dial' , peersThatHaveNotFailed . length , peers . length )
252
+ this . # log( 'selected %d/%d peers to dial' , peersThatHaveNotFailed . length , peers . length )
253
253
254
254
for ( const peer of peersThatHaveNotFailed ) {
255
255
this . queue . add ( async ( ) => {
256
256
const numConnections = this . connectionManager . getConnectionsMap ( ) . size
257
257
258
258
// Check to see if we still need to auto dial
259
259
if ( numConnections >= this . minConnections ) {
260
- log ( 'got enough connections now %d/%d' , numConnections , this . minConnections )
260
+ this . # log( 'got enough connections now %d/%d' , numConnections , this . minConnections )
261
261
this . queue . clear ( )
262
262
return
263
263
}
264
264
265
- log ( 'connecting to a peerStore stored peer %p' , peer . id )
265
+ this . # log( 'connecting to a peerStore stored peer %p' , peer . id )
266
266
await this . connectionManager . openConnection ( peer . id , {
267
267
priority : this . autoDialPriority
268
268
} )
269
269
} , {
270
270
peerId : peer . id
271
271
} ) . catch ( err => {
272
- log . error ( 'could not connect to peerStore stored peer' , err )
272
+ this . # log. error ( 'could not connect to peerStore stored peer' , err )
273
273
} )
274
274
}
275
275
@@ -279,7 +279,7 @@ export class AutoDial implements Startable {
279
279
this . autoDialInterval = setTimeout ( ( ) => {
280
280
this . autoDial ( )
281
281
. catch ( err => {
282
- log . error ( 'error while autodialing' , err )
282
+ this . # log. error ( 'error while autodialing' , err )
283
283
} )
284
284
} , this . autoDialIntervalMs )
285
285
}
0 commit comments