2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
PoolCount() int
|
|
|
|
RequestRemote(int)
|
2018-03-23 20:36:59 +00:00
|
|
|
RegisterBadAddr(string)
|
2019-09-13 16:51:58 +00:00
|
|
|
RegisterGoodAddr(string)
|
|
|
|
UnregisterConnectedAddr(string)
|
2018-03-23 20:36:59 +00:00
|
|
|
UnconnectedPeers() []string
|
|
|
|
BadPeers() []string
|
2019-09-13 16:51:58 +00:00
|
|
|
GoodPeers() []string
|
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
|
|
|
|
dialTimeout time.Duration
|
|
|
|
badAddrs map[string]bool
|
2019-09-13 16:51:58 +00:00
|
|
|
connectedAddrs map[string]bool
|
|
|
|
goodAddrs map[string]bool
|
2019-09-12 13:29:50 +00:00
|
|
|
unconnectedAddrs map[string]int
|
2018-03-23 20:36:59 +00:00
|
|
|
requestCh chan int
|
|
|
|
connectedCh chan string
|
|
|
|
backFill chan string
|
|
|
|
badAddrCh chan string
|
|
|
|
pool chan string
|
2019-09-13 16:51:58 +00:00
|
|
|
goodCh chan string
|
|
|
|
unconnectedCh 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),
|
|
|
|
goodAddrs: make(map[string]bool),
|
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),
|
|
|
|
connectedCh: make(chan string),
|
2019-09-13 16:51:58 +00:00
|
|
|
goodCh: make(chan string),
|
|
|
|
unconnectedCh: make(chan string),
|
2018-03-23 20:36:59 +00:00
|
|
|
backFill: make(chan string),
|
|
|
|
badAddrCh: make(chan string),
|
|
|
|
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) {
|
|
|
|
for _, addr := range addrs {
|
|
|
|
d.backFill <- addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
// ok, queued
|
|
|
|
default:
|
|
|
|
// whatever
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// RequestRemote will try to establish a connection with n nodes.
|
2018-03-14 09:36:59 +00:00
|
|
|
func (d *DefaultDiscovery) RequestRemote(n int) {
|
|
|
|
d.requestCh <- n
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// RegisterBadAddr registers the given address as a bad address.
|
|
|
|
func (d *DefaultDiscovery) RegisterBadAddr(addr string) {
|
|
|
|
d.badAddrCh <- addr
|
|
|
|
d.RequestRemote(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnconnectedPeers returns all addresses of unconnected addrs.
|
|
|
|
func (d *DefaultDiscovery) UnconnectedPeers() []string {
|
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)
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadPeers returns all addresses of bad addrs.
|
|
|
|
func (d *DefaultDiscovery) BadPeers() []string {
|
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)
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:51:58 +00:00
|
|
|
// GoodPeers returns all addresses of known good peers (that at least once
|
|
|
|
// succeded handshaking with us).
|
|
|
|
func (d *DefaultDiscovery) GoodPeers() []string {
|
|
|
|
addrs := make([]string, 0, len(d.goodAddrs))
|
|
|
|
for addr := range d.goodAddrs {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterGoodAddr registers good known connected address that passed
|
|
|
|
// handshake successfuly.
|
|
|
|
func (d *DefaultDiscovery) RegisterGoodAddr(s string) {
|
|
|
|
d.goodCh <- s
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnregisterConnectedAddr tells discoverer that this address is no longer
|
|
|
|
// connected, but it still is considered as good one.
|
|
|
|
func (d *DefaultDiscovery) UnregisterConnectedAddr(s string) {
|
|
|
|
d.unconnectedCh <- s
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:59:16 +00:00
|
|
|
func (d *DefaultDiscovery) tryAddress(addr string) {
|
|
|
|
if err := d.transport.Dial(addr, d.dialTimeout); err != nil {
|
|
|
|
d.badAddrCh <- addr
|
|
|
|
} else {
|
|
|
|
d.connectedCh <- addr
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:59:16 +00:00
|
|
|
func (d *DefaultDiscovery) requestToWork() {
|
2019-09-12 13:19:18 +00:00
|
|
|
var requested int
|
|
|
|
|
|
|
|
for {
|
|
|
|
for requested = <-d.requestCh; requested > 0; requested-- {
|
|
|
|
select {
|
|
|
|
case r := <-d.requestCh:
|
|
|
|
if requested < r {
|
|
|
|
requested = r
|
|
|
|
}
|
|
|
|
case addr := <-d.pool:
|
2019-09-13 16:51:58 +00:00
|
|
|
if !d.connectedAddrs[addr] {
|
|
|
|
go d.tryAddress(addr)
|
|
|
|
}
|
2019-09-12 13:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultDiscovery) run() {
|
2019-09-13 08:59:16 +00:00
|
|
|
go d.requestToWork()
|
2018-03-14 09:36:59 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case addr := <-d.backFill:
|
2019-09-13 16:51:58 +00:00
|
|
|
if d.badAddrs[addr] || d.connectedAddrs[addr] ||
|
|
|
|
d.unconnectedAddrs[addr] > 0 {
|
2018-03-14 09:36:59 +00:00
|
|
|
break
|
|
|
|
}
|
2019-09-13 16:51:58 +00:00
|
|
|
d.unconnectedAddrs[addr] = connRetries
|
|
|
|
d.pushToPoolOrDrop(addr)
|
2018-03-23 20:36:59 +00:00
|
|
|
case addr := <-d.badAddrCh:
|
2019-09-12 13:29:50 +00:00
|
|
|
d.unconnectedAddrs[addr]--
|
|
|
|
if d.unconnectedAddrs[addr] > 0 {
|
2019-09-12 13:47:08 +00:00
|
|
|
d.pushToPoolOrDrop(addr)
|
2019-09-12 13:29:50 +00:00
|
|
|
} else {
|
|
|
|
d.badAddrs[addr] = true
|
|
|
|
delete(d.unconnectedAddrs, addr)
|
|
|
|
}
|
2019-09-12 13:19:18 +00:00
|
|
|
d.RequestRemote(1)
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
case addr := <-d.connectedCh:
|
|
|
|
delete(d.unconnectedAddrs, addr)
|
2019-09-13 16:51:58 +00:00
|
|
|
if !d.connectedAddrs[addr] {
|
|
|
|
d.connectedAddrs[addr] = true
|
|
|
|
}
|
|
|
|
case addr := <-d.goodCh:
|
|
|
|
if !d.goodAddrs[addr] {
|
|
|
|
d.goodAddrs[addr] = true
|
|
|
|
}
|
|
|
|
case addr := <-d.unconnectedCh:
|
|
|
|
delete(d.connectedAddrs, addr)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|