Commit 61972176 authored by Dirk McCormick's avatar Dirk McCormick

refactor: cheaper rand want selection

parent ec9fb77f
package session
import (
"math"
"math/rand"
"sync"
"time"
......@@ -134,7 +133,7 @@ func (sw *sessionWants) LiveWants() []cid.Cid {
// RandomLiveWant returns a randomly selected live want
func (sw *sessionWants) RandomLiveWant() cid.Cid {
r := rand.Float64()
i := rand.Uint64()
sw.RLock()
defer sw.RUnlock()
......@@ -142,7 +141,7 @@ func (sw *sessionWants) RandomLiveWant() cid.Cid {
if len(sw.liveWants) == 0 {
return cid.Cid{}
}
i := math.Floor(r * float64(len(sw.liveWants)))
i %= uint64(len(sw.liveWants))
// picking a random live want
for k := range sw.liveWants {
if i == 0 {
......
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