Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
ad303335
Commit
ad303335
authored
10 years ago
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(bitswap:ledger) race conditions
https://github.com/jbenet/go-ipfs/issues/39
parent
7bae7428
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
bitswap/ledger.go
bitswap/ledger.go
+23
-0
bitswap/ledger_test.go
bitswap/ledger_test.go
+23
-0
No files found.
bitswap/ledger.go
View file @
ad303335
package
bitswap
import
(
"sync"
"time"
peer
"github.com/jbenet/go-ipfs/peer"
...
...
@@ -9,6 +10,7 @@ import (
// Ledger stores the data exchange relationship between two peers.
type
Ledger
struct
{
lock
sync
.
RWMutex
// Partner is the remote Peer.
Partner
*
peer
.
Peer
...
...
@@ -35,16 +37,25 @@ type Ledger struct {
type
LedgerMap
map
[
u
.
Key
]
*
Ledger
func
(
l
*
Ledger
)
ShouldSend
()
bool
{
l
.
lock
.
Lock
()
defer
l
.
lock
.
Unlock
()
return
l
.
Strategy
(
l
)
}
func
(
l
*
Ledger
)
SentBytes
(
n
int
)
{
l
.
lock
.
Lock
()
defer
l
.
lock
.
Unlock
()
l
.
exchangeCount
++
l
.
lastExchange
=
time
.
Now
()
l
.
Accounting
.
BytesSent
+=
uint64
(
n
)
}
func
(
l
*
Ledger
)
ReceivedBytes
(
n
int
)
{
l
.
lock
.
Lock
()
defer
l
.
lock
.
Unlock
()
l
.
exchangeCount
++
l
.
lastExchange
=
time
.
Now
()
l
.
Accounting
.
BytesRecv
+=
uint64
(
n
)
...
...
@@ -52,10 +63,22 @@ func (l *Ledger) ReceivedBytes(n int) {
// TODO: this needs to be different. We need timeouts.
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
{
l
.
lock
.
RLock
()
defer
l
.
lock
.
RUnlock
()
_
,
ok
:=
l
.
wantList
[
k
]
return
ok
}
func
(
l
*
Ledger
)
ExchangeCount
()
uint64
{
l
.
lock
.
RLock
()
defer
l
.
lock
.
RUnlock
()
return
l
.
exchangeCount
}
This diff is collapsed.
Click to expand it.
bitswap/ledger_test.go
0 → 100644
View file @
ad303335
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
()
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment