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
p2p
go-p2p-kad-dht
Commits
ba0ddf36
Commit
ba0ddf36
authored
Jul 26, 2017
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
notify: make locking clearer
parent
13a847a8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
32 deletions
+35
-32
notif.go
notif.go
+35
-32
No files found.
notif.go
View file @
ba0ddf36
...
...
@@ -45,40 +45,43 @@ func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
cancel
:
cancel
,
}
go
func
()
{
// Note: We *could* just check the peerstore to see if the remote side supports the dht
// protocol, but its not clear that that information will make it into the peerstore
// by the time this notification is sent. So just to be very careful, we brute force this
// and open a new stream
for
{
s
,
err
:=
dht
.
host
.
NewStream
(
ctx
,
v
.
RemotePeer
(),
ProtocolDHT
,
ProtocolDHTOld
)
switch
err
{
case
nil
:
s
.
Close
()
dht
.
plk
.
Lock
()
defer
dht
.
plk
.
Unlock
()
// Check if canceled under the lock.
if
ctx
.
Err
()
==
nil
{
dht
.
Update
(
dht
.
Context
(),
v
.
RemotePeer
())
}
case
io
.
EOF
:
if
ctx
.
Err
()
==
nil
{
// Connection died but we may still have *an* open connection (context not canceled) so try again.
continue
}
case
mstream
.
ErrNotSupported
:
// Client mode only, don't bother adding them to our routing table
default
:
// real error? thats odd
log
.
Errorf
(
"checking dht client type: %s"
,
err
)
// Note: We *could* just check the peerstore to see if the remote side supports the dht
// protocol, but its not clear that that information will make it into the peerstore
// by the time this notification is sent. So just to be very careful, we brute force this
// and open a new stream
go
nn
.
testConnection
(
ctx
,
v
)
}
func
(
nn
*
netNotifiee
)
testConnection
(
ctx
context
.
Context
,
v
inet
.
Conn
)
{
dht
:=
nn
.
DHT
()
for
{
s
,
err
:=
dht
.
host
.
NewStream
(
ctx
,
v
.
RemotePeer
(),
ProtocolDHT
,
ProtocolDHTOld
)
switch
err
{
case
nil
:
s
.
Close
()
dht
.
plk
.
Lock
()
// Check if canceled under the lock.
if
ctx
.
Err
()
==
nil
{
dht
.
Update
(
dht
.
Context
(),
v
.
RemotePeer
())
}
dht
.
plk
.
Unlock
()
case
io
.
EOF
:
if
ctx
.
Err
()
==
nil
{
// Connection died but we may still have *an* open connection (context not canceled) so try again.
continue
}
return
case
mstream
.
ErrNotSupported
:
// Client mode only, don't bother adding them to our routing table
default
:
// real error? thats odd
log
.
Errorf
(
"checking dht client type: %s"
,
err
)
}
}()
return
}
}
func
(
nn
*
netNotifiee
)
Disconnected
(
n
inet
.
Network
,
v
inet
.
Conn
)
{
...
...
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