Skip to content

Commit

Permalink
try another peer sorting metric. fix bug in when findproviders returns
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann authored and Stebalien committed Mar 4, 2020
1 parent 3139a22 commit 29f8bb3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
38 changes: 36 additions & 2 deletions kpeerset/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kpeerset

import (
"math/big"
"sort"
"time"

Expand All @@ -20,7 +21,12 @@ type peerLatencyMetricList []peerLatencyMetric
func (p peerLatencyMetricList) Len() int { return len(p) }
func (p peerLatencyMetricList) Less(i, j int) bool {
pm1, pm2 := p[i], p[j]
return calculationLess(pm1, pm2)
}
func (p peerLatencyMetricList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p peerLatencyMetricList) GetPeerID(i int) peer.ID { return p[i].peer }

func less(pm1, pm2 *peerLatencyMetric) bool {
p1Connectedness, p2Connectedness := pm1.connectedness, pm2.connectedness
p1Latency, p2Latency := pm1.latency, pm2.latency

Expand Down Expand Up @@ -58,8 +64,36 @@ func (p peerLatencyMetricList) Less(i, j int) bool {

return pm1.metric.Cmp(pm2.metric) == -1
}
func (p peerLatencyMetricList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p peerLatencyMetricList) GetPeerID(i int) peer.ID { return p[i].peer }

func calculationLess(pm1, pm2 peerLatencyMetric) bool {
return calc(pm1).Cmp(calc(pm2)) == -1
}

func calc(pm peerLatencyMetric) *big.Int {
var c int64
switch pm.connectedness {
case network.Connected:
c = 1
case network.CanConnect:
c = 5
case network.CannotConnect:
c = 10000
default:
c = 20
}

l := int64(pm.latency)
if l <= 0 {
l = int64(time.Second) * 10
}

res := big.NewInt(c)
tmp := big.NewInt(l)
res.Mul(res, tmp)
res.Mul(res, pm.metric)

return res
}

var _ SortablePeers = (*peerLatencyMetricList)(nil)

Expand Down
2 changes: 1 addition & 1 deletion routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key multihash
return peers, nil
},
func(peerset *kpeerset.SortedPeerset) bool {
return ps.Size() > count
return ps.Size() >= count
},
)

Expand Down

0 comments on commit 29f8bb3

Please sign in to comment.