@libp2p/kad-dht 8.0.9 → 8.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- import { TimeoutController } from 'timeout-abort-controller'
2
1
  import { anySignal } from 'any-signal'
3
2
  import {
4
3
  ALPHA, K, DEFAULT_QUERY_TIMEOUT
@@ -39,7 +38,7 @@ export class QueryManager implements Startable {
39
38
  private readonly lan: boolean
40
39
  public disjointPaths: number
41
40
  private readonly alpha: number
42
- private readonly controllers: Set<AbortController>
41
+ private readonly shutDownController: AbortController
43
42
  private running: boolean
44
43
  private queries: number
45
44
  private metrics?: {
@@ -52,11 +51,19 @@ export class QueryManager implements Startable {
52
51
 
53
52
  this.components = components
54
53
  this.disjointPaths = disjointPaths ?? K
55
- this.controllers = new Set()
56
54
  this.running = false
57
55
  this.alpha = alpha ?? ALPHA
58
56
  this.lan = lan
59
57
  this.queries = 0
58
+
59
+ // allow us to stop queries on shut down
60
+ this.shutDownController = new AbortController()
61
+ // make sure we don't make a lot of noise in the logs
62
+ try {
63
+ if (setMaxListeners != null) {
64
+ setMaxListeners(Infinity, this.shutDownController.signal)
65
+ }
66
+ } catch {} // fails on node < 15.4
60
67
  }
61
68
 
62
69
  isStarted (): boolean {
@@ -83,11 +90,7 @@ export class QueryManager implements Startable {
83
90
  async stop (): Promise<void> {
84
91
  this.running = false
85
92
 
86
- for (const controller of this.controllers) {
87
- controller.abort()
88
- }
89
-
90
- this.controllers.clear()
93
+ this.shutDownController.abort()
91
94
  }
92
95
 
93
96
  async * run (key: Uint8Array, peers: PeerId[], queryFunc: QueryFunc, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
@@ -96,32 +99,21 @@ export class QueryManager implements Startable {
96
99
  }
97
100
 
98
101
  const stopQueryTimer = this.metrics?.queryTime.timer()
99
- let timeoutController
100
102
 
101
103
  if (options.signal == null) {
102
104
  // don't let queries run forever
103
- timeoutController = new TimeoutController(DEFAULT_QUERY_TIMEOUT)
104
- options.signal = timeoutController.signal
105
+ options.signal = AbortSignal.timeout(DEFAULT_QUERY_TIMEOUT)
105
106
 
106
107
  // this signal will get listened to for network requests, etc
107
108
  // so make sure we don't make a lot of noise in the logs
108
109
  try {
109
110
  if (setMaxListeners != null) {
110
- setMaxListeners(Infinity, timeoutController.signal)
111
+ setMaxListeners(Infinity, options.signal)
111
112
  }
112
113
  } catch {} // fails on node < 15.4
113
114
  }
114
115
 
115
- // allow us to stop queries on shut down
116
- const abortController = new AbortController()
117
- this.controllers.add(abortController)
118
- const signals = [abortController.signal]
119
-
120
- if (options.signal != null) {
121
- signals.push(options.signal)
122
- }
123
-
124
- const signal = anySignal(signals)
116
+ const signal = anySignal([this.shutDownController.signal, options.signal])
125
117
 
126
118
  // this signal will get listened to for every invocation of queryFunc
127
119
  // so make sure we don't make a lot of noise in the logs
@@ -186,12 +178,6 @@ export class QueryManager implements Startable {
186
178
  } finally {
187
179
  signal.clear()
188
180
 
189
- this.controllers.delete(abortController)
190
-
191
- if (timeoutController != null) {
192
- timeoutController.clear()
193
- }
194
-
195
181
  this.queries--
196
182
  this.metrics?.runningQueries.update(this.queries)
197
183
 
@@ -4,7 +4,6 @@ import { toString } from 'uint8arrays/to-string'
4
4
  import defer from 'p-defer'
5
5
  import { CodeError } from '@libp2p/interfaces/errors'
6
6
  import { convertPeerId, convertBuffer } from '../utils.js'
7
- import { TimeoutController } from 'timeout-abort-controller'
8
7
  import { anySignal } from 'any-signal'
9
8
  import type { PeerId } from '@libp2p/interface-peer-id'
10
9
  import type { EventEmitter } from '@libp2p/interfaces/events'
@@ -108,12 +107,10 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
108
107
  const peerXor = BigInt('0x' + toString(xor(peerKadId, kadId), 'base16'))
109
108
 
110
109
  queue.add(async () => {
111
- let timeout
112
110
  const signals = [signal]
113
111
 
114
112
  if (queryFuncTimeout != null) {
115
- timeout = new TimeoutController(queryFuncTimeout)
116
- signals.push(timeout.signal)
113
+ signals.push(AbortSignal.timeout(queryFuncTimeout))
117
114
  }
118
115
 
119
116
  const compoundSignal = anySignal(signals)
@@ -158,8 +155,6 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
158
155
  }
159
156
  queue.emit('completed', event)
160
157
  }
161
-
162
- timeout?.clear()
163
158
  } catch (err: any) {
164
159
  if (!signal.aborted) {
165
160
  return queryErrorEvent({
@@ -169,7 +164,6 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
169
164
  }
170
165
  } finally {
171
166
  compoundSignal.clear()
172
- timeout?.clear()
173
167
  }
174
168
  }, {
175
169
  // use xor value as the queue priority - closer peers should execute first
package/src/query-self.ts CHANGED
@@ -2,7 +2,6 @@ import { setMaxListeners } from 'events'
2
2
  import take from 'it-take'
3
3
  import length from 'it-length'
4
4
  import { QUERY_SELF_INTERVAL, QUERY_SELF_TIMEOUT, K } from './constants.js'
5
- import { TimeoutController } from 'timeout-abort-controller'
6
5
  import { anySignal } from 'any-signal'
7
6
  import { logger, Logger } from '@libp2p/logger'
8
7
  import type { PeerRouting } from './peer-routing/index.js'
@@ -71,9 +70,8 @@ export class QuerySelf implements Startable {
71
70
 
72
71
  _querySelf (): void {
73
72
  Promise.resolve().then(async () => {
74
- const timeoutController = new TimeoutController(this.queryTimeout)
75
73
  this.controller = new AbortController()
76
- const signal = anySignal([this.controller.signal, timeoutController.signal])
74
+ const signal = anySignal([this.controller.signal, AbortSignal.timeout(this.queryTimeout)])
77
75
 
78
76
  // this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
79
77
  try {
@@ -96,7 +94,6 @@ export class QuerySelf implements Startable {
96
94
  this.log('query error', err)
97
95
  } finally {
98
96
  this.timeoutId = setTimeout(this._querySelf.bind(this), this.interval)
99
- timeoutController.clear()
100
97
  signal.clear()
101
98
  }
102
99
  }).catch(err => {
@@ -2,7 +2,6 @@
2
2
  import KBuck from 'k-bucket'
3
3
  import * as utils from '../utils.js'
4
4
  import Queue from 'p-queue'
5
- import { TimeoutController } from 'timeout-abort-controller'
6
5
  import { logger } from '@libp2p/logger'
7
6
  import type { PeerId } from '@libp2p/interface-peer-id'
8
7
  import type { Startable } from '@libp2p/interfaces/startable'
@@ -222,13 +221,9 @@ export class RoutingTable implements Startable {
222
221
  try {
223
222
  await Promise.all(
224
223
  oldContacts.map(async oldContact => {
225
- let timeoutController
226
-
227
224
  try {
228
- timeoutController = new TimeoutController(this.pingTimeout)
229
-
230
225
  const options = {
231
- signal: timeoutController.signal
226
+ signal: AbortSignal.timeout(this.pingTimeout)
232
227
  }
233
228
 
234
229
  this.log('pinging old contact %p', oldContact.peer)
@@ -245,10 +240,6 @@ export class RoutingTable implements Startable {
245
240
  this.kb.remove(oldContact.id)
246
241
  }
247
242
  } finally {
248
- if (timeoutController != null) {
249
- timeoutController.clear()
250
- }
251
-
252
243
  this.metrics?.routingTableSize.update(this.size)
253
244
  }
254
245
  })
@@ -5,7 +5,6 @@ import { randomBytes } from '@libp2p/crypto'
5
5
  import { peerIdFromBytes } from '@libp2p/peer-id'
6
6
  import { logger } from '@libp2p/logger'
7
7
  import length from 'it-length'
8
- import { TimeoutController } from 'timeout-abort-controller'
9
8
  import { TABLE_REFRESH_INTERVAL, TABLE_REFRESH_QUERY_TIMEOUT } from '../constants.js'
10
9
  import type { RoutingTable } from './index.js'
11
10
  import type { Logger } from '@libp2p/logger'
@@ -135,16 +134,10 @@ export class RoutingTableRefresh {
135
134
 
136
135
  this.log('starting refreshing cpl %s with key %p (routing table size was %s)', cpl, peerId, this.routingTable.size)
137
136
 
138
- const controller = new TimeoutController(this.refreshQueryTimeout)
137
+ const peers = await length(this.peerRouting.getClosestPeers(peerId.toBytes(), { signal: AbortSignal.timeout(this.refreshQueryTimeout) }))
139
138
 
140
- try {
141
- const peers = await length(this.peerRouting.getClosestPeers(peerId.toBytes(), { signal: controller.signal }))
142
-
143
- this.log(`found ${peers} peers that were close to imaginary peer %p`, peerId)
144
- this.log('finished refreshing cpl %s with key %p (routing table size is now %s)', cpl, peerId, this.routingTable.size)
145
- } finally {
146
- controller.clear()
147
- }
139
+ this.log(`found ${peers} peers that were close to imaginary peer %p`, peerId)
140
+ this.log('finished refreshing cpl %s with key %p (routing table size is now %s)', cpl, peerId, this.routingTable.size)
148
141
  }
149
142
 
150
143
  _getTrackedCommonPrefixLengthsForRefresh (maxCommonPrefix: number): Date[] {