From f13217670a2de38e7baefef7c99d34343fe64669 Mon Sep 17 00:00:00 2001 From: Maria Ines Parnisari Date: Sun, 25 Feb 2024 20:23:56 -0300 Subject: [PATCH] put cap of 1 second to NewPeerClient connect --- gubernator.go | 2 ++ peer_client.go | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/gubernator.go b/gubernator.go index b8f18601..0a4701e5 100644 --- a/gubernator.go +++ b/gubernator.go @@ -610,6 +610,7 @@ func (s *V1Instance) SetPeers(peerInfo []PeerInfo) { }) if err != nil { s.log.Error("error connecting to peer: %s", err) + return } } regionPicker.Add(peer) @@ -628,6 +629,7 @@ func (s *V1Instance) SetPeers(peerInfo []PeerInfo) { }) if err != nil { s.log.Error("error connecting to peer: %s", err) + return } } localPicker.Add(peer) diff --git a/peer_client.go b/peer_client.go index 36a60f08..e7c40a4c 100644 --- a/peer_client.go +++ b/peer_client.go @@ -22,6 +22,7 @@ import ( "fmt" "sync" "sync/atomic" + "time" "github.com/mailgun/holster/v4/clock" "github.com/mailgun/holster/v4/collections" @@ -79,7 +80,7 @@ type PeerConfig struct { TraceGRPC bool } -// NewPeerClient establishes a connection to a peer in a blocking fashion. +// NewPeerClient tries to establish a connection to a peer in a blocking fashion with a 1 second timeout. // If batching is enabled, it also starts a goroutine where batches will be processed. func NewPeerClient(conf PeerConfig) (*PeerClient, error) { peerClient := &PeerClient{ @@ -101,8 +102,11 @@ func NewPeerClient(conf PeerConfig) (*PeerClient, error) { opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) } + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + var err error - peerClient.conn, err = grpc.Dial(conf.Info.GRPCAddress, opts...) + peerClient.conn, err = grpc.DialContext(ctx, conf.Info.GRPCAddress, opts...) if err != nil { return nil, err }