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-bitswap
Commits
d8454fe8
Commit
d8454fe8
authored
Feb 20, 2019
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(peermanager): fix get/set race
repace get/set with getOrCreate to keep operations atomic
parent
9b54f912
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
27 deletions
+15
-27
messagequeue/messagequeue.go
messagequeue/messagequeue.go
+1
-1
peermanager/peermanager.go
peermanager/peermanager.go
+13
-25
peermanager/peermanager_test.go
peermanager/peermanager_test.go
+1
-1
No files found.
messagequeue/messagequeue.go
View file @
d8454fe8
...
...
@@ -48,7 +48,7 @@ func New(p peer.ID, network MessageNetwork) *MessageQueue {
wl
:
wantlist
.
NewThreadSafe
(),
network
:
network
,
p
:
p
,
refcnt
:
1
,
refcnt
:
0
,
}
}
...
...
peermanager/peermanager.go
View file @
d8454fe8
...
...
@@ -67,22 +67,12 @@ func (pm *PeerManager) ConnectedPeers() []peer.ID {
// Connected is called to add a new peer to the pool, and send it an initial set
// of wants.
func
(
pm
*
PeerManager
)
Connected
(
p
peer
.
ID
,
initialEntries
[]
*
wantlist
.
Entry
)
{
mq
,
ok
:=
pm
.
get
(
p
)
mq
:=
pm
.
get
OrCreate
(
p
)
if
ok
{
if
mq
.
RefCount
()
==
0
{
mq
.
AddWantlist
(
initialEntries
)
}
mq
.
RefIncrement
()
return
if
mq
.
RefCount
()
==
0
{
mq
.
AddWantlist
(
initialEntries
)
}
mq
=
pm
.
createPeerQueue
(
p
)
pm
.
set
(
p
,
mq
)
mq
.
Startup
(
pm
.
ctx
)
mq
.
AddWantlist
(
initialEntries
)
mq
.
RefIncrement
()
}
// Disconnected is called to remove a peer from the pool.
...
...
@@ -112,15 +102,7 @@ func (pm *PeerManager) SendMessage(entries []*bsmsg.Entry, targets []peer.ID, fr
})
}
else
{
for
_
,
t
:=
range
targets
{
p
,
ok
:=
pm
.
get
(
t
)
if
!
ok
{
p
=
pm
.
createPeerQueue
(
t
)
pm
.
set
(
t
,
p
)
p
.
Startup
(
pm
.
ctx
)
// this is a "0 reference" queue because we haven't actually connected to it
// sending the first message will cause it to connect
p
.
RefDecrement
()
}
p
:=
pm
.
getOrCreate
(
t
)
p
.
AddMessage
(
entries
,
from
)
}
}
...
...
@@ -133,10 +115,16 @@ func (pm *PeerManager) get(p peer.ID) (PeerQueue, bool) {
return
pq
,
ok
}
func
(
pm
*
PeerManager
)
s
et
(
p
peer
.
ID
,
pq
PeerQueue
)
{
func
(
pm
*
PeerManager
)
g
et
OrCreate
(
p
peer
.
ID
)
PeerQueue
{
pm
.
peerQueuesLk
.
Lock
()
pm
.
peerQueues
[
p
]
=
pq
pq
,
ok
:=
pm
.
peerQueues
[
p
]
if
!
ok
{
pq
=
pm
.
createPeerQueue
(
p
)
pq
.
Startup
(
pm
.
ctx
)
pm
.
peerQueues
[
p
]
=
pq
}
pm
.
peerQueuesLk
.
Unlock
()
return
pq
}
func
(
pm
*
PeerManager
)
remove
(
p
peer
.
ID
)
{
...
...
peermanager/peermanager_test.go
View file @
d8454fe8
...
...
@@ -41,7 +41,7 @@ func makePeerQueueFactory(messagesSent chan messageSent) PeerQueueFactory {
return
func
(
p
peer
.
ID
)
PeerQueue
{
return
&
fakePeer
{
p
:
p
,
refcnt
:
1
,
refcnt
:
0
,
messagesSent
:
messagesSent
,
}
}
...
...
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