@@ -12,8 +12,11 @@ const { TimeoutController } = require('timeout-abort-controller')
1212 * @typedef {import('./types').KBucket } KBucket
1313 * @typedef {import('./types').KBucketTree } KBucketTree
1414 * @typedef {import('peer-id') } PeerId
15+ * @typedef {import('../types').Metrics } Metrics
1516 */
1617
18+ const METRIC_ROUTING_TABLE_SIZE = 'routing-table-size'
19+
1720/**
1821 * A wrapper around `k-bucket`, to provide easy store and
1922 * retrieval for peers.
@@ -24,15 +27,18 @@ class RoutingTable {
2427 * @param {import('peer-id') } params.peerId
2528 * @param {import('../types').Dialer } params.dialer
2629 * @param {boolean } params.lan
30+ * @param {Metrics } [params.metrics]
2731 * @param {number } [params.kBucketSize=20]
2832 * @param {number } [params.pingTimeout=10000]
2933 */
30- constructor ( { peerId, dialer, kBucketSize, pingTimeout, lan } ) {
34+ constructor ( { peerId, dialer, kBucketSize, pingTimeout, lan, metrics } ) {
3135 this . _log = utils . logger ( `libp2p:kad-dht:${ lan ? 'lan' : 'wan' } :routing-table` )
3236 this . _peerId = peerId
3337 this . _dialer = dialer
3438 this . _kBucketSize = kBucketSize || 20
3539 this . _pingTimeout = pingTimeout || 10000
40+ this . _lan = lan
41+ this . _metrics = metrics
3642
3743 /** @type {KBucketTree } */
3844 this . kb // eslint-disable-line no-unused-expressions
@@ -111,6 +117,8 @@ class RoutingTable {
111117 if ( timeoutController ) {
112118 timeoutController . clear ( )
113119 }
120+
121+ this . _metrics && this . _metrics . updateComponentMetric ( `kad-dht-${ this . _lan ? 'lan' : 'wan' } ` , METRIC_ROUTING_TABLE_SIZE , this . size )
114122 }
115123 } )
116124 )
@@ -184,6 +192,8 @@ class RoutingTable {
184192 this . kb . add ( { id : id , peer : peer } )
185193
186194 this . _log ( 'added %p with kad id %b' , peer , id )
195+
196+ this . _metrics && this . _metrics . updateComponentMetric ( `kad-dht-${ this . _lan ? 'lan' : 'wan' } ` , METRIC_ROUTING_TABLE_SIZE , this . size )
187197 }
188198
189199 /**
@@ -195,6 +205,8 @@ class RoutingTable {
195205 const id = await utils . convertPeerId ( peer )
196206
197207 this . kb . remove ( id )
208+
209+ this . _metrics && this . _metrics . updateComponentMetric ( `kad-dht-${ this . _lan ? 'lan' : 'wan' } ` , METRIC_ROUTING_TABLE_SIZE , this . size )
198210 }
199211}
200212
0 commit comments