fix: set concurrent dials to 5

This commit is contained in:
Richard Ramos 2023-07-06 10:19:51 -04:00 committed by richΛrd
parent dc08c44804
commit 88d52d6417

View File

@ -245,8 +245,18 @@ func (c *PeerConnectionStrategy) workPublisher(ctx context.Context) {
}
}
const maxActiveDials = 5
func (c *PeerConnectionStrategy) dialPeers(ctx context.Context) {
defer c.wg.Done()
maxGoRoutines := c.minPeers
if maxGoRoutines > maxActiveDials {
maxGoRoutines = maxActiveDials
}
sem := make(chan struct{}, maxGoRoutines)
for {
select {
case pi, ok := <-c.dialCh:
@ -281,13 +291,19 @@ func (c *PeerConnectionStrategy) dialPeers(ctx context.Context) {
}
c.mux.Unlock()
dialCtx, dialCtxCancel := context.WithTimeout(c.workerCtx, c.dialTimeout)
err := c.host.Connect(dialCtx, pi)
sem <- struct{}{}
c.wg.Add(1)
go func(pi peer.AddrInfo) {
defer c.wg.Done()
ctx, cancel := context.WithTimeout(c.workerCtx, c.dialTimeout)
defer cancel()
err := c.host.Connect(ctx, pi)
if err != nil && !errors.Is(err, context.Canceled) {
c.host.Peerstore().(peers.WakuPeerstore).AddConnFailure(pi)
c.logger.Info("connecting to peer", logging.HostID("peerID", pi.ID), zap.Error(err))
}
dialCtxCancel()
<-sem
}(pi)
case <-ctx.Done():
return