prysm-pulse/beacon-chain/p2p/watch_peers.go
Victor Farazdagi c35bdf2649
Refactors calls to deprecated libp2p methods (#6554)
* refactors calls to deprecated IDB58Decode()
* Merge branch 'master' into p2p-refactor-deprecated
* updated packages
* gazelle
* mod tidy
* refactors publish()/subscribe() deprecated methods
* gofmt
* test update join/leave topic methods
* re-arrange imports
* Merge branch 'master' into p2p-refactor-deprecated
* Merge refs/heads/master into p2p-refactor-deprecated
2020-07-13 02:28:40 +00:00

37 lines
901 B
Go

package p2p
import (
"context"
"github.com/libp2p/go-libp2p-core/host"
)
// 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, maxDialTimeout)
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
}
}
}
}