Skip to content

Commit 3c8dd5b

Browse files
joeltgachingbrain
andauthored
fix: accept custom ping protocol prefix in connection monitor (#2667)
The new connection monitor currently uses the hard-coded default ping protocol `'/ipfs/ping/1.0.0'`. The ping service lets the developer override the `ipfs` component with a custom protocol prefix, and some apps are already doing this. This PR adds support for a `protocolPrefix` option in `ConnectionMonitorInit` that matches the option in `PingServiceInit`. --------- Co-authored-by: Alex Potsides <[email protected]>
1 parent 34e0408 commit 3c8dd5b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/libp2p/src/connection-monitor.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import type { ConnectionManager } from '@libp2p/interface-internal'
66
import type { AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'
77

88
const DEFAULT_PING_INTERVAL_MS = 10000
9+
const PROTOCOL_VERSION = '1.0.0'
10+
const PROTOCOL_NAME = 'ping'
11+
const PROTOCOL_PREFIX = 'ipfs'
912

1013
export interface ConnectionMonitorInit {
1114
/**
@@ -37,6 +40,13 @@ export interface ConnectionMonitorInit {
3740
* @default true
3841
*/
3942
abortConnectionOnPingFailure?: boolean
43+
44+
/**
45+
* Override the ping protocol prefix
46+
*
47+
* @default 'ipfs'
48+
*/
49+
protocolPrefix?: string
4050
}
4151

4252
export interface ConnectionMonitorComponents {
@@ -46,6 +56,7 @@ export interface ConnectionMonitorComponents {
4656
}
4757

4858
export class ConnectionMonitor implements Startable {
59+
private readonly protocol: string
4960
private readonly components: ConnectionMonitorComponents
5061
private readonly log: Logger
5162
private heartbeatInterval?: ReturnType<typeof setInterval>
@@ -55,6 +66,7 @@ export class ConnectionMonitor implements Startable {
5566

5667
constructor (components: ConnectionMonitorComponents, init: ConnectionMonitorInit = {}) {
5768
this.components = components
69+
this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
5870

5971
this.log = components.logger.forComponent('libp2p:connection-monitor')
6072
this.pingIntervalMs = init.pingInterval ?? DEFAULT_PING_INTERVAL_MS
@@ -83,7 +95,7 @@ export class ConnectionMonitor implements Startable {
8395
const signal = this.timeout.getTimeoutSignal({
8496
signal: this.abortController?.signal
8597
})
86-
const stream = await conn.newStream('/ipfs/ping/1.0.0', {
98+
const stream = await conn.newStream(this.protocol, {
8799
signal,
88100
runOnTransientConnection: true
89101
})

packages/libp2p/test/connection-monitor/index.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ describe('connection monitor', () => {
5050
expect(connection.rtt).to.be.gte(0)
5151
})
5252

53+
it('should monitor the liveness of a connection with a custom ping protocol prefix', async () => {
54+
monitor = new ConnectionMonitor(components, {
55+
pingInterval: 10,
56+
protocolPrefix: 'foobar'
57+
})
58+
59+
await start(monitor)
60+
61+
const connection = stubInterface<Connection>()
62+
const stream = stubInterface<Stream>({
63+
...pair<any>()
64+
})
65+
connection.newStream.withArgs('/foobar/ping/1.0.0').resolves(stream)
66+
67+
components.connectionManager.getConnections.returns([connection])
68+
69+
await delay(100)
70+
71+
expect(connection.rtt).to.be.gte(0)
72+
})
73+
5374
it('should monitor the liveness of a connection that does not support ping', async () => {
5475
monitor = new ConnectionMonitor(components, {
5576
pingInterval: 10

0 commit comments

Comments
 (0)