Skip to content

Commit 7f3245e

Browse files
authored
fix: remove timeout-abort-controller dependency (libp2p#454)
1 parent 08dc6f1 commit 7f3245e

File tree

6 files changed

+20
-60
lines changed

6 files changed

+20
-60
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@
182182
"p-queue": "^7.3.4",
183183
"private-ip": "^3.0.0",
184184
"protons-runtime": "^5.0.0",
185-
"timeout-abort-controller": "^3.0.0",
186185
"uint8arraylist": "^2.0.0",
187186
"uint8arrays": "^4.0.2",
188187
"varint": "^6.0.0"

src/query-self.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { setMaxListeners } from 'events'
22
import take from 'it-take'
33
import length from 'it-length'
44
import { QUERY_SELF_INTERVAL, QUERY_SELF_TIMEOUT, K } from './constants.js'
5-
import { TimeoutController } from 'timeout-abort-controller'
65
import { anySignal } from 'any-signal'
76
import { logger, Logger } from '@libp2p/logger'
87
import type { PeerRouting } from './peer-routing/index.js'
@@ -71,9 +70,8 @@ export class QuerySelf implements Startable {
7170

7271
_querySelf (): void {
7372
Promise.resolve().then(async () => {
74-
const timeoutController = new TimeoutController(this.queryTimeout)
7573
this.controller = new AbortController()
76-
const signal = anySignal([this.controller.signal, timeoutController.signal])
74+
const signal = anySignal([this.controller.signal, AbortSignal.timeout(this.queryTimeout)])
7775

7876
// this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
7977
try {
@@ -96,7 +94,6 @@ export class QuerySelf implements Startable {
9694
this.log('query error', err)
9795
} finally {
9896
this.timeoutId = setTimeout(this._querySelf.bind(this), this.interval)
99-
timeoutController.clear()
10097
signal.clear()
10198
}
10299
}).catch(err => {

src/query/manager.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { TimeoutController } from 'timeout-abort-controller'
21
import { anySignal } from 'any-signal'
32
import {
43
ALPHA, K, DEFAULT_QUERY_TIMEOUT
@@ -39,7 +38,7 @@ export class QueryManager implements Startable {
3938
private readonly lan: boolean
4039
public disjointPaths: number
4140
private readonly alpha: number
42-
private readonly controllers: Set<AbortController>
41+
private readonly shutDownController: AbortController
4342
private running: boolean
4443
private queries: number
4544
private metrics?: {
@@ -52,11 +51,19 @@ export class QueryManager implements Startable {
5251

5352
this.components = components
5453
this.disjointPaths = disjointPaths ?? K
55-
this.controllers = new Set()
5654
this.running = false
5755
this.alpha = alpha ?? ALPHA
5856
this.lan = lan
5957
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
6067
}
6168

6269
isStarted (): boolean {
@@ -83,11 +90,7 @@ export class QueryManager implements Startable {
8390
async stop (): Promise<void> {
8491
this.running = false
8592

86-
for (const controller of this.controllers) {
87-
controller.abort()
88-
}
89-
90-
this.controllers.clear()
93+
this.shutDownController.abort()
9194
}
9295

9396
async * run (key: Uint8Array, peers: PeerId[], queryFunc: QueryFunc, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
@@ -96,32 +99,21 @@ export class QueryManager implements Startable {
9699
}
97100

98101
const stopQueryTimer = this.metrics?.queryTime.timer()
99-
let timeoutController
100102

101103
if (options.signal == null) {
102104
// 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)
105106

106107
// this signal will get listened to for network requests, etc
107108
// so make sure we don't make a lot of noise in the logs
108109
try {
109110
if (setMaxListeners != null) {
110-
setMaxListeners(Infinity, timeoutController.signal)
111+
setMaxListeners(Infinity, options.signal)
111112
}
112113
} catch {} // fails on node < 15.4
113114
}
114115

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])
125117

126118
// this signal will get listened to for every invocation of queryFunc
127119
// so make sure we don't make a lot of noise in the logs
@@ -186,12 +178,6 @@ export class QueryManager implements Startable {
186178
} finally {
187179
signal.clear()
188180

189-
this.controllers.delete(abortController)
190-
191-
if (timeoutController != null) {
192-
timeoutController.clear()
193-
}
194-
195181
this.queries--
196182
this.metrics?.runningQueries.update(this.queries)
197183

src/query/query-path.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { toString } from 'uint8arrays/to-string'
44
import defer from 'p-defer'
55
import { CodeError } from '@libp2p/interfaces/errors'
66
import { convertPeerId, convertBuffer } from '../utils.js'
7-
import { TimeoutController } from 'timeout-abort-controller'
87
import { anySignal } from 'any-signal'
98
import type { PeerId } from '@libp2p/interface-peer-id'
109
import type { EventEmitter } from '@libp2p/interfaces/events'
@@ -108,12 +107,10 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
108107
const peerXor = BigInt('0x' + toString(xor(peerKadId, kadId), 'base16'))
109108

110109
queue.add(async () => {
111-
let timeout
112110
const signals = [signal]
113111

114112
if (queryFuncTimeout != null) {
115-
timeout = new TimeoutController(queryFuncTimeout)
116-
signals.push(timeout.signal)
113+
signals.push(AbortSignal.timeout(queryFuncTimeout))
117114
}
118115

119116
const compoundSignal = anySignal(signals)
@@ -158,8 +155,6 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
158155
}
159156
queue.emit('completed', event)
160157
}
161-
162-
timeout?.clear()
163158
} catch (err: any) {
164159
if (!signal.aborted) {
165160
return queryErrorEvent({
@@ -169,7 +164,6 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
169164
}
170165
} finally {
171166
compoundSignal.clear()
172-
timeout?.clear()
173167
}
174168
}, {
175169
// use xor value as the queue priority - closer peers should execute first

src/routing-table/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import KBuck from 'k-bucket'
33
import * as utils from '../utils.js'
44
import Queue from 'p-queue'
5-
import { TimeoutController } from 'timeout-abort-controller'
65
import { logger } from '@libp2p/logger'
76
import type { PeerId } from '@libp2p/interface-peer-id'
87
import type { Startable } from '@libp2p/interfaces/startable'
@@ -222,13 +221,9 @@ export class RoutingTable implements Startable {
222221
try {
223222
await Promise.all(
224223
oldContacts.map(async oldContact => {
225-
let timeoutController
226-
227224
try {
228-
timeoutController = new TimeoutController(this.pingTimeout)
229-
230225
const options = {
231-
signal: timeoutController.signal
226+
signal: AbortSignal.timeout(this.pingTimeout)
232227
}
233228

234229
this.log('pinging old contact %p', oldContact.peer)
@@ -245,10 +240,6 @@ export class RoutingTable implements Startable {
245240
this.kb.remove(oldContact.id)
246241
}
247242
} finally {
248-
if (timeoutController != null) {
249-
timeoutController.clear()
250-
}
251-
252243
this.metrics?.routingTableSize.update(this.size)
253244
}
254245
})

src/routing-table/refresh.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { randomBytes } from '@libp2p/crypto'
55
import { peerIdFromBytes } from '@libp2p/peer-id'
66
import { logger } from '@libp2p/logger'
77
import length from 'it-length'
8-
import { TimeoutController } from 'timeout-abort-controller'
98
import { TABLE_REFRESH_INTERVAL, TABLE_REFRESH_QUERY_TIMEOUT } from '../constants.js'
109
import type { RoutingTable } from './index.js'
1110
import type { Logger } from '@libp2p/logger'
@@ -135,16 +134,10 @@ export class RoutingTableRefresh {
135134

136135
this.log('starting refreshing cpl %s with key %p (routing table size was %s)', cpl, peerId, this.routingTable.size)
137136

138-
const controller = new TimeoutController(this.refreshQueryTimeout)
137+
const peers = await length(this.peerRouting.getClosestPeers(peerId.toBytes(), { signal: AbortSignal.timeout(this.refreshQueryTimeout) }))
139138

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)
148141
}
149142

150143
_getTrackedCommonPrefixLengthsForRefresh (maxCommonPrefix: number): Date[] {

0 commit comments

Comments
 (0)