Use grpc.NewClient instead of deprecated ones

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko 2024-05-21 10:48:24 -07:00
parent a5be629936
commit 63b4688175
5 changed files with 8 additions and 28 deletions

View File

@ -115,17 +115,14 @@ func New(address string, opts ...Opt) (*Client, error) {
} }
if address != "" { if address != "" {
backoffConfig := backoff.DefaultConfig backoffConfig := backoff.DefaultConfig
backoffConfig.MaxDelay = 3 * time.Second backoffConfig.MaxDelay = copts.timeout
connParams := grpc.ConnectParams{ connParams := grpc.ConnectParams{
Backoff: backoffConfig, Backoff: backoffConfig,
} }
gopts := []grpc.DialOption{ gopts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.FailOnNonTempDialError(true),
grpc.WithConnectParams(connParams), grpc.WithConnectParams(connParams),
grpc.WithContextDialer(dialer.ContextDialer), grpc.WithContextDialer(dialer.ContextDialer),
grpc.WithReturnConnectionError(),
} }
if len(copts.dialOptions) > 0 { if len(copts.dialOptions) > 0 {
gopts = copts.dialOptions gopts = copts.dialOptions
@ -143,9 +140,7 @@ func New(address string, opts ...Opt) (*Client, error) {
} }
connector := func() (*grpc.ClientConn, error) { connector := func() (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), copts.timeout) conn, err := grpc.NewClient(dialer.DialAddress(address), gopts...)
defer cancel()
conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to dial %q: %w", address, err) return nil, fmt.Errorf("failed to dial %q: %w", address, err)
} }

View File

@ -100,15 +100,11 @@ func connect(address string, d func(context.Context, string) (net.Conn, error))
Backoff: backoffConfig, Backoff: backoffConfig,
} }
gopts := []grpc.DialOption{ gopts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(d), grpc.WithContextDialer(d),
grpc.FailOnNonTempDialError(true),
grpc.WithConnectParams(connParams), grpc.WithConnectParams(connParams),
} }
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) conn, err := grpc.NewClient(dialer.DialAddress(address), gopts...)
defer cancel()
conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to dial %q: %w", address, err) return nil, fmt.Errorf("failed to dial %q: %w", address, err)
} }

View File

@ -587,7 +587,7 @@ func (pc *proxyClients) getClient(address string) (*grpc.ClientConn, error) {
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)), grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)),
} }
conn, err := grpc.Dial(dialer.DialAddress(address), gopts...) conn, err := grpc.NewClient(dialer.DialAddress(address), gopts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to dial %q: %w", address, err) return nil, fmt.Errorf("failed to dial %q: %w", address, err)
} }

View File

@ -275,14 +275,10 @@ func makeConnection(ctx context.Context, id string, params client.BootstrapParam
return ttrpc.NewClient(conn, ttrpc.WithOnClose(onClose)), nil return ttrpc.NewClient(conn, ttrpc.WithOnClose(onClose)), nil
case "grpc": case "grpc":
ctx, cancel := context.WithTimeout(ctx, time.Second*100)
defer cancel()
gopts := []grpc.DialOption{ gopts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
} }
return grpcDialContext(ctx, params.Address, onClose, gopts...) return grpcDialContext(params.Address, onClose, gopts...)
default: default:
return nil, fmt.Errorf("unexpected protocol: %q", params.Protocol) return nil, fmt.Errorf("unexpected protocol: %q", params.Protocol)
} }
@ -292,7 +288,6 @@ func makeConnection(ctx context.Context, id string, params client.BootstrapParam
// so we can have something similar to ttrpc.WithOnClose to have // so we can have something similar to ttrpc.WithOnClose to have
// a callback run when the connection is severed or explicitly closed. // a callback run when the connection is severed or explicitly closed.
func grpcDialContext( func grpcDialContext(
ctx context.Context,
address string, address string,
onClose func(), onClose func(),
gopts ...grpc.DialOption, gopts ...grpc.DialOption,
@ -316,7 +311,7 @@ func grpcDialContext(
conn.Close() conn.Close()
target := dialer.DialAddress(address) target := dialer.DialAddress(address)
client, err := grpc.DialContext(ctx, target, gopts...) client, err := grpc.NewClient(target, gopts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create GRPC connection: %w", err) return nil, fmt.Errorf("failed to create GRPC connection: %w", err)
} }

View File

@ -245,14 +245,8 @@ func openStream(ctx context.Context, urlStr string) (streamingapi.Stream, error)
return stream, nil return stream, nil
case "grpc": case "grpc":
ctx, cancel := context.WithTimeout(ctx, time.Second*100) gopts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
defer cancel() conn, err := grpc.NewClient(realAddress, gopts...)
gopts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}
conn, err := grpc.DialContext(ctx, realAddress, gopts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }