strategy.go 3.32 KB
Newer Older
1 2 3 4
package strategy

import (
	"errors"
5
	"sync"
Jeromy's avatar
Jeromy committed
6
	"time"
7 8

	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
9
	peer "github.com/jbenet/go-ipfs/peer"
10 11 12
	u "github.com/jbenet/go-ipfs/util"
)

13 14 15 16 17 18 19 20 21 22
// 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
	}
23 24
	return &strategist{
		ledgerMap:    ledgerMap{},
25
		strategyFunc: stratFunc,
26 27 28 29
	}
}

type strategist struct {
30
	lock sync.RWMutex
31 32 33 34 35 36 37 38 39 40 41
	ledgerMap
	strategyFunc
}

// LedgerMap lists Ledgers by their Partner key.
type ledgerMap map[peerKey]*ledger

// FIXME share this externally
type peerKey u.Key

// Peers returns a list of peers
42
func (s *strategist) Peers() []peer.Peer {
43 44 45
	s.lock.RLock()
	defer s.lock.RUnlock()

46
	response := make([]peer.Peer, 0)
47 48 49 50 51 52
	for _, ledger := range s.ledgerMap {
		response = append(response, ledger.Partner)
	}
	return response
}

53
func (s *strategist) BlockIsWantedByPeer(k u.Key, p peer.Peer) bool {
54 55 56
	s.lock.RLock()
	defer s.lock.RUnlock()

57 58 59 60
	ledger := s.ledger(p)
	return ledger.WantListContains(k)
}

61
func (s *strategist) ShouldSendBlockToPeer(k u.Key, p peer.Peer) bool {
62 63 64
	s.lock.RLock()
	defer s.lock.RUnlock()

65 66 67 68 69
	ledger := s.ledger(p)
	return ledger.ShouldSend()
}

func (s *strategist) Seed(int64) {
70 71 72
	s.lock.Lock()
	defer s.lock.Unlock()

73 74 75
	// TODO
}

76 77
// MessageReceived performs book-keeping. Returns error if passed invalid
// arguments.
78
func (s *strategist) MessageReceived(p peer.Peer, m bsmsg.BitSwapMessage) error {
79 80 81
	s.lock.Lock()
	defer s.lock.Unlock()

Brian Tiger Chow's avatar
Brian Tiger Chow committed
82 83 84 85 86 87 88
	// TODO find a more elegant way to handle this check
	if p == nil {
		return errors.New("Strategy received nil peer")
	}
	if m == nil {
		return errors.New("Strategy received nil message")
	}
89 90 91 92 93 94 95 96
	l := s.ledger(p)
	for _, key := range m.Wantlist() {
		l.Wants(key)
	}
	for _, block := range m.Blocks() {
		// FIXME extract blocks.NumBytes(block) or block.NumBytes() method
		l.ReceivedBytes(len(block.Data))
	}
97
	return nil
98 99 100 101 102 103 104 105
}

// TODO add contents of m.WantList() to my local wantlist? NB: could introduce
// race conditions where I send a message, but MessageSent gets handled after
// MessageReceived. The information in the local wantlist could become
// inconsistent. Would need to ensure that Sends and acknowledgement of the
// send happen atomically

106
func (s *strategist) MessageSent(p peer.Peer, m bsmsg.BitSwapMessage) error {
107 108 109
	s.lock.Lock()
	defer s.lock.Unlock()

110 111 112 113
	l := s.ledger(p)
	for _, block := range m.Blocks() {
		l.SentBytes(len(block.Data))
	}
114 115 116

	// TODO remove these blocks from peer's want list

117 118 119
	return nil
}

120
func (s *strategist) NumBytesSentTo(p peer.Peer) uint64 {
121 122 123
	s.lock.RLock()
	defer s.lock.RUnlock()

124 125 126
	return s.ledger(p).Accounting.BytesSent
}

127
func (s *strategist) NumBytesReceivedFrom(p peer.Peer) uint64 {
128 129 130
	s.lock.RLock()
	defer s.lock.RUnlock()

131 132 133
	return s.ledger(p).Accounting.BytesRecv
}

134
// ledger lazily instantiates a ledger
135
func (s *strategist) ledger(p peer.Peer) *ledger {
136 137 138 139 140 141 142
	l, ok := s.ledgerMap[peerKey(p.Key())]
	if !ok {
		l = newLedger(p, s.strategyFunc)
		s.ledgerMap[peerKey(p.Key())] = l
	}
	return l
}
Jeromy's avatar
Jeromy committed
143 144 145 146 147 148 149 150

func (s *strategist) GetBatchSize() int {
	return 10
}

func (s *strategist) GetRebroadcastDelay() time.Duration {
	return time.Second * 2
}