network: start worker goroutine for every connection attempts

Prevents blocking on write to workCh which can be dangerous for the server.
This commit is contained in:
Roman Khimov 2019-09-13 11:59:16 +03:00
parent b4e284f301
commit 2a49e68d77

View file

@ -105,18 +105,15 @@ func (d *DefaultDiscovery) BadPeers() []string {
return addrs return addrs
} }
func (d *DefaultDiscovery) work(addrCh chan string) { func (d *DefaultDiscovery) tryAddress(addr string) {
for {
addr := <-addrCh
if err := d.transport.Dial(addr, d.dialTimeout); err != nil { if err := d.transport.Dial(addr, d.dialTimeout); err != nil {
d.badAddrCh <- addr d.badAddrCh <- addr
} else { } else {
d.connectedCh <- addr d.connectedCh <- addr
} }
} }
}
func (d *DefaultDiscovery) requestToWork(workCh chan string) { func (d *DefaultDiscovery) requestToWork() {
var requested int var requested int
for { for {
@ -127,23 +124,14 @@ func (d *DefaultDiscovery) requestToWork(workCh chan string) {
requested = r requested = r
} }
case addr := <-d.pool: case addr := <-d.pool:
workCh <- addr go d.tryAddress(addr)
} }
} }
} }
} }
func (d *DefaultDiscovery) run() { func (d *DefaultDiscovery) run() {
var ( go d.requestToWork()
maxWorkers = 5
workCh = make(chan string)
)
for i := 0; i < maxWorkers; i++ {
go d.work(workCh)
}
go d.requestToWork(workCh)
for { for {
select { select {
case addr := <-d.backFill: case addr := <-d.backFill: