Skip to content

Commit

Permalink
improve connect
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Nov 12, 2023
1 parent 6eb65d2 commit 67923c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
16 changes: 11 additions & 5 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,22 @@ async function holepunch (c, opts) {
}

async function findAndConnect (c, opts) {
let attempts = 0
let closestNodes = (opts.relayAddresses && opts.relayAddresses.length) ? opts.relayAddresses : null

if (c.dht._persistent) { // check if we know the route ourself...
const route = c.dht._router.get(c.target)
if (route && route.relay !== null) closestNodes = [{ host: route.relay.host, port: route.relay.port }]
}

// 2 is how many parallel connect attempts we want to do, we can make this configurable
const sem = new Semaphore(2)
let attempts = 0
const signal = sem.signal.bind(sem)

let closestNodes = (opts.relayAddresses && opts.relayAddresses.length) ? opts.relayAddresses : null
const tries = closestNodes !== null ? 2 : 1

try {
for (let i = 0; i < 2 && !isDone(c) && !c.connect; i++) {
c.query = c.dht.findPeer(c.target, { hash: false, session: c.session, closestNodes, onlyClosestNodes: !!closestNodes })
for (let i = 0; i < tries && !isDone(c) && !c.connect; i++) {
c.query = c.dht.findPeer(c.target, { hash: false, session: c.session, closestNodes, onlyClosestNodes: closestNodes !== null })

for await (const data of c.query) {
await sem.wait()
Expand Down
39 changes: 37 additions & 2 deletions test/announces.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,50 @@ test('server announces relay addrs', async function (t) {
await b.findNode(b.id).finished()

const server = await a.createServer().listen()

const nodes = await toArray(b.findPeer(server.publicKey))
const q = b.findPeer(server.publicKey)
const nodes = await toArray(q)

for (const addr of server.relayAddresses) {
let found = false

for (const node of nodes) {
found = node.from.port === addr.port && node.from.host === addr.host
if (found) break
}

if (!found) {
const { host, port } = b.remoteAddress()
found = port === addr.port && host === addr.host
}

if (!found) {
const { host, port } = a.remoteAddress()
found = port === addr.port && host === addr.host
}

t.ok(found, 'found addr')
}
})

test('connect when we relay ourself', async function (t) {
const testnet = await swarm(t)

const server = await testnet.nodes[1].createServer(function (sock) {
sock.resume()
sock.end()
}).listen()

const addr = server.relayAddresses[server.relayAddresses.length - 1]

for (const node of testnet.nodes) {
const { host, port } = node.remoteAddress()
if (addr.port === port && addr.host === host) {
const sock = node.connect(server.publicKey)
await sock.opened
t.pass('worked')
sock.end()
await new Promise(resolve => sock.once('close', resolve))
break
}
}
})

0 comments on commit 67923c5

Please sign in to comment.