1
1
'use strict'
2
2
3
3
const debug = require ( 'debug' )
4
- const log = debug ( 'libp2p:connection-manager' )
5
- log . error = debug ( 'libp2p:connection-manager:error' )
4
+ const log = Object . assign ( debug ( 'libp2p:connection-manager' ) , {
5
+ error : debug ( 'libp2p:connection-manager:err' )
6
+ } )
6
7
7
8
const errcode = require ( 'err-code' )
8
9
const mergeOptions = require ( 'merge-options' )
@@ -14,7 +15,7 @@ const { EventEmitter } = require('events')
14
15
const PeerId = require ( 'peer-id' )
15
16
16
17
const {
17
- ERR_INVALID_PARAMETERS
18
+ codes : { ERR_INVALID_PARAMETERS }
18
19
} = require ( '../errors' )
19
20
20
21
const defaultOptions = {
@@ -187,15 +188,17 @@ class ConnectionManager extends EventEmitter {
187
188
* @private
188
189
*/
189
190
_checkMetrics ( ) {
190
- const movingAverages = this . _libp2p . metrics . global . movingAverages
191
- const received = movingAverages . dataReceived [ this . _options . movingAverageInterval ] . movingAverage ( )
192
- this . _checkMaxLimit ( 'maxReceivedData' , received )
193
- const sent = movingAverages . dataSent [ this . _options . movingAverageInterval ] . movingAverage ( )
194
- this . _checkMaxLimit ( 'maxSentData' , sent )
195
- const total = received + sent
196
- this . _checkMaxLimit ( 'maxData' , total )
197
- log ( 'metrics update' , total )
198
- this . _timer = retimer ( this . _checkMetrics , this . _options . pollInterval )
191
+ if ( this . _libp2p . metrics ) {
192
+ const movingAverages = this . _libp2p . metrics . global . movingAverages
193
+ const received = movingAverages . dataReceived [ this . _options . movingAverageInterval ] . movingAverage ( )
194
+ this . _checkMaxLimit ( 'maxReceivedData' , received )
195
+ const sent = movingAverages . dataSent [ this . _options . movingAverageInterval ] . movingAverage ( )
196
+ this . _checkMaxLimit ( 'maxSentData' , sent )
197
+ const total = received + sent
198
+ this . _checkMaxLimit ( 'maxData' , total )
199
+ log ( 'metrics update' , total )
200
+ this . _timer = retimer ( this . _checkMetrics , this . _options . pollInterval )
201
+ }
199
202
}
200
203
201
204
/**
@@ -249,7 +252,7 @@ class ConnectionManager extends EventEmitter {
249
252
* Get a connection with a peer.
250
253
*
251
254
* @param {PeerId } peerId
252
- * @returns {Connection }
255
+ * @returns {Connection|null }
253
256
*/
254
257
get ( peerId ) {
255
258
const connections = this . getAll ( peerId )
0 commit comments