Skip to content

Commit

Permalink
feat: prioritize geoip lookup of closest peers
Browse files Browse the repository at this point in the history
This change improves performance of initial load
of Peers screen when there are thousands of peers.

During the first three times PeerLocationResolver.findLocations is called
we sort peers by latency and resolve geoip only for the closest subset.

This ensures the top of the list in the UI gets updated fast while
thousands of more distant peers gets resolved later.

closes #1273
  • Loading branch information
lidel committed Oct 24, 2019
1 parent 0e15b96 commit ab83def
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/bundles/peer-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ class PeerLocationResolver {
})

this.geoipLookupPromises = {}

this.pass = 0
}

async findLocations (peers, getIpfs) {
const res = {}

for (const p of peers) {
for (const p of this.optimizedPeerSet(peers)) {
const peerId = p.peer.toB58String()
const addr = p.addr.toString()

Expand Down Expand Up @@ -204,4 +206,32 @@ class PeerLocationResolver {

return res
}

optimizedPeerSet (peers) {
if (this.pass < 3) {
// sort by latency so we can resolve closes ones first
// (https://github.com/ipfs-shipyard/ipfs-webui/issues/1273)
peers.sort((a, b) => {
a = parseLatency(a.latency) || 9999
b = parseLatency(b.latency) || 9999
return a - b
})
// take the closest subset, increase sample size each time
// this ensures initial map updates are fast even with thousands of peers
switch (this.pass) {
case 0:
peers = peers.slice(0, 10)
break
case 1:
peers = peers.slice(0, 100)
break
case 2:
peers = peers.slice(0, 200)
break
default:
}
this.pass = this.pass + 1
}
return peers
}
}

0 comments on commit ab83def

Please sign in to comment.