Skip to content

Commit d3d2702

Browse files
cleanup: replace dial with newclient (#8196)
1 parent d46d6d8 commit d3d2702

File tree

9 files changed

+30
-30
lines changed

9 files changed

+30
-30
lines changed

Documentation/encoding.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ As a reminder, all `CallOption`s may be converted into `DialOption`s that become
5757
the default for all RPCs sent through a client using `grpc.WithDefaultCallOptions`:
5858

5959
```go
60-
myclient := grpc.Dial(ctx, target, grpc.WithDefaultCallOptions(grpc.CallContentSubtype("mycodec")))
60+
myclient := grpc.NewClient(target, grpc.WithDefaultCallOptions(grpc.CallContentSubtype("mycodec")))
6161
```
6262

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

134134
```go
135-
myclient := grpc.Dial(ctx, target, grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")))
135+
myclient := grpc.NewClient(target, grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")))
136136
```
137137

138138
When specified in either of these ways, messages will be compressed using this

Documentation/grpc-auth-support.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ As outlined in the [gRPC authentication guide](https://grpc.io/docs/guides/auth.
55
# Enabling TLS on a gRPC client
66

77
```Go
8-
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
8+
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
99
```
1010

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

6565
```Go
66-
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
66+
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
6767
```
6868

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

balancer/endpointsharding/endpointsharding_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/json"
2424
"errors"
2525
"fmt"
26-
"log"
2726
"strings"
2827
"testing"
2928
"time"
@@ -166,7 +165,7 @@ func (s) TestEndpointShardingBasic(t *testing.T) {
166165
}
167166
cc, err := grpc.NewClient(mr.Scheme()+":///", dOpts...)
168167
if err != nil {
169-
log.Fatalf("Failed to create new client: %v", err)
168+
t.Fatalf("Failed to create new client: %v", err)
170169
}
171170
defer cc.Close()
172171
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
@@ -246,7 +245,7 @@ func (s) TestEndpointShardingReconnectDisabled(t *testing.T) {
246245

247246
cc, err := grpc.NewClient(mr.Scheme()+":///", grpc.WithResolvers(mr), grpc.WithTransportCredentials(insecure.NewCredentials()))
248247
if err != nil {
249-
log.Fatalf("Failed to create new client: %v", err)
248+
t.Fatalf("Failed to create new client: %v", err)
250249
}
251250
defer cc.Close()
252251
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)

credentials/insecure/insecure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
// NewCredentials returns a credentials which disables transport security.
3131
//
3232
// Note that using this credentials with per-RPC credentials which require
33-
// transport security is incompatible and will cause grpc.Dial() to fail.
33+
// transport security is incompatible and will cause RPCs to fail.
3434
func NewCredentials() credentials.TransportCredentials {
3535
return insecureTC{}
3636
}

dialoptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func WithReturnConnectionError() DialOption {
360360
//
361361
// Note that using this DialOption with per-RPC credentials (through
362362
// WithCredentialsBundle or WithPerRPCCredentials) which require transport
363-
// security is incompatible and will cause grpc.Dial() to fail.
363+
// security is incompatible and will cause RPCs to fail.
364364
//
365365
// Deprecated: use WithTransportCredentials and insecure.NewCredentials()
366366
// instead. Will be supported throughout 1.x.

internal/stats/metrics_recorder_list_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ package stats_test
2323
import (
2424
"context"
2525
"fmt"
26-
"log"
2726
"strings"
2827
"testing"
2928
"time"
@@ -154,7 +153,7 @@ func (s) TestMetricsRecorderList(t *testing.T) {
154153

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

stats/stats_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/google/go-cmp/cmp"
3232
"google.golang.org/grpc"
33+
"google.golang.org/grpc/connectivity"
3334
"google.golang.org/grpc/credentials/insecure"
3435
"google.golang.org/grpc/internal"
3536
"google.golang.org/grpc/internal/grpctest"
@@ -268,13 +269,12 @@ func (te *test) startServer(ts testgrpc.TestServiceServer) {
268269
te.srvAddr = lis.Addr().String()
269270
}
270271

271-
func (te *test) clientConn() *grpc.ClientConn {
272+
func (te *test) clientConn(ctx context.Context) *grpc.ClientConn {
272273
if te.cc != nil {
273274
return te.cc
274275
}
275276
opts := []grpc.DialOption{
276277
grpc.WithTransportCredentials(insecure.NewCredentials()),
277-
grpc.WithBlock(),
278278
grpc.WithUserAgent("test/0.0.1"),
279279
}
280280
if te.compress == "gzip" {
@@ -288,10 +288,12 @@ func (te *test) clientConn() *grpc.ClientConn {
288288
}
289289

290290
var err error
291-
te.cc, err = grpc.Dial(te.srvAddr, opts...)
291+
te.cc, err = grpc.NewClient(te.srvAddr, opts...)
292292
if err != nil {
293-
te.t.Fatalf("Dial(%q) = %v", te.srvAddr, err)
293+
te.t.Fatalf("grpc.NewClient(%q) failed: %v", te.srvAddr, err)
294294
}
295+
te.cc.Connect()
296+
testutils.AwaitState(ctx, te.t, te.cc, connectivity.Ready)
295297
return te.cc
296298
}
297299

@@ -317,15 +319,15 @@ func (te *test) doUnaryCall(c *rpcConfig) (*testpb.SimpleRequest, *testpb.Simple
317319
req *testpb.SimpleRequest
318320
err error
319321
)
320-
tc := testgrpc.NewTestServiceClient(te.clientConn())
322+
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
323+
defer cancel()
324+
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
321325
if c.success {
322326
req = &testpb.SimpleRequest{Payload: idToPayload(errorID + 1)}
323327
} else {
324328
req = &testpb.SimpleRequest{Payload: idToPayload(errorID)}
325329
}
326330

327-
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
328-
defer cancel()
329331
resp, err = tc.UnaryCall(metadata.NewOutgoingContext(tCtx, testMetadata), req, grpc.WaitForReady(!c.failfast))
330332
return req, resp, err
331333
}
@@ -336,9 +338,9 @@ func (te *test) doFullDuplexCallRoundtrip(c *rpcConfig) ([]proto.Message, []prot
336338
resps []proto.Message
337339
err error
338340
)
339-
tc := testgrpc.NewTestServiceClient(te.clientConn())
340341
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
341342
defer cancel()
343+
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
342344
stream, err := tc.FullDuplexCall(metadata.NewOutgoingContext(tCtx, testMetadata), grpc.WaitForReady(!c.failfast))
343345
if err != nil {
344346
return reqs, resps, err
@@ -377,9 +379,9 @@ func (te *test) doClientStreamCall(c *rpcConfig) ([]proto.Message, *testpb.Strea
377379
resp *testpb.StreamingInputCallResponse
378380
err error
379381
)
380-
tc := testgrpc.NewTestServiceClient(te.clientConn())
381382
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
382383
defer cancel()
384+
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
383385
stream, err := tc.StreamingInputCall(metadata.NewOutgoingContext(tCtx, testMetadata), grpc.WaitForReady(!c.failfast))
384386
if err != nil {
385387
return reqs, resp, err
@@ -407,16 +409,15 @@ func (te *test) doServerStreamCall(c *rpcConfig) (*testpb.StreamingOutputCallReq
407409
resps []proto.Message
408410
err error
409411
)
410-
411-
tc := testgrpc.NewTestServiceClient(te.clientConn())
412+
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
413+
defer cancel()
414+
tc := testgrpc.NewTestServiceClient(te.clientConn(tCtx))
412415

413416
var startID int32
414417
if !c.success {
415418
startID = errorID
416419
}
417420
req = &testpb.StreamingOutputCallRequest{Payload: idToPayload(startID)}
418-
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
419-
defer cancel()
420421
stream, err := tc.StreamingOutputCall(metadata.NewOutgoingContext(tCtx, testMetadata), req, grpc.WaitForReady(!c.failfast))
421422
if err != nil {
422423
return req, resps, err

test/channelz_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,11 @@ func (s) TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
304304
}
305305
}
306306

307-
func (s) TestCZTopChannelRegistrationAndDeletionWhenDialFail(t *testing.T) {
308-
// Make dial fails (due to no transport security specified)
307+
func (s) TestCZTopChannelRegistrationAndDeletionWhenNewClientFail(t *testing.T) {
308+
// Make newclient fails (due to no transport security specified)
309309
_, err := grpc.NewClient("fake.addr")
310310
if err == nil {
311-
t.Fatal("expecting dial to fail")
311+
t.Fatal("expecting newclient to fail")
312312
}
313313
if tcs, end := channelz.GetTopChannels(0, 0); tcs != nil || !end {
314314
t.Fatalf("GetTopChannels(0, 0) = %v, %v, want <nil>, true", tcs, end)

test/end2end_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,11 @@ func (te *test) clientConn(opts ...grpc.DialOption) *grpc.ClientConn {
854854
var scheme string
855855
opts, scheme = te.configDial(opts...)
856856
var err error
857-
te.cc, err = grpc.Dial(scheme+te.srvAddr, opts...)
857+
te.cc, err = grpc.NewClient(scheme+te.srvAddr, opts...)
858858
if err != nil {
859-
te.t.Fatalf("Dial(%q) = %v", scheme+te.srvAddr, err)
859+
te.t.Fatalf("grpc.NewClient(%q) failed: %v", scheme+te.srvAddr, err)
860860
}
861+
te.cc.Connect()
861862
return te.cc
862863
}
863864

0 commit comments

Comments
 (0)