From 8f3207466791f6c21d6e3837858560cff12dd143 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 5 Mar 2024 15:13:25 -0500 Subject: [PATCH] refactor: switch ConnectToNode to use a retries counter and make it configurable via P2PConfig --- config/config.go | 1 + service/default/mediator.go | 2 +- service/default/p2p.go | 10 +++++----- service/p2p.go | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index c3019f6..5483727 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ type P2PConfig struct { Network string `mapstructure:"network"` Peers PeersConfig `mapstructure:"peers"` MaxOutgoingPeerFailures uint `mapstructure:"max_outgoing_peer_failures"` + MaxConnectionAttempts uint `mapstructure:"max_connection_attempts"` } type PeersConfig struct { diff --git a/service/default/mediator.go b/service/default/mediator.go index fee7b76..26775dd 100644 --- a/service/default/mediator.go +++ b/service/default/mediator.go @@ -66,7 +66,7 @@ func (m MediatorDefault) RegistryGet(pk []byte) (protocol.SignedRegistryEntry, e } func (m MediatorDefault) ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error { - return m.Services().P2P().ConnectToNode(connectionUris, retried, fromPeer) + return m.Services().P2P().ConnectToNode(connectionUris, 0, fromPeer) } func (m MediatorDefault) ServicesStarted() bool { diff --git a/service/default/p2p.go b/service/default/p2p.go index a4aff50..44bc438 100644 --- a/service/default/p2p.go +++ b/service/default/p2p.go @@ -98,7 +98,7 @@ func (p *P2PServiceDefault) Start(ctx context.Context) error { peer := peer go func() { - err := p.ConnectToNode([]*url.URL{u}, false, nil) + err := p.ConnectToNode([]*url.URL{u}, 0, nil) if err != nil { p.Logger().Error("failed to connect to initial peer", zap.Error(err), zap.String("peer", peer)) } @@ -129,7 +129,7 @@ func (p *P2PServiceDefault) Init(ctx context.Context) error { return nil } -func (p *P2PServiceDefault) ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error { +func (p *P2PServiceDefault) ConnectToNode(connectionUris []*url.URL, retry uint, fromPeer net.Peer) error { if !p.Services().IsStarted() { if !p.Services().IsStarting() { return nil @@ -237,7 +237,7 @@ func (p *P2PServiceDefault) ConnectToNode(connectionUris []*url.URL, retried boo socket, err := net.CreateTransportSocket(scheme, connectionUri) if err != nil { - if retried { + if retry >= p.Config().P2P.MaxConnectionAttempts { p.Logger().Error("failed to connect, too many retries", zap.String("node", connectionUri.String()), zap.Error(err)) counter := uint(0) if p.outgoingPeerFailures.Contains(idString) { @@ -291,7 +291,7 @@ func (p *P2PServiceDefault) ConnectToNode(connectionUris []*url.URL, retried boo return err } - retried = true + retry++ p.Logger().Error("failed to connect", zap.String("node", connectionUri.String()), zap.Error(err)) @@ -305,7 +305,7 @@ func (p *P2PServiceDefault) ConnectToNode(connectionUris []*url.URL, retried boo time.Sleep(time.Duration(delayDeref) * time.Second) - return p.ConnectToNode(connectionUris, retried, fromPeer) + return p.ConnectToNode(connectionUris, retry, fromPeer) } if p.outgoingPeerFailures.Contains(idString) { diff --git a/service/p2p.go b/service/p2p.go index a4826df..74d4586 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -12,7 +12,7 @@ import ( type P2PService interface { SelfConnectionUris() []*url.URL Peers() structs.Map - ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error + ConnectToNode(connectionUris []*url.URL, retry uint, fromPeer net.Peer) error OnNewPeer(peer net.Peer, verifyId bool) error GetNodeScore(nodeId *encoding.NodeId) (float64, error) SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error)