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
}
func (d *DefaultDiscovery) work(addrCh chan string) {
for {
addr := <-addrCh
func (d *DefaultDiscovery) tryAddress(addr string) {
if err := d.transport.Dial(addr, d.dialTimeout); err != nil {
d.badAddrCh <- addr
} else {
d.connectedCh <- addr
}
}
}
func (d *DefaultDiscovery) requestToWork(workCh chan string) {
func (d *DefaultDiscovery) requestToWork() {
var requested int
for {
@ -127,23 +124,14 @@ func (d *DefaultDiscovery) requestToWork(workCh chan string) {
requested = r
}
case addr := <-d.pool:
workCh <- addr
go d.tryAddress(addr)
}
}
}
}
func (d *DefaultDiscovery) run() {
var (
maxWorkers = 5
workCh = make(chan string)
)
for i := 0; i < maxWorkers; i++ {
go d.work(workCh)
}
go d.requestToWork(workCh)
go d.requestToWork()
for {
select {
case addr := <-d.backFill: