Skip to content

Commit

Permalink
* Re-added insecure function and corrected the logic
Browse files Browse the repository at this point in the history
* Added more information for connection errors
* Replaced deprecated functions
  • Loading branch information
nitinatgh committed Apr 6, 2023
1 parent 70a2104 commit 7b9dfd8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions internal/pkg/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package grpc
import (
"bytes"
"fmt"
"google.golang.org/grpc/credentials/insecure"
"log"
"mittens/internal/pkg/placeholders"
"mittens/internal/pkg/response"
Expand Down Expand Up @@ -61,8 +62,14 @@ func (c *Client) Connect(headers []string) error {
headersMetadata := grpcurl.MetadataFromHeaders(headers)
contextWithMetadata := metadata.NewOutgoingContext(ctx, headersMetadata)

dialOptions := []grpc.DialOption{grpc.WithBlock()}
// grpc.WithReturnConnectionError() is EXPERIMENTAL and may be changed or removed in a later release
// Added to provide more information if a connection error occurs
dialOptions := []grpc.DialOption{grpc.WithBlock(), grpc.WithReturnConnectionError()}
if c.insecure {
log.Print("ignoring gRPC server SSL/TLS authentication")
dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
log.Print("using gRPC server SSL/TLS authentication")
tlsConf, err := grpcurl.ClientTLSConfig(c.insecure, "", "", "")
if err != nil {
return fmt.Errorf("failed to create TLS config: %v", err)
Expand All @@ -71,12 +78,13 @@ func (c *Client) Connect(headers []string) error {
dialOptions = append(dialOptions, grpc.WithTransportCredentials(creds))
}

grpc.WithReturnConnectionError()
conn, err := grpc.DialContext(ctx, c.host, dialOptions...)
if err != nil {
return fmt.Errorf("gRPC dial: %v", err)
}

reflectionClient := grpcreflect.NewClient(contextWithMetadata, reflectpb.NewServerReflectionClient(conn))
reflectionClient := grpcreflect.NewClientV1Alpha(contextWithMetadata, reflectpb.NewServerReflectionClient(conn))
descriptorSource := grpcurl.DescriptorSourceFromServer(contextWithMetadata, reflectionClient)

log.Print("gRPC client connected")
Expand Down

0 comments on commit 7b9dfd8

Please sign in to comment.