2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-10-28 04:25:52 +00:00
|
|
|
"sync"
|
2018-03-14 09:36:59 +00:00
|
|
|
"time"
|
2020-05-22 09:59:18 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxPoolSize = 200
|
2019-09-12 13:29:50 +00:00
|
|
|
connRetries = 3
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Discoverer is an interface that is responsible for maintaining
|
2019-02-13 18:01:10 +00:00
|
|
|
// a healthy connection pool.
|
2018-03-14 09:36:59 +00:00
|
|
|
type Discoverer interface {
|
|
|
|
BackFill(...string)
|
2020-02-24 12:39:31 +00:00
|
|
|
Close()
|
2018-03-14 09:36:59 +00:00
|
|
|
PoolCount() int
|
|
|
|
RequestRemote(int)
|
2018-03-23 20:36:59 +00:00
|
|
|
RegisterBadAddr(string)
|
2020-05-22 09:59:18 +00:00
|
|
|
RegisterGoodAddr(string, capability.Capabilities)
|
2020-01-28 16:10:13 +00:00
|
|
|
RegisterConnectedAddr(string)
|
2019-09-13 16:51:58 +00:00
|
|
|
UnregisterConnectedAddr(string)
|
2018-03-23 20:36:59 +00:00
|
|
|
UnconnectedPeers() []string
|
|
|
|
BadPeers() []string
|
2020-05-22 09:59:18 +00:00
|
|
|
GoodPeers() []AddressWithCapabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressWithCapabilities represents node address with its capabilities
|
|
|
|
type AddressWithCapabilities struct {
|
|
|
|
Address string
|
|
|
|
Capabilities capability.Capabilities
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// DefaultDiscovery default implementation of the Discoverer interface.
|
2018-03-14 09:36:59 +00:00
|
|
|
type DefaultDiscovery struct {
|
2018-03-23 20:36:59 +00:00
|
|
|
transport Transporter
|
2019-10-28 04:25:52 +00:00
|
|
|
lock sync.RWMutex
|
2020-03-10 12:40:23 +00:00
|
|
|
closeMtx sync.RWMutex
|
2018-03-23 20:36:59 +00:00
|
|
|
dialTimeout time.Duration
|
|
|
|
badAddrs map[string]bool
|
2019-09-13 16:51:58 +00:00
|
|
|
connectedAddrs map[string]bool
|
2020-05-22 09:59:18 +00:00
|
|
|
goodAddrs map[string]capability.Capabilities
|
2019-09-12 13:29:50 +00:00
|
|
|
unconnectedAddrs map[string]int
|
2020-02-24 12:39:31 +00:00
|
|
|
isDead bool
|
2018-03-23 20:36:59 +00:00
|
|
|
requestCh chan int
|
|
|
|
pool chan string
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultDiscovery returns a new DefaultDiscovery.
|
|
|
|
func NewDefaultDiscovery(dt time.Duration, ts Transporter) *DefaultDiscovery {
|
|
|
|
d := &DefaultDiscovery{
|
2018-03-23 20:36:59 +00:00
|
|
|
transport: ts,
|
|
|
|
dialTimeout: dt,
|
|
|
|
badAddrs: make(map[string]bool),
|
2019-09-13 16:51:58 +00:00
|
|
|
connectedAddrs: make(map[string]bool),
|
2020-05-22 09:59:18 +00:00
|
|
|
goodAddrs: make(map[string]capability.Capabilities),
|
2019-09-12 13:29:50 +00:00
|
|
|
unconnectedAddrs: make(map[string]int),
|
2018-03-23 20:36:59 +00:00
|
|
|
requestCh: make(chan int),
|
|
|
|
pool: make(chan string, maxPoolSize),
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
go d.run()
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// BackFill implements the Discoverer interface and will backfill the
|
|
|
|
// the pool with the given addresses.
|
|
|
|
func (d *DefaultDiscovery) BackFill(addrs ...string) {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2018-03-14 09:36:59 +00:00
|
|
|
for _, addr := range addrs {
|
2019-10-28 04:25:52 +00:00
|
|
|
if d.badAddrs[addr] || d.connectedAddrs[addr] ||
|
|
|
|
d.unconnectedAddrs[addr] > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.unconnectedAddrs[addr] = connRetries
|
|
|
|
d.pushToPoolOrDrop(addr)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Unlock()
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PoolCount returns the number of available node addresses.
|
|
|
|
func (d *DefaultDiscovery) PoolCount() int {
|
|
|
|
return len(d.pool)
|
|
|
|
}
|
|
|
|
|
2019-09-12 13:47:08 +00:00
|
|
|
// pushToPoolOrDrop tries to push address given into the pool, but if the pool
|
|
|
|
// is already full, it just drops it
|
|
|
|
func (d *DefaultDiscovery) pushToPoolOrDrop(addr string) {
|
|
|
|
select {
|
|
|
|
case d.pool <- addr:
|
2019-10-29 17:51:17 +00:00
|
|
|
updatePoolCountMetric(d.PoolCount())
|
2019-09-12 13:47:08 +00:00
|
|
|
// ok, queued
|
|
|
|
default:
|
|
|
|
// whatever
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// RequestRemote tries to establish a connection with n nodes.
|
2018-03-14 09:36:59 +00:00
|
|
|
func (d *DefaultDiscovery) RequestRemote(n int) {
|
2020-03-10 12:40:23 +00:00
|
|
|
d.closeMtx.RLock()
|
2020-02-24 12:39:31 +00:00
|
|
|
if !d.isDead {
|
|
|
|
d.requestCh <- n
|
|
|
|
}
|
2020-03-10 12:40:23 +00:00
|
|
|
d.closeMtx.RUnlock()
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// RegisterBadAddr registers the given address as a bad address.
|
|
|
|
func (d *DefaultDiscovery) RegisterBadAddr(addr string) {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
|
|
|
d.unconnectedAddrs[addr]--
|
|
|
|
if d.unconnectedAddrs[addr] > 0 {
|
|
|
|
d.pushToPoolOrDrop(addr)
|
|
|
|
} else {
|
|
|
|
d.badAddrs[addr] = true
|
|
|
|
delete(d.unconnectedAddrs, addr)
|
2020-10-13 11:16:06 +00:00
|
|
|
delete(d.goodAddrs, addr)
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
|
|
|
d.lock.Unlock()
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnconnectedPeers returns all addresses of unconnected addrs.
|
|
|
|
func (d *DefaultDiscovery) UnconnectedPeers() []string {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
2019-02-09 15:53:58 +00:00
|
|
|
addrs := make([]string, 0, len(d.unconnectedAddrs))
|
2018-03-23 20:36:59 +00:00
|
|
|
for addr := range d.unconnectedAddrs {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RUnlock()
|
2018-03-23 20:36:59 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadPeers returns all addresses of bad addrs.
|
|
|
|
func (d *DefaultDiscovery) BadPeers() []string {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
2019-02-09 15:53:58 +00:00
|
|
|
addrs := make([]string, 0, len(d.badAddrs))
|
2018-03-23 20:36:59 +00:00
|
|
|
for addr := range d.badAddrs {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RUnlock()
|
2018-03-23 20:36:59 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:51:58 +00:00
|
|
|
// GoodPeers returns all addresses of known good peers (that at least once
|
2019-10-17 09:30:24 +00:00
|
|
|
// succeeded handshaking with us).
|
2020-05-22 09:59:18 +00:00
|
|
|
func (d *DefaultDiscovery) GoodPeers() []AddressWithCapabilities {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
2020-05-22 09:59:18 +00:00
|
|
|
addrs := make([]AddressWithCapabilities, 0, len(d.goodAddrs))
|
|
|
|
for addr, cap := range d.goodAddrs {
|
|
|
|
addrs = append(addrs, AddressWithCapabilities{
|
|
|
|
Address: addr,
|
|
|
|
Capabilities: cap,
|
|
|
|
})
|
2019-09-13 16:51:58 +00:00
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RUnlock()
|
2019-09-13 16:51:58 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterGoodAddr registers good known connected address that passed
|
2019-10-17 09:30:24 +00:00
|
|
|
// handshake successfully.
|
2020-05-22 09:59:18 +00:00
|
|
|
func (d *DefaultDiscovery) RegisterGoodAddr(s string, c capability.Capabilities) {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
2020-05-22 09:59:18 +00:00
|
|
|
d.goodAddrs[s] = c
|
2020-10-13 11:16:06 +00:00
|
|
|
delete(d.badAddrs, s)
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Unlock()
|
2019-09-13 16:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnregisterConnectedAddr tells discoverer that this address is no longer
|
|
|
|
// connected, but it still is considered as good one.
|
|
|
|
func (d *DefaultDiscovery) UnregisterConnectedAddr(s string) {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
|
|
|
delete(d.connectedAddrs, s)
|
|
|
|
d.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-01-28 16:10:13 +00:00
|
|
|
// RegisterConnectedAddr tells discoverer that given address is now connected.
|
|
|
|
func (d *DefaultDiscovery) RegisterConnectedAddr(addr string) {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.Lock()
|
|
|
|
delete(d.unconnectedAddrs, addr)
|
|
|
|
d.connectedAddrs[addr] = true
|
|
|
|
d.lock.Unlock()
|
2019-09-13 16:51:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 08:59:16 +00:00
|
|
|
func (d *DefaultDiscovery) tryAddress(addr string) {
|
|
|
|
if err := d.transport.Dial(addr, d.dialTimeout); err != nil {
|
2019-10-28 04:25:52 +00:00
|
|
|
d.RegisterBadAddr(addr)
|
|
|
|
d.RequestRemote(1)
|
2019-09-13 08:59:16 +00:00
|
|
|
} else {
|
2020-01-28 16:10:13 +00:00
|
|
|
d.RegisterConnectedAddr(addr)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 12:39:31 +00:00
|
|
|
// Close stops discoverer pool processing making discoverer almost useless.
|
|
|
|
func (d *DefaultDiscovery) Close() {
|
2020-03-10 12:40:23 +00:00
|
|
|
d.closeMtx.Lock()
|
2020-02-24 12:39:31 +00:00
|
|
|
d.isDead = true
|
2020-03-10 12:40:23 +00:00
|
|
|
d.closeMtx.Unlock()
|
2020-02-24 12:39:31 +00:00
|
|
|
select {
|
|
|
|
case <-d.requestCh: // Drain the channel if there is anything there.
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(d.requestCh)
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:25:52 +00:00
|
|
|
// run is a goroutine that makes DefaultDiscovery process its queue to connect
|
|
|
|
// to other nodes.
|
|
|
|
func (d *DefaultDiscovery) run() {
|
2020-02-24 12:39:31 +00:00
|
|
|
var requested, r int
|
|
|
|
var ok bool
|
2019-09-12 13:19:18 +00:00
|
|
|
|
|
|
|
for {
|
2020-02-24 12:39:31 +00:00
|
|
|
for requested, ok = <-d.requestCh; ok && requested > 0; requested-- {
|
2019-09-12 13:19:18 +00:00
|
|
|
select {
|
2020-02-24 12:39:31 +00:00
|
|
|
case r, ok = <-d.requestCh:
|
2019-10-28 04:25:52 +00:00
|
|
|
if requested <= r {
|
|
|
|
requested = r + 1
|
2019-09-12 13:19:18 +00:00
|
|
|
}
|
|
|
|
case addr := <-d.pool:
|
2019-10-28 04:25:52 +00:00
|
|
|
d.lock.RLock()
|
|
|
|
addrIsConnected := d.connectedAddrs[addr]
|
|
|
|
d.lock.RUnlock()
|
2019-10-29 17:51:17 +00:00
|
|
|
updatePoolCountMetric(d.PoolCount())
|
2019-10-28 04:25:52 +00:00
|
|
|
if !addrIsConnected {
|
2019-09-13 16:51:58 +00:00
|
|
|
go d.tryAddress(addr)
|
|
|
|
}
|
2019-09-12 13:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 12:39:31 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-09-12 13:19:18 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|