Skip to content

Commit

Permalink
fix: accept custom ping protocol prefix in connection monitor (#2667)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
joeltg and achingbrain authored Aug 16, 2024
1 parent 34e0408 commit 3c8dd5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/libp2p/src/connection-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import type { ConnectionManager } from '@libp2p/interface-internal'
import type { AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'

const DEFAULT_PING_INTERVAL_MS = 10000
const PROTOCOL_VERSION = '1.0.0'
const PROTOCOL_NAME = 'ping'
const PROTOCOL_PREFIX = 'ipfs'

export interface ConnectionMonitorInit {
/**
Expand Down Expand Up @@ -37,6 +40,13 @@ export interface ConnectionMonitorInit {
* @default true
*/
abortConnectionOnPingFailure?: boolean

/**
* Override the ping protocol prefix
*
* @default 'ipfs'
*/
protocolPrefix?: string
}

export interface ConnectionMonitorComponents {
Expand All @@ -46,6 +56,7 @@ export interface ConnectionMonitorComponents {
}

export class ConnectionMonitor implements Startable {
private readonly protocol: string
private readonly components: ConnectionMonitorComponents
private readonly log: Logger
private heartbeatInterval?: ReturnType<typeof setInterval>
Expand All @@ -55,6 +66,7 @@ export class ConnectionMonitor implements Startable {

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

this.log = components.logger.forComponent('libp2p:connection-monitor')
this.pingIntervalMs = init.pingInterval ?? DEFAULT_PING_INTERVAL_MS
Expand Down Expand Up @@ -83,7 +95,7 @@ export class ConnectionMonitor implements Startable {
const signal = this.timeout.getTimeoutSignal({
signal: this.abortController?.signal
})
const stream = await conn.newStream('/ipfs/ping/1.0.0', {
const stream = await conn.newStream(this.protocol, {
signal,
runOnTransientConnection: true
})
Expand Down
21 changes: 21 additions & 0 deletions packages/libp2p/test/connection-monitor/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ describe('connection monitor', () => {
expect(connection.rtt).to.be.gte(0)
})

it('should monitor the liveness of a connection with a custom ping protocol prefix', async () => {
monitor = new ConnectionMonitor(components, {
pingInterval: 10,
protocolPrefix: 'foobar'
})

await start(monitor)

const connection = stubInterface<Connection>()
const stream = stubInterface<Stream>({
...pair<any>()
})
connection.newStream.withArgs('/foobar/ping/1.0.0').resolves(stream)

components.connectionManager.getConnections.returns([connection])

await delay(100)

expect(connection.rtt).to.be.gte(0)
})

it('should monitor the liveness of a connection that does not support ping', async () => {
monitor = new ConnectionMonitor(components, {
pingInterval: 10
Expand Down

0 comments on commit 3c8dd5b

Please sign in to comment.