mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
c69b3f568e
* Move hard-coded constants to global consts * Fixes * Merge branch 'master' into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Comments * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge refs/heads/master into remove-hard-constants * Merge branch 'master' of github.com:prysmaticlabs/prysm into remove-hard-constants
40 lines
942 B
Go
40 lines
942 B
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
|
)
|
|
|
|
const timeoutMax = 30 * time.Second
|
|
|
|
// ensurePeerConnections will attempt to reestablish connection to the peers
|
|
// if there are currently no connections to that peer.
|
|
func ensurePeerConnections(ctx context.Context, h host.Host, peers ...string) {
|
|
if len(peers) == 0 {
|
|
return
|
|
}
|
|
for _, p := range peers {
|
|
if p == "" {
|
|
continue
|
|
}
|
|
peer, err := MakePeer(p)
|
|
if err != nil {
|
|
log.Errorf("Could not make peer: %v", err)
|
|
continue
|
|
}
|
|
|
|
c := h.Network().ConnsToPeer(peer.ID)
|
|
if len(c) == 0 {
|
|
log.WithField("peer", peer.ID).Debug("No connections to peer, reconnecting")
|
|
ctx, cancel := context.WithTimeout(ctx, timeoutMax)
|
|
defer cancel()
|
|
if err := h.Connect(ctx, *peer); err != nil {
|
|
log.WithField("peer", peer.ID).WithField("addrs", peer.Addrs).WithError(err).Errorf("Failed to reconnect to peer")
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|