Skip to content

Commit 685ec81

Browse files
authored
feat: add an option to enable DirectPath xDS (#1942)
Currently DirectPath xDS is enabled by using env. We want to also provide an option for this. Java PR: googleapis/gax-java#1968
1 parent d85769c commit 685ec81

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

internal/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type DialSettings struct {
4646
SkipValidation bool
4747
ImpersonationConfig *impersonate.Config
4848
EnableDirectPath bool
49+
EnableDirectPathXds bool
4950
AllowNonDefaultServiceAccount bool
5051

5152
// Google API system parameters. For more information please read:

option/internaloption/internaloption.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ func (e enableDirectPath) Apply(o *internal.DialSettings) {
6767
o.EnableDirectPath = bool(e)
6868
}
6969

70+
// EnableDirectPathXds returns a ClientOption that overrides the default
71+
// DirectPath type. It is only valid when DirectPath is enabled.
72+
//
73+
// It should only be used internally by generated clients.
74+
// This is an EXPERIMENTAL API and may be changed or removed in the future.
75+
func EnableDirectPathXds() option.ClientOption {
76+
return enableDirectPathXds(true)
77+
}
78+
79+
type enableDirectPathXds bool
80+
81+
func (x enableDirectPathXds) Apply(o *internal.DialSettings) {
82+
o.EnableDirectPathXds = bool(x)
83+
}
84+
7085
// AllowNonDefaultServiceAccount returns a ClientOption that overrides the default
7186
// requirement for using the default service account for DirectPath.
7287
//

transport/grpc/dial.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.C
164164
grpcOpts = append(grpcOpts, timeoutDialerOption)
165165
}
166166
// Check if google-c2p resolver is enabled for DirectPath
167-
if strings.EqualFold(os.Getenv(enableDirectPathXds), "true") {
167+
if isDirectPathXdsUsed(o) {
168168
// google-c2p resolver target must not have a port number
169169
if addr, _, err := net.SplitHostPort(endpoint); err == nil {
170170
endpoint = "google-c2p:///" + addr
@@ -251,6 +251,19 @@ func isDirectPathEnabled(endpoint string, o *internal.DialSettings) bool {
251251
return true
252252
}
253253

254+
func isDirectPathXdsUsed(o *internal.DialSettings) bool {
255+
// Method 1: Enable DirectPath xDS by env;
256+
if strings.EqualFold(os.Getenv(enableDirectPathXds), "true") {
257+
return true
258+
}
259+
// Method 2: Enable DirectPath xDS by option;
260+
if o.EnableDirectPathXds {
261+
return true
262+
}
263+
return false
264+
265+
}
266+
254267
func isTokenSourceDirectPathCompatible(ts oauth2.TokenSource, o *internal.DialSettings) bool {
255268
if ts == nil {
256269
return false

0 commit comments

Comments
 (0)