diff --git a/bitswap/bitswap.go b/bitswap/bitswap.go index 7c5460000..3a42af297 100644 --- a/bitswap/bitswap.go +++ b/bitswap/bitswap.go @@ -48,7 +48,7 @@ type bitswap struct { // The Ledger has the peer.ID, and the peer connection works through net. // Ledgers of known relationships (active or inactive) stored in datastore. // Changes to the Ledger should be committed to the datastore. - partners LedgerMap + partners ledgerMap // haveList is the set of keys we have values for. a map for fast lookups. // haveList KeySet -- not needed. all values in datastore? @@ -68,7 +68,7 @@ func NewSession(parent context.Context, s bsnet.NetworkService, p *peer.Peer, d bs := &bitswap{ peer: p, blockstore: blockstore.NewBlockstore(d), - partners: LedgerMap{}, + partners: ledgerMap{}, wantList: KeySet{}, routing: r, sender: bsnet.NewNetworkAdapter(s, &receiver), @@ -196,13 +196,13 @@ func (bs *bitswap) blockReceive(p *peer.Peer, blk blocks.Block) { ledger.ReceivedBytes(len(blk.Data)) } -func (bs *bitswap) getLedger(p *peer.Peer) *Ledger { +func (bs *bitswap) getLedger(p *peer.Peer) *ledger { l, ok := bs.partners[p.Key()] if ok { return l } - l = new(Ledger) + l = new(ledger) l.Strategy = bs.strategy l.Partner = p bs.partners[p.Key()] = l diff --git a/bitswap/ledger.go b/bitswap/ledger.go index bf3b423b4..37731ebf8 100644 --- a/bitswap/ledger.go +++ b/bitswap/ledger.go @@ -8,8 +8,8 @@ import ( u "github.com/jbenet/go-ipfs/util" ) -// Ledger stores the data exchange relationship between two peers. -type Ledger struct { +// ledger stores the data exchange relationship between two peers. +type ledger struct { lock sync.RWMutex // Partner is the remote Peer. @@ -34,16 +34,16 @@ type Ledger struct { } // LedgerMap lists Ledgers by their Partner key. -type LedgerMap map[u.Key]*Ledger +type ledgerMap map[u.Key]*ledger -func (l *Ledger) ShouldSend() bool { +func (l *ledger) ShouldSend() bool { l.lock.Lock() defer l.lock.Unlock() return l.Strategy(l) } -func (l *Ledger) SentBytes(n int) { +func (l *ledger) SentBytes(n int) { l.lock.Lock() defer l.lock.Unlock() @@ -52,7 +52,7 @@ func (l *Ledger) SentBytes(n int) { l.Accounting.BytesSent += uint64(n) } -func (l *Ledger) ReceivedBytes(n int) { +func (l *ledger) ReceivedBytes(n int) { l.lock.Lock() defer l.lock.Unlock() @@ -62,14 +62,14 @@ func (l *Ledger) ReceivedBytes(n int) { } // TODO: this needs to be different. We need timeouts. -func (l *Ledger) Wants(k u.Key) { +func (l *ledger) Wants(k u.Key) { l.lock.Lock() defer l.lock.Unlock() l.wantList[k] = struct{}{} } -func (l *Ledger) WantListContains(k u.Key) bool { +func (l *ledger) WantListContains(k u.Key) bool { l.lock.RLock() defer l.lock.RUnlock() @@ -77,7 +77,7 @@ func (l *Ledger) WantListContains(k u.Key) bool { return ok } -func (l *Ledger) ExchangeCount() uint64 { +func (l *ledger) ExchangeCount() uint64 { l.lock.RLock() defer l.lock.RUnlock() return l.exchangeCount diff --git a/bitswap/ledger_test.go b/bitswap/ledger_test.go index d651d485f..b2bf9ee5f 100644 --- a/bitswap/ledger_test.go +++ b/bitswap/ledger_test.go @@ -7,7 +7,7 @@ import ( func TestRaceConditions(t *testing.T) { const numberOfExpectedExchanges = 10000 - l := new(Ledger) + l := new(ledger) var wg sync.WaitGroup for i := 0; i < numberOfExpectedExchanges; i++ { wg.Add(1) diff --git a/bitswap/strategy.go b/bitswap/strategy.go index 19f24dddd..a2c2db186 100644 --- a/bitswap/strategy.go +++ b/bitswap/strategy.go @@ -5,13 +5,13 @@ import ( "math/rand" ) -type strategyFunc func(*Ledger) bool +type strategyFunc func(*ledger) bool -func standardStrategy(l *Ledger) bool { +func standardStrategy(l *ledger) bool { return rand.Float64() <= probabilitySend(l.Accounting.Value()) } -func yesManStrategy(l *Ledger) bool { +func yesManStrategy(l *ledger) bool { return true }