mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-04-25 06:42:13 +00:00
notify: make locking clearer
This commit is contained in:
parent
13a847a8bb
commit
ba0ddf36bd
59
notif.go
59
notif.go
@ -45,40 +45,43 @@ func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
|
|||||||
cancel: cancel,
|
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
|
||||||
|
go nn.testConnection(ctx, v)
|
||||||
|
|
||||||
// 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 {
|
func (nn *netNotifiee) testConnection(ctx context.Context, v inet.Conn) {
|
||||||
s, err := dht.host.NewStream(ctx, v.RemotePeer(), ProtocolDHT, ProtocolDHTOld)
|
dht := nn.DHT()
|
||||||
|
for {
|
||||||
|
s, err := dht.host.NewStream(ctx, v.RemotePeer(), ProtocolDHT, ProtocolDHTOld)
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
s.Close()
|
s.Close()
|
||||||
dht.plk.Lock()
|
dht.plk.Lock()
|
||||||
defer dht.plk.Unlock()
|
|
||||||
|
|
||||||
// Check if canceled under the lock.
|
// Check if canceled under the lock.
|
||||||
if ctx.Err() == nil {
|
if ctx.Err() == nil {
|
||||||
dht.Update(dht.Context(), v.RemotePeer())
|
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)
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
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) {
|
func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user