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:
parent
b4e284f301
commit
2a49e68d77
1 changed files with 8 additions and 20 deletions
|
@ -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 {
|
if err := d.transport.Dial(addr, d.dialTimeout); err != nil {
|
||||||
addr := <-addrCh
|
d.badAddrCh <- addr
|
||||||
if err := d.transport.Dial(addr, d.dialTimeout); err != nil {
|
} else {
|
||||||
d.badAddrCh <- addr
|
d.connectedCh <- addr
|
||||||
} else {
|
|
||||||
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:
|
||||||
|
|
Loading…
Reference in a new issue