Commit cd0cb0b7 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 1afac8dc
...@@ -19,13 +19,13 @@ import ( ...@@ -19,13 +19,13 @@ import (
// NetMessageSession initializes a BitSwap session that communicates over the // NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service // 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) networkAdapter := bsnet.NetMessageAdapter(s, nil)
bs := &bitswap{ bs := &bitswap{
blockstore: blockstore.NewBlockstore(d), blockstore: blockstore.NewBlockstore(d),
notifications: notifications.New(), notifications: notifications.New(),
strategy: strategy.New(), strategy: strategy.New(nice),
routing: directory, routing: directory,
sender: networkAdapter, sender: networkAdapter,
wantlist: u.NewKeySet(), wantlist: u.NewKeySet(),
......
...@@ -283,10 +283,11 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance { ...@@ -283,10 +283,11 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
htc := rs.Client(p) htc := rs.Client(p)
blockstore := bstore.NewBlockstore(ds.NewMapDatastore()) blockstore := bstore.NewBlockstore(ds.NewMapDatastore())
const alwaysSendToPeer = true
bs := &bitswap{ bs := &bitswap{
blockstore: blockstore, blockstore: blockstore,
notifications: notifications.New(), notifications: notifications.New(),
strategy: strategy.New(), strategy: strategy.New(alwaysSendToPeer),
routing: htc, routing: htc,
sender: adapter, sender: adapter,
wantlist: util.NewKeySet(), wantlist: util.NewKeySet(),
......
...@@ -7,6 +7,9 @@ import ( ...@@ -7,6 +7,9 @@ import (
type strategyFunc func(*ledger) bool 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 { func standardStrategy(l *ledger) bool {
return rand.Float64() <= probabilitySend(l.Accounting.Value()) return rand.Float64() <= probabilitySend(l.Accounting.Value())
} }
......
...@@ -9,10 +9,19 @@ import ( ...@@ -9,10 +9,19 @@ import (
) )
// TODO declare thread-safe datastore // 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{ return &strategist{
ledgerMap: ledgerMap{}, ledgerMap: ledgerMap{},
strategyFunc: yesManStrategy, strategyFunc: stratFunc,
} }
} }
......
...@@ -17,7 +17,7 @@ type peerAndStrategist struct { ...@@ -17,7 +17,7 @@ type peerAndStrategist struct {
func newPeerAndStrategist(idStr string) peerAndStrategist { func newPeerAndStrategist(idStr string) peerAndStrategist {
return peerAndStrategist{ return peerAndStrategist{
Peer: &peer.Peer{ID: peer.ID(idStr)}, 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