feat(bitswap) expose ability to toggle "niceness"

true -> always send to peer

false -> use ledger-based strategy described in IPFS paper draft 3
This commit is contained in:
Brian Tiger Chow 2014-09-21 23:34:42 -07:00
parent 767d6ca633
commit 9d7ae40003
6 changed files with 21 additions and 7 deletions

View File

@ -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)

View File

@ -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(),

View File

@ -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(),

View File

@ -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())
}

View File

@ -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,
}
}

View File

@ -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),
}
}