Commit 9d7ae400 authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat(bitswap) expose ability to toggle "niceness"

true -> always send to peer

false -> use ledger-based strategy described in IPFS paper draft 3
parent 767d6ca6
......@@ -118,7 +118,8 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
// TODO(brian): perform this inside NewDHT factory method
dhtService.Handler = route // wire the handler to the service.
exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d)
const alwaysSendToPeer = true // use YesManStrategy
exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer)
// TODO(brian): pass a context to initConnections
go initConnections(ctx, cfg, peerstore, route)
......
......@@ -19,13 +19,13 @@ import (
// NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service
func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore) exchange.Interface {
func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore, nice bool) exchange.Interface {
networkAdapter := bsnet.NetMessageAdapter(s, nil)
bs := &bitswap{
blockstore: blockstore.NewBlockstore(d),
notifications: notifications.New(),
strategy: strategy.New(),
strategy: strategy.New(nice),
routing: directory,
sender: networkAdapter,
wantlist: u.NewKeySet(),
......
......@@ -283,10 +283,11 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
htc := rs.Client(p)
blockstore := bstore.NewBlockstore(ds.NewMapDatastore())
const alwaysSendToPeer = true
bs := &bitswap{
blockstore: blockstore,
notifications: notifications.New(),
strategy: strategy.New(),
strategy: strategy.New(alwaysSendToPeer),
routing: htc,
sender: adapter,
wantlist: util.NewKeySet(),
......
......@@ -7,6 +7,9 @@ import (
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())
}
......
......@@ -9,10 +9,19 @@ import (
)
// TODO declare thread-safe datastore
func New() Strategy {
// TODO niceness should be on a per-peer basis. Use-case: Certain peers are
// "trusted" and/or controlled by a single human user. The user may want for
// these peers to exchange data freely
func New(nice bool) Strategy {
var stratFunc strategyFunc
if nice {
stratFunc = yesManStrategy
} else {
stratFunc = standardStrategy
}
return &strategist{
ledgerMap: ledgerMap{},
strategyFunc: yesManStrategy,
strategyFunc: stratFunc,
}
}
......
......@@ -17,7 +17,7 @@ type peerAndStrategist struct {
func newPeerAndStrategist(idStr string) peerAndStrategist {
return peerAndStrategist{
Peer: &peer.Peer{ID: peer.ID(idStr)},
Strategy: New(),
Strategy: New(true),
}
}
......
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