refactor: connect to discovered peers linearly

This commit is contained in:
Richard Ramos 2023-06-30 11:26:35 -04:00 committed by richΛrd
parent dbd94ebb81
commit 6d9018cb11
1 changed files with 7 additions and 20 deletions

View File

@ -250,13 +250,6 @@ func (c *PeerConnectionStrategy) workPublisher(ctx context.Context) {
func (c *PeerConnectionStrategy) dialPeers(ctx context.Context) { func (c *PeerConnectionStrategy) dialPeers(ctx context.Context) {
defer c.wg.Done() defer c.wg.Done()
maxGoRoutines := c.minPeers
if maxGoRoutines > 15 {
maxGoRoutines = 15
}
sem := make(chan struct{}, maxGoRoutines)
for { for {
select { select {
case pi, ok := <-c.dialCh: case pi, ok := <-c.dialCh:
@ -291,20 +284,14 @@ func (c *PeerConnectionStrategy) dialPeers(ctx context.Context) {
continue continue
} }
sem <- struct{}{} dialCtx, dialCtxCancel := context.WithTimeout(c.workerCtx, c.dialTimeout)
c.wg.Add(1) err := c.host.Connect(dialCtx, pi)
go func(pi peer.AddrInfo) { if err != nil && !errors.Is(err, context.Canceled) {
defer c.wg.Done() c.host.Peerstore().(peers.WakuPeerstore).AddConnFailure(pi)
c.logger.Info("connecting to peer", logging.HostID("peerID", pi.ID), zap.Error(err))
}
dialCtxCancel()
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))
}
<-sem
}(pi)
case <-ctx.Done(): case <-ctx.Done():
return return
} }