Add timeout to grpc dial function call.

Signed-off-by: thatInfrastructureGuy <thatInfrastructureGuy@gmail.com>
pull/806/head
thatInfrastructureGuy 2020-08-31 17:19:33 -07:00
parent 8f800e9b64
commit 24eeec99c9
No known key found for this signature in database
GPG Key ID: 3E9D4A7275BC5A6A
1 changed files with 7 additions and 7 deletions

View File

@ -161,7 +161,13 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
} }
} }
conn, err := grpc.Dial(domain, grpcOption, grpc.WithBlock()) // Context will cancel the request when timeout is exceeded.
// Cancel the context when request is served within the timeout limit.
timeout := time.Duration(s.Timeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
conn, err := grpc.DialContext(ctx, domain, grpcOption, grpc.WithBlock())
if err != nil { if err != nil {
if record { if record {
RecordFailure(s, fmt.Sprintf("Dial Error %v", err), "connection") RecordFailure(s, fmt.Sprintf("Dial Error %v", err), "connection")
@ -169,12 +175,6 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
return s, err return s, err
} }
// Context will cancel the request when timeout is exceeded.
// Cancel the context when request is served within the timeout limit.
timeout := time.Duration(s.Timeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// Create a new health check client // Create a new health check client
c := healthpb.NewHealthClient(conn) c := healthpb.NewHealthClient(conn)
in := &healthpb.HealthCheckRequest{} in := &healthpb.HealthCheckRequest{}