Skip to content

Commit

Permalink
grpc: optional interface to provide channel authority (#6752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-Sood committed Dec 5, 2023
1 parent 5d7453e commit 0866ce0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
24 changes: 6 additions & 18 deletions clientconn.go
Expand Up @@ -1860,27 +1860,15 @@ func (cc *ClientConn) determineAuthority() error {
}

endpoint := cc.parsedTarget.Endpoint()
target := cc.target
switch {
case authorityFromDialOption != "":
if authorityFromDialOption != "" {
cc.authority = authorityFromDialOption
case authorityFromCreds != "":
} else if authorityFromCreds != "" {
cc.authority = authorityFromCreds
case strings.HasPrefix(target, "unix:") || strings.HasPrefix(target, "unix-abstract:"):
// TODO: remove when the unix resolver implements optional interface to
// return channel authority.
cc.authority = "localhost"
case strings.HasPrefix(endpoint, ":"):
} else if auth, ok := cc.resolverBuilder.(resolver.AuthorityOverrider); ok {
cc.authority = auth.OverrideAuthority(cc.parsedTarget)
} else if strings.HasPrefix(endpoint, ":") {
cc.authority = "localhost" + endpoint
default:
// TODO: Define an optional interface on the resolver builder to return
// the channel authority given the user's dial target. For resolvers
// which don't implement this interface, we will use the endpoint from
// "scheme://authority/endpoint" as the default authority.
// Escape the endpoint to handle use cases where the endpoint
// might not be a valid authority by default.
// For example an endpoint which has multiple paths like
// 'a/b/c', which is not a valid authority by default.
} else {
cc.authority = encodeAuthority(endpoint)
}
channelz.Infof(logger, cc.channelzID, "Channel authority set to %q", cc.authority)
Expand Down
4 changes: 4 additions & 0 deletions internal/resolver/unix/unix.go
Expand Up @@ -61,6 +61,10 @@ func (b *builder) Scheme() string {
return b.scheme
}

func (b *builder) OverrideAuthority(resolver.Target) string {
return "localhost"
}

type nopResolver struct {
}

Expand Down
10 changes: 10 additions & 0 deletions resolver/resolver.go
Expand Up @@ -314,3 +314,13 @@ type Resolver interface {
// Close closes the resolver.
Close()
}

// AuthorityOverrider is implemented by Builders that wish to override the
// default authority for the ClientConn.
// By default, the authority used is target.Endpoint().
type AuthorityOverrider interface {
// OverrideAuthority returns the authority to use for a ClientConn with the
// given target. The implementation must generate it without blocking,
// typically in line, and must keep it unchanged.
OverrideAuthority(Target) string
}

0 comments on commit 0866ce0

Please sign in to comment.