mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-27 03:58:06 +00:00
Merge pull request #741 from nspcc-dev/fix/network
network: fix possible deadlock in DefaultDiscovery
This commit is contained in:
commit
b7fa8dd40f
1 changed files with 5 additions and 4 deletions
|
@ -30,6 +30,7 @@ type Discoverer interface {
|
||||||
type DefaultDiscovery struct {
|
type DefaultDiscovery struct {
|
||||||
transport Transporter
|
transport Transporter
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
|
closeMtx sync.RWMutex
|
||||||
dialTimeout time.Duration
|
dialTimeout time.Duration
|
||||||
badAddrs map[string]bool
|
badAddrs map[string]bool
|
||||||
connectedAddrs map[string]bool
|
connectedAddrs map[string]bool
|
||||||
|
@ -90,11 +91,11 @@ func (d *DefaultDiscovery) pushToPoolOrDrop(addr string) {
|
||||||
|
|
||||||
// RequestRemote tries to establish a connection with n nodes.
|
// RequestRemote tries to establish a connection with n nodes.
|
||||||
func (d *DefaultDiscovery) RequestRemote(n int) {
|
func (d *DefaultDiscovery) RequestRemote(n int) {
|
||||||
d.lock.RLock()
|
d.closeMtx.RLock()
|
||||||
if !d.isDead {
|
if !d.isDead {
|
||||||
d.requestCh <- n
|
d.requestCh <- n
|
||||||
}
|
}
|
||||||
d.lock.RUnlock()
|
d.closeMtx.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterBadAddr registers the given address as a bad address.
|
// RegisterBadAddr registers the given address as a bad address.
|
||||||
|
@ -179,9 +180,9 @@ func (d *DefaultDiscovery) tryAddress(addr string) {
|
||||||
|
|
||||||
// Close stops discoverer pool processing making discoverer almost useless.
|
// Close stops discoverer pool processing making discoverer almost useless.
|
||||||
func (d *DefaultDiscovery) Close() {
|
func (d *DefaultDiscovery) Close() {
|
||||||
d.lock.Lock()
|
d.closeMtx.Lock()
|
||||||
d.isDead = true
|
d.isDead = true
|
||||||
d.lock.Unlock()
|
d.closeMtx.Unlock()
|
||||||
select {
|
select {
|
||||||
case <-d.requestCh: // Drain the channel if there is anything there.
|
case <-d.requestCh: // Drain the channel if there is anything there.
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue