Skip to content

Commit a8d11ce

Browse files
balancer: introduce env flag for case-sensitive registry (#8837)
Part of #5288 Follow up of PR: #6647 This PR introduces the `GRPC_GO_EXPERIMENTAL_CASE_SENSITIVE_BALANCER_REGISTRIES` environment variable to transition balancer registries to be case-sensitive. **Default Behavior**: * The env var is disabled by default. * The registry remains case-insensitive (legacy behavior) and a warning is logged if a balancer name contains uppercase letters, notifying users of the upcoming change. RELEASE NOTES: * balancer: grpc will log a warning if balancer registries are used with uppercase letter(s).
1 parent c7ec4d9 commit a8d11ce

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

balancer/balancer.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
estats "google.golang.org/grpc/experimental/stats"
3434
"google.golang.org/grpc/grpclog"
3535
"google.golang.org/grpc/internal"
36+
"google.golang.org/grpc/internal/envconfig"
3637
"google.golang.org/grpc/metadata"
3738
"google.golang.org/grpc/resolver"
3839
"google.golang.org/grpc/serviceconfig"
@@ -46,21 +47,21 @@ var (
4647
)
4748

4849
// Register registers the balancer builder to the balancer map. b.Name
49-
// (lowercased) will be used as the name registered with this builder. If the
50-
// Builder implements ConfigParser, ParseConfig will be called when new service
50+
// will be used as the name registered with this builder. If the Builder
51+
// implements ConfigParser, ParseConfig will be called when new service
5152
// configs are received by the resolver, and the result will be provided to the
5253
// Balancer in UpdateClientConnState.
5354
//
5455
// NOTE: this function must only be called during initialization time (i.e. in
5556
// an init() function), and is not thread-safe. If multiple Balancers are
5657
// registered with the same name, the one registered last will take effect.
5758
func Register(b Builder) {
58-
name := strings.ToLower(b.Name())
59-
if name != b.Name() {
60-
// TODO: Skip the use of strings.ToLower() to index the map after v1.59
61-
// is released to switch to case sensitive balancer registry. Also,
62-
// remove this warning and update the docstrings for Register and Get.
63-
logger.Warningf("Balancer registered with name %q. grpc-go will be switching to case sensitive balancer registries soon", b.Name())
59+
name := b.Name()
60+
if !envconfig.CaseSensitiveBalancerRegistries {
61+
name = strings.ToLower(name)
62+
if name != b.Name() {
63+
logger.Warningf("Balancer registered with name %q. grpc-go will be switching to case sensitive balancer registries soon. After 2 releases, we will enable the env var by default.", b.Name())
64+
}
6465
}
6566
m[name] = b
6667
}
@@ -78,16 +79,17 @@ func init() {
7879
}
7980

8081
// Get returns the resolver builder registered with the given name.
81-
// Note that the compare is done in a case-insensitive fashion.
82+
// Note that the compare is done in a case-sensitive fashion.
8283
// If no builder is register with the name, nil will be returned.
8384
func Get(name string) Builder {
84-
if strings.ToLower(name) != name {
85-
// TODO: Skip the use of strings.ToLower() to index the map after v1.59
86-
// is released to switch to case sensitive balancer registry. Also,
87-
// remove this warning and update the docstrings for Register and Get.
88-
logger.Warningf("Balancer retrieved for name %q. grpc-go will be switching to case sensitive balancer registries soon", name)
85+
if !envconfig.CaseSensitiveBalancerRegistries {
86+
lowerName := strings.ToLower(name)
87+
if lowerName != name {
88+
logger.Warningf("Balancer retrieved for name %q. grpc-go will be switching to case sensitive balancer registries soon. After 2 releases, we will enable the env var by default.", name)
89+
}
90+
name = lowerName
8991
}
90-
if b, ok := m[strings.ToLower(name)]; ok {
92+
if b, ok := m[name]; ok {
9193
return b
9294
}
9395
return nil

internal/envconfig/envconfig.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ var (
7878
// - The DNS resolver is being used.
7979
EnableDefaultPortForProxyTarget = boolFromEnv("GRPC_EXPERIMENTAL_ENABLE_DEFAULT_PORT_FOR_PROXY_TARGET", true)
8080

81+
// CaseSensitiveBalancerRegistries is set if the balancer registry should be
82+
// case-sensitive. This is disabled by default, but can be enabled by setting
83+
// the env variable "GRPC_GO_EXPERIMENTAL_CASE_SENSITIVE_BALANCER_REGISTRIES"
84+
// to "true".
85+
//
86+
// TODO: After 2 releases, we will enable the env var by default.
87+
CaseSensitiveBalancerRegistries = boolFromEnv("GRPC_GO_EXPERIMENTAL_CASE_SENSITIVE_BALANCER_REGISTRIES", false)
88+
8189
// XDSAuthorityRewrite indicates whether xDS authority rewriting is enabled.
8290
// This feature is defined in gRFC A81 and is enabled by setting the
8391
// environment variable GRPC_EXPERIMENTAL_XDS_AUTHORITY_REWRITE to "true".

0 commit comments

Comments
 (0)