提交 9d7ae400 作者: 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
上级 767d6ca6
...@@ -118,7 +118,8 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { ...@@ -118,7 +118,8 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
// TODO(brian): perform this inside NewDHT factory method // TODO(brian): perform this inside NewDHT factory method
dhtService.Handler = route // wire the handler to the service. 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 // TODO(brian): pass a context to initConnections
go initConnections(ctx, cfg, peerstore, route) go initConnections(ctx, cfg, peerstore, route)
......
...@@ -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 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论