mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
fix(bitswap:ledger) race conditions
https://github.com/jbenet/go-ipfs/issues/39
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package bitswap
|
package bitswap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
peer "github.com/jbenet/go-ipfs/peer"
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
@ -9,6 +10,7 @@ import (
|
|||||||
|
|
||||||
// Ledger stores the data exchange relationship between two peers.
|
// Ledger stores the data exchange relationship between two peers.
|
||||||
type Ledger struct {
|
type Ledger struct {
|
||||||
|
lock sync.RWMutex
|
||||||
|
|
||||||
// Partner is the remote Peer.
|
// Partner is the remote Peer.
|
||||||
Partner *peer.Peer
|
Partner *peer.Peer
|
||||||
@ -35,16 +37,25 @@ type Ledger struct {
|
|||||||
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)
|
return l.Strategy(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Ledger) SentBytes(n int) {
|
func (l *Ledger) SentBytes(n int) {
|
||||||
|
l.lock.Lock()
|
||||||
|
defer l.lock.Unlock()
|
||||||
|
|
||||||
l.exchangeCount++
|
l.exchangeCount++
|
||||||
l.lastExchange = time.Now()
|
l.lastExchange = time.Now()
|
||||||
l.Accounting.BytesSent += uint64(n)
|
l.Accounting.BytesSent += uint64(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Ledger) ReceivedBytes(n int) {
|
func (l *Ledger) ReceivedBytes(n int) {
|
||||||
|
l.lock.Lock()
|
||||||
|
defer l.lock.Unlock()
|
||||||
|
|
||||||
l.exchangeCount++
|
l.exchangeCount++
|
||||||
l.lastExchange = time.Now()
|
l.lastExchange = time.Now()
|
||||||
l.Accounting.BytesRecv += uint64(n)
|
l.Accounting.BytesRecv += uint64(n)
|
||||||
@ -52,10 +63,22 @@ func (l *Ledger) ReceivedBytes(n int) {
|
|||||||
|
|
||||||
// TODO: this needs to be different. We need timeouts.
|
// 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{}{}
|
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()
|
||||||
|
|
||||||
_, ok := l.wantList[k]
|
_, ok := l.wantList[k]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Ledger) ExchangeCount() uint64 {
|
||||||
|
l.lock.RLock()
|
||||||
|
defer l.lock.RUnlock()
|
||||||
|
return l.exchangeCount
|
||||||
|
}
|
||||||
|
23
bitswap/ledger_test.go
Normal file
23
bitswap/ledger_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package bitswap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRaceConditions(t *testing.T) {
|
||||||
|
const numberOfExpectedExchanges = 10000
|
||||||
|
l := new(Ledger)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < numberOfExpectedExchanges; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
l.ReceivedBytes(1)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
if l.ExchangeCount() != numberOfExpectedExchanges {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user