@cryptotaxi247 / kubo / commits / b36670df7

refac(bitswap) privatize ledger

temporarily. at least until refactor is complete

Brian Tiger Chow committed Sep 16, 2014 at 08:27 UTC b36670df7ee460d20088bbe957400c5f2bdb1631
4 files changed +17 -17
bitswap/bitswap.go
+4 -4
@@ -48,7 +48,7 @@ type bitswap struct {
48 // The Ledger has the peer.ID, and the peer connection works through net.
49 // Ledgers of known relationships (active or inactive) stored in datastore.
50 // Changes to the Ledger should be committed to the datastore.
51 - partners LedgerMap
51 + partners ledgerMap
52
53 // haveList is the set of keys we have values for. a map for fast lookups.
54 // haveList KeySet -- not needed. all values in datastore?
@@ -68,7 +68,7 @@ func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d
68 bs := &bitswap{
69 peer: p,
70 blockstore: blockstore.NewBlockstore(d),
71 - partners: LedgerMap{},
71 + partners: ledgerMap{},
72 wantList: KeySet{},
73 routing: r,
74 sender: bsnet.NewNetworkAdapter(s, &receiver),
@@ -196,13 +196,13 @@ func (bs *bitswap) blockReceive(p *peer.Peer, blk blocks.Block) {
196 ledger.ReceivedBytes(len(blk.Data))
197 }
198
199 -func (bs *bitswap) getLedger(p *peer.Peer) *Ledger {
199 +func (bs *bitswap) getLedger(p *peer.Peer) *ledger {
200 l, ok := bs.partners[p.Key()]
201 if ok {
202 return l
203 }
204
205 - l = new(Ledger)
205 + l = new(ledger)
206 l.Strategy = bs.strategy
207 l.Partner = p
208 bs.partners[p.Key()] = l
bitswap/ledger.go
+9 -9
@@ -8,8 +8,8 @@ import (
8 u "github.com/jbenet/go-ipfs/util"
9 )
10
11 -// Ledger stores the data exchange relationship between two peers.
12 -type Ledger struct {
11 +// ledger stores the data exchange relationship between two peers.
12 +type ledger struct {
13 lock sync.RWMutex
14
15 // Partner is the remote Peer.
@@ -34,16 +34,16 @@ type Ledger struct {
34 }
35
36 // LedgerMap lists Ledgers by their Partner key.
37 -type LedgerMap map[u.Key]*Ledger
37 +type ledgerMap map[u.Key]*ledger
38
39 -func (l *Ledger) ShouldSend() bool {
39 +func (l *ledger) ShouldSend() bool {
40 l.lock.Lock()
41 defer l.lock.Unlock()
42
43 return l.Strategy(l)
44 }
45
46 -func (l *Ledger) SentBytes(n int) {
46 +func (l *ledger) SentBytes(n int) {
47 l.lock.Lock()
48 defer l.lock.Unlock()
49
@@ -52,7 +52,7 @@ func (l *Ledger) SentBytes(n int) {
52 l.Accounting.BytesSent += uint64(n)
53 }
54
55 -func (l *Ledger) ReceivedBytes(n int) {
55 +func (l *ledger) ReceivedBytes(n int) {
56 l.lock.Lock()
57 defer l.lock.Unlock()
58
@@ -62,14 +62,14 @@ func (l *Ledger) ReceivedBytes(n int) {
62 }
63
64 // TODO: this needs to be different. We need timeouts.
65 -func (l *Ledger) Wants(k u.Key) {
65 +func (l *ledger) Wants(k u.Key) {
66 l.lock.Lock()
67 defer l.lock.Unlock()
68
69 l.wantList[k] = struct{}{}
70 }
71
72 -func (l *Ledger) WantListContains(k u.Key) bool {
72 +func (l *ledger) WantListContains(k u.Key) bool {
73 l.lock.RLock()
74 defer l.lock.RUnlock()
75
@@ -77,7 +77,7 @@ func (l *Ledger) WantListContains(k u.Key) bool {
77 return ok
78 }
79
80 -func (l *Ledger) ExchangeCount() uint64 {
80 +func (l *ledger) ExchangeCount() uint64 {
81 l.lock.RLock()
82 defer l.lock.RUnlock()
83 return l.exchangeCount
bitswap/ledger_test.go
+1 -1
@@ -7,7 +7,7 @@ import (
7
8 func TestRaceConditions(t *testing.T) {
9 const numberOfExpectedExchanges = 10000
10 - l := new(Ledger)
10 + l := new(ledger)
11 var wg sync.WaitGroup
12 for i := 0; i < numberOfExpectedExchanges; i++ {
13 wg.Add(1)
bitswap/strategy.go
+3 -3
@@ -5,13 +5,13 @@ import (
5 "math/rand"
6 )
7
8 -type strategyFunc func(*Ledger) bool
8 +type strategyFunc func(*ledger) bool
9
10 -func standardStrategy(l *Ledger) bool {
10 +func standardStrategy(l *ledger) bool {
11 return rand.Float64() <= probabilitySend(l.Accounting.Value())
12 }
13
14 -func yesManStrategy(l *Ledger) bool {
14 +func yesManStrategy(l *ledger) bool {
15 return true
16 }
17