-
Notifications
You must be signed in to change notification settings - Fork 4k
Closed
Labels
Description
Is your feature request related to a problem?
The built-in load balancing policy names "round_robin" and "pick_first" are hardcoded as string literals throughout the codebase and by library users.
This leads to:
- Magic strings — typos like "round_robbin" can only be caught at runtime
- Poor discoverability — users can't find available policy names via IDE autocomplete
- No single source of truth — the only constant (ROUND_ROBIN_FIELD_NAME in LoadBalancerConfigFactory) is package-private and limited to the xds module
- Fragile user code — library users are forced to write code like defaultLoadBalancingPolicy("round_robin") or getProvider("pick_first") with raw strings, making their code vulnerable to silent misconfiguration that only manifests as unexpected runtime behavior
Describe the solution you'd like
Add public constants to LoadBalancerRegistry:
public static final String PICK_FIRST_POLICY_NAME = "pick_first";
public static final String ROUND_ROBIN_POLICY_NAME = "round_robin";
LoadBalancerRegistry is the natural home because it is the public API entry point where users look up providers via getProvider(String policy). Then replace all hardcoded string literals in main, test, and example sources with these constants.
Describe alternatives you've considered
- Adding constants to GrpcUtil — rejected because GrpcUtil is in the io.grpc.internal package and not intended for external use, even though it already has DEFAULT_LB_POLICY = "pick_first".
- Creating a new dedicated constants class (e.g., LoadBalancingPolicies) — rejected as unnecessary; LoadBalancerRegistry already serves as the central API for policy lookup.
Additional context
- The implicit API contract already exists — users already depend on these string values.
- This change makes the existing contract explicit, improving type safety and discoverability.
- GrpcUtil.DEFAULT_LB_POLICY (internal) can be updated to reference the new public constant.
Reactions are currently unavailable