Skip to content

cleanup: replace dial with newclient #8196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 12, 2025
4 changes: 2 additions & 2 deletions Documentation/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ As a reminder, all `CallOption`s may be converted into `DialOption`s that become
the default for all RPCs sent through a client using `grpc.WithDefaultCallOptions`:

```go
myclient := grpc.Dial(ctx, target, grpc.WithDefaultCallOptions(grpc.CallContentSubtype("mycodec")))
myclient := grpc.NewClient(target, grpc.WithDefaultCallOptions(grpc.CallContentSubtype("mycodec")))
```

When specified in either of these ways, messages will be encoded using this
Expand Down Expand Up @@ -132,7 +132,7 @@ As a reminder, all `CallOption`s may be converted into `DialOption`s that become
the default for all RPCs sent through a client using `grpc.WithDefaultCallOptions`:

```go
myclient := grpc.Dial(ctx, target, grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")))
myclient := grpc.NewClient(target, grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")))
```

When specified in either of these ways, messages will be compressed using this
Expand Down
6 changes: 3 additions & 3 deletions Documentation/grpc-auth-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ As outlined in the [gRPC authentication guide](https://grpc.io/docs/guides/auth.
# Enabling TLS on a gRPC client

```Go
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
```

# Enabling TLS on a gRPC server
Expand Down Expand Up @@ -63,7 +63,7 @@ to prevent any insecure transmission of tokens.
## Google Compute Engine (GCE)

```Go
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
```

## JWT
Expand All @@ -73,6 +73,6 @@ jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthS
if err != nil {
log.Fatalf("Failed to create JWT credentials: %v", err)
}
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(jwtCreds))
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(jwtCreds))
```

5 changes: 2 additions & 3 deletions balancer/endpointsharding/endpointsharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -166,7 +165,7 @@ func (s) TestEndpointShardingBasic(t *testing.T) {
}
cc, err := grpc.NewClient(mr.Scheme()+":///", dOpts...)
if err != nil {
log.Fatalf("Failed to create new client: %v", err)
t.Fatalf("Failed to create new client: %v", err)
}
defer cc.Close()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down Expand Up @@ -246,7 +245,7 @@ func (s) TestEndpointShardingReconnectDisabled(t *testing.T) {

cc, err := grpc.NewClient(mr.Scheme()+":///", grpc.WithResolvers(mr), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Failed to create new client: %v", err)
t.Fatalf("Failed to create new client: %v", err)
}
defer cc.Close()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down
2 changes: 1 addition & 1 deletion credentials/insecure/insecure.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// NewCredentials returns a credentials which disables transport security.
//
// Note that using this credentials with per-RPC credentials which require
// transport security is incompatible and will cause grpc.Dial() to fail.
// transport security is incompatible and will cause RPCs to fail.
func NewCredentials() credentials.TransportCredentials {
return insecureTC{}
}
Expand Down
2 changes: 1 addition & 1 deletion dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func WithReturnConnectionError() DialOption {
//
// Note that using this DialOption with per-RPC credentials (through
// WithCredentialsBundle or WithPerRPCCredentials) which require transport
// security is incompatible and will cause grpc.Dial() to fail.
// security is incompatible and will cause RPCs to fail.
//
// Deprecated: use WithTransportCredentials and insecure.NewCredentials()
// instead. Will be supported throughout 1.x.
Expand Down
3 changes: 1 addition & 2 deletions internal/stats/metrics_recorder_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package stats_test
import (
"context"
"fmt"
"log"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -154,7 +153,7 @@ func (s) TestMetricsRecorderList(t *testing.T) {

cc, err := grpc.NewClient(mr.Scheme()+":///", grpc.WithResolvers(mr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(mr2))
if err != nil {
log.Fatalf("Failed to dial: %v", err)
t.Fatalf("grpc.NewClient() failed: %v", err)
}
defer cc.Close()

Expand Down
27 changes: 14 additions & 13 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/grpctest"
Expand Down Expand Up @@ -268,13 +269,12 @@ func (te *test) startServer(ts testgrpc.TestServiceServer) {
te.srvAddr = lis.Addr().String()
}

func (te *test) clientConn() *grpc.ClientConn {
func (te *test) clientConn(ctx context.Context) *grpc.ClientConn {
if te.cc != nil {
return te.cc
}
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithUserAgent("test/0.0.1"),
}
if te.compress == "gzip" {
Expand All @@ -288,10 +288,12 @@ func (te *test) clientConn() *grpc.ClientConn {
}

var err error
te.cc, err = grpc.Dial(te.srvAddr, opts...)
te.cc, err = grpc.NewClient(te.srvAddr, opts...)
if err != nil {
te.t.Fatalf("Dial(%q) = %v", te.srvAddr, err)
te.t.Fatalf("grpc.NewClient(%q) failed: %v", te.srvAddr, err)
}
te.cc.Connect()
testutils.AwaitState(ctx, te.t, te.cc, connectivity.Ready)
return te.cc
}

Expand All @@ -317,15 +319,15 @@ func (te *test) doUnaryCall(c *rpcConfig) (*testpb.SimpleRequest, *testpb.Simple
req *testpb.SimpleRequest
err error
)
tc := testgrpc.NewTestServiceClient(te.clientConn())
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
if c.success {
req = &testpb.SimpleRequest{Payload: idToPayload(errorID + 1)}
} else {
req = &testpb.SimpleRequest{Payload: idToPayload(errorID)}
}

tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
resp, err = tc.UnaryCall(metadata.NewOutgoingContext(tCtx, testMetadata), req, grpc.WaitForReady(!c.failfast))
return req, resp, err
}
Expand All @@ -336,9 +338,9 @@ func (te *test) doFullDuplexCallRoundtrip(c *rpcConfig) ([]proto.Message, []prot
resps []proto.Message
err error
)
tc := testgrpc.NewTestServiceClient(te.clientConn())
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
stream, err := tc.FullDuplexCall(metadata.NewOutgoingContext(tCtx, testMetadata), grpc.WaitForReady(!c.failfast))
if err != nil {
return reqs, resps, err
Expand Down Expand Up @@ -377,9 +379,9 @@ func (te *test) doClientStreamCall(c *rpcConfig) ([]proto.Message, *testpb.Strea
resp *testpb.StreamingInputCallResponse
err error
)
tc := testgrpc.NewTestServiceClient(te.clientConn())
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
stream, err := tc.StreamingInputCall(metadata.NewOutgoingContext(tCtx, testMetadata), grpc.WaitForReady(!c.failfast))
if err != nil {
return reqs, resp, err
Expand Down Expand Up @@ -407,16 +409,15 @@ func (te *test) doServerStreamCall(c *rpcConfig) (*testpb.StreamingOutputCallReq
resps []proto.Message
err error
)

tc := testgrpc.NewTestServiceClient(te.clientConn())
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))

var startID int32
if !c.success {
startID = errorID
}
req = &testpb.StreamingOutputCallRequest{Payload: idToPayload(startID)}
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
stream, err := tc.StreamingOutputCall(metadata.NewOutgoingContext(tCtx, testMetadata), req, grpc.WaitForReady(!c.failfast))
if err != nil {
return req, resps, err
Expand Down
6 changes: 3 additions & 3 deletions test/channelz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,11 @@ func (s) TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
}
}

func (s) TestCZTopChannelRegistrationAndDeletionWhenDialFail(t *testing.T) {
// Make dial fails (due to no transport security specified)
func (s) TestCZTopChannelRegistrationAndDeletionWhenNewClientFail(t *testing.T) {
// Make newclient fails (due to no transport security specified)
_, err := grpc.NewClient("fake.addr")
if err == nil {
t.Fatal("expecting dial to fail")
t.Fatal("expecting newclient to fail")
}
if tcs, end := channelz.GetTopChannels(0, 0); tcs != nil || !end {
t.Fatalf("GetTopChannels(0, 0) = %v, %v, want <nil>, true", tcs, end)
Expand Down
5 changes: 3 additions & 2 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,11 @@ func (te *test) clientConn(opts ...grpc.DialOption) *grpc.ClientConn {
var scheme string
opts, scheme = te.configDial(opts...)
var err error
te.cc, err = grpc.Dial(scheme+te.srvAddr, opts...)
te.cc, err = grpc.NewClient(scheme+te.srvAddr, opts...)
if err != nil {
te.t.Fatalf("Dial(%q) = %v", scheme+te.srvAddr, err)
te.t.Fatalf("grpc.NewClient(%q) failed: %v", scheme+te.srvAddr, err)
}
te.cc.Connect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not actionable: Normally I would suggest moving the Connect() call to the calling functions because most of the callers wouldn't need it as they're making RPCs. In this case, there are 116 references, so we can keep the existing behaviour.

return te.cc
}

Expand Down