Skip to content

Commit f8daa0f

Browse files
committed
client: add GRPC_GO_REQUIRE_HANDSHAKE options to control connection behavior
Possible settings of this environment variable: - "hybrid" (default; removed after the 1.17 release): wait for handshake before considering a connection ready, but wait before considering successful. - "on" (default after the 1.17 release): wait for handshake before considering a connection ready/successful. - "off": do not wait for handshake before considering a connection ready/successful. This setting will be completely removed after the 1.18 release, and "on" will be the only supported behavior.
1 parent ef2b8e2 commit f8daa0f

File tree

5 files changed

+309
-80
lines changed

5 files changed

+309
-80
lines changed

Diff for: clientconn.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"google.golang.org/grpc/internal"
4040
"google.golang.org/grpc/internal/backoff"
4141
"google.golang.org/grpc/internal/channelz"
42+
"google.golang.org/grpc/internal/envconfig"
4243
"google.golang.org/grpc/internal/grpcsync"
4344
"google.golang.org/grpc/internal/transport"
4445
"google.golang.org/grpc/keepalive"
@@ -132,6 +133,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
132133
blockingpicker: newPickerWrapper(),
133134
czData: new(channelzData),
134135
firstResolveEvent: grpcsync.NewEvent(),
136+
reqHandshake: envconfig.RequireHandshake,
135137
}
136138
cc.retryThrottler.Store((*retryThrottler)(nil))
137139
cc.ctx, cc.cancel = context.WithCancel(context.Background())
@@ -403,6 +405,8 @@ type ClientConn struct {
403405

404406
channelzID int64 // channelz unique identification number
405407
czData *channelzData
408+
409+
reqHandshake envconfig.RequireHandshakeSetting // local copy to avoid test races if it changes
406410
}
407411

408412
// WaitForStateChange waits until the connectivity.State of ClientConn changes from sourceState or
@@ -1148,12 +1152,12 @@ func (ac *addrConn) createTransport(backoffNum int, addr resolver.Address, copts
11481152
if err == nil {
11491153
prefaceMu.Lock()
11501154
clientPrefaceWrote = true
1151-
if serverPrefaceReceived {
1155+
if serverPrefaceReceived || (!ac.dopts.waitForHandshake && ac.cc.reqHandshake == envconfig.RequireHandshakeOff) {
11521156
ac.successfulHandshake = true
11531157
}
11541158
prefaceMu.Unlock()
11551159

1156-
if ac.dopts.waitForHandshake {
1160+
if ac.dopts.waitForHandshake || ac.cc.reqHandshake == envconfig.RequireHandshakeOn {
11571161
select {
11581162
case <-prefaceTimer.C:
11591163
// We didn't get the preface in time.
@@ -1166,7 +1170,7 @@ func (ac *addrConn) createTransport(backoffNum int, addr resolver.Address, copts
11661170
close(allowedToReset)
11671171
return nil
11681172
}
1169-
} else {
1173+
} else if ac.cc.reqHandshake == envconfig.RequireHandshakeHybrid {
11701174
go func() {
11711175
select {
11721176
case <-prefaceTimer.C:

0 commit comments

Comments
 (0)