-
Notifications
You must be signed in to change notification settings - Fork 479
feat(libp2p): add autodial retry threshold config option #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3626644
9b59ccc
dea67af
bcc23a3
295544a
8285e9e
e65724f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,13 +7,15 @@ import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers' | |||||
import { type ClearableSignal, anySignal } from 'any-signal' | ||||||
import pDefer from 'p-defer' | ||||||
import PQueue from 'p-queue' | ||||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||||||
import { codes } from '../errors.js' | ||||||
import { getPeerAddress } from '../get-peer.js' | ||||||
import { | ||||||
DIAL_TIMEOUT, | ||||||
MAX_PARALLEL_DIALS_PER_PEER, | ||||||
MAX_PARALLEL_DIALS, | ||||||
MAX_PEER_ADDRS_TO_DIAL | ||||||
MAX_PEER_ADDRS_TO_DIAL, | ||||||
LAST_DIAL_FAILURE_KEY | ||||||
} from './constants.js' | ||||||
import { combineSignals, resolveMultiaddrs } from './utils.js' | ||||||
import type { AddressSorter, AbortOptions, PendingDial } from '@libp2p/interface' | ||||||
|
@@ -230,9 +232,22 @@ export class DialQueue { | |||||
// clean up abort signals/controllers | ||||||
signal.clear() | ||||||
}) | ||||||
.catch(err => { | ||||||
.catch(async err => { | ||||||
log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err) | ||||||
|
||||||
if (peerId != null) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
to address manual dials? or do we want a specific dial option to flag whether we will update the LAST_DIAL_FAILURE_KEY for a peer that we only use with auto-dialer? |
||||||
// record the last failed dial | ||||||
try { | ||||||
await this.peerStore.patch(peerId, { | ||||||
maschad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
metadata: { | ||||||
[LAST_DIAL_FAILURE_KEY]: uint8ArrayFromString(Date.now().toString()) | ||||||
} | ||||||
}) | ||||||
} catch (err: any) { | ||||||
log.error('could not update last dial failure key for %p', peerId, err) | ||||||
} | ||||||
} | ||||||
|
||||||
// Error is a timeout | ||||||
if (signal.aborted) { | ||||||
const error = new CodeError(err.message, codes.ERR_TIMEOUT) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ import delay from 'delay' | |
import pWaitFor from 'p-wait-for' | ||
import Sinon from 'sinon' | ||
import { stubInterface } from 'sinon-ts' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { AutoDial } from '../../src/connection-manager/auto-dial.js' | ||
import { LAST_DIAL_FAILURE_KEY } from '../../src/connection-manager/constants.js' | ||
import { matchPeerId } from '../fixtures/match-peer-id.js' | ||
import type { Libp2pEvents } from '@libp2p/interface' | ||
import type { Connection } from '@libp2p/interface/connection' | ||
|
@@ -224,4 +226,69 @@ describe('auto-dial', () => { | |
// should only have queried peer store once | ||
expect(peerStoreAllSpy.callCount).to.equal(1) | ||
}) | ||
|
||
it('should not re-dial peers we have recently failed to dial', async () => { | ||
const peerWithAddress: Peer = { | ||
id: await createEd25519PeerId(), | ||
protocols: [], | ||
addresses: [{ | ||
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4001'), | ||
isCertified: true | ||
}], | ||
metadata: new Map(), | ||
tags: new Map() | ||
} | ||
const undialablePeer: Peer = { | ||
id: await createEd25519PeerId(), | ||
protocols: [], | ||
addresses: [{ | ||
multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4002'), | ||
isCertified: true | ||
}], | ||
// we failed to dial them recently | ||
metadata: new Map([[LAST_DIAL_FAILURE_KEY, uint8ArrayFromString(`${Date.now() - 10}`)]]), | ||
tags: new Map() | ||
} | ||
|
||
await peerStore.save(peerWithAddress.id, peerWithAddress) | ||
await peerStore.save(undialablePeer.id, undialablePeer) | ||
|
||
const connectionManager = stubInterface<ConnectionManager>({ | ||
getConnectionsMap: new PeerMap(), | ||
getDialQueue: [] | ||
}) | ||
|
||
autoDialler = new AutoDial({ | ||
peerStore, | ||
connectionManager, | ||
events | ||
}, { | ||
minConnections: 10, | ||
autoDialPeerRetryThreshold: 2000 | ||
}) | ||
autoDialler.start() | ||
|
||
void autoDialler.autoDial() | ||
|
||
await pWaitFor(() => { | ||
return connectionManager.openConnection.callCount === 1 | ||
}) | ||
|
||
expect(connectionManager.openConnection.callCount).to.equal(1) | ||
expect(connectionManager.openConnection.calledWith(matchPeerId(peerWithAddress.id))).to.be.true() | ||
expect(connectionManager.openConnection.calledWith(matchPeerId(undialablePeer.id))).to.be.false() | ||
|
||
// pass the retry threshold | ||
await delay(2000) | ||
|
||
// autodial again | ||
void autoDialler.autoDial() | ||
|
||
await pWaitFor(() => { | ||
return connectionManager.openConnection.callCount === 3 | ||
}) | ||
|
||
// should have retried the unreachable peer | ||
expect(connectionManager.openConnection.calledWith(matchPeerId(undialablePeer.id))).to.be.true() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add another test to ensure that the undialable peer is eventually dialled once the threshold has expired. |
||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if i'm not mistaken, this dial-queue is also used for direct dials, should we discern between an auto-dial triggered dial vs a manually/user requested dial?