Commit 64ecba67 authored by Dirk McCormick's avatar Dirk McCormick

refactor: session peer manager ordering

parent 6a1362ca
...@@ -280,6 +280,8 @@ type getPeersMessage struct { ...@@ -280,6 +280,8 @@ type getPeersMessage struct {
// Get all optimized peers in order followed by randomly ordered unoptimized // Get all optimized peers in order followed by randomly ordered unoptimized
// peers, with a limit of maxOptimizedPeers // peers, with a limit of maxOptimizedPeers
func (prm *getPeersMessage) handle(spm *SessionPeerManager) { func (prm *getPeersMessage) handle(spm *SessionPeerManager) {
randomOrder := rand.Perm(len(spm.unoptimizedPeersArr))
// Number of peers to get in total: unoptimized + optimized // Number of peers to get in total: unoptimized + optimized
// limited by maxOptimizedPeers // limited by maxOptimizedPeers
maxPeers := len(spm.unoptimizedPeersArr) + len(spm.optimizedPeersArr) maxPeers := len(spm.unoptimizedPeersArr) + len(spm.optimizedPeersArr)
...@@ -287,8 +289,8 @@ func (prm *getPeersMessage) handle(spm *SessionPeerManager) { ...@@ -287,8 +289,8 @@ func (prm *getPeersMessage) handle(spm *SessionPeerManager) {
maxPeers = maxOptimizedPeers maxPeers = maxOptimizedPeers
} }
// The best peer latency is 1 if we have recorded at least one peer's // The best peer latency is the first optimized peer's latency.
// latency, 0 otherwise // If we haven't recorded any peer's latency, use 0.
var bestPeerLatency float64 var bestPeerLatency float64
if len(spm.optimizedPeersArr) > 0 { if len(spm.optimizedPeersArr) > 0 {
bestPeerLatency = float64(spm.activePeers[spm.optimizedPeersArr[0]].latency) bestPeerLatency = float64(spm.activePeers[spm.optimizedPeersArr[0]].latency)
...@@ -297,22 +299,19 @@ func (prm *getPeersMessage) handle(spm *SessionPeerManager) { ...@@ -297,22 +299,19 @@ func (prm *getPeersMessage) handle(spm *SessionPeerManager) {
} }
optimizedPeers := make([]bssd.OptimizedPeer, 0, maxPeers) optimizedPeers := make([]bssd.OptimizedPeer, 0, maxPeers)
for i := 0; i < maxPeers; i++ {
// Add optimized peers in order // First add optimized peers in order
for i := 0; i < maxPeers && i < len(spm.optimizedPeersArr); i++ { if i < len(spm.optimizedPeersArr) {
p := spm.optimizedPeersArr[i] p := spm.optimizedPeersArr[i]
optimizedPeers = append(optimizedPeers, bssd.OptimizedPeer{ optimizedPeers = append(optimizedPeers, bssd.OptimizedPeer{
Peer: p, Peer: p,
OptimizationRating: bestPeerLatency / float64(spm.activePeers[p].latency), OptimizationRating: bestPeerLatency / float64(spm.activePeers[p].latency),
}) })
} } else {
// Then add unoptimized peers in random order
// Add unoptimized peers in random order p := spm.unoptimizedPeersArr[randomOrder[i-len(spm.optimizedPeersArr)]]
randomOrder := rand.Perm(len(spm.unoptimizedPeersArr)) optimizedPeers = append(optimizedPeers, bssd.OptimizedPeer{Peer: p, OptimizationRating: 0.0})
remaining := maxPeers - len(optimizedPeers) }
for i := 0; i < remaining; i++ {
p := spm.unoptimizedPeersArr[randomOrder[i]]
optimizedPeers = append(optimizedPeers, bssd.OptimizedPeer{Peer: p, OptimizationRating: 0.0})
} }
prm.resp <- optimizedPeers prm.resp <- optimizedPeers
} }
......
...@@ -167,7 +167,7 @@ func TestOrderingPeers(t *testing.T) { ...@@ -167,7 +167,7 @@ func TestOrderingPeers(t *testing.T) {
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("Did not finish finding providers") t.Fatal("Did not finish finding providers")
} }
time.Sleep(20 * time.Millisecond) time.Sleep(5 * time.Millisecond)
// record broadcast // record broadcast
sessionPeerManager.RecordPeerRequests(nil, c) sessionPeerManager.RecordPeerRequests(nil, c)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment