kubo/exchange/bitswap/strategy/math.go
Brian Tiger Chow 9d7ae40003 feat(bitswap) expose ability to toggle "niceness"
true -> always send to peer

false -> use ledger-based strategy described in IPFS paper draft 3
2014-09-22 04:06:19 -07:00

35 lines
687 B
Go

package strategy
import (
"math"
"math/rand"
)
type strategyFunc func(*ledger) bool
// TODO avoid using rand.Float64 method. it uses a singleton lock and may cause
// performance issues. Instead, instantiate a rand struct and use that to call
// Float64()
func standardStrategy(l *ledger) bool {
return rand.Float64() <= probabilitySend(l.Accounting.Value())
}
func yesManStrategy(l *ledger) bool {
return true
}
func probabilitySend(ratio float64) float64 {
x := 1 + math.Exp(6-3*ratio)
y := 1 / x
return 1 - y
}
type debtRatio struct {
BytesSent uint64
BytesRecv uint64
}
func (dr *debtRatio) Value() float64 {
return float64(dr.BytesSent) / float64(dr.BytesRecv+1)
}