Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions api/src/main/java/io/grpc/Grpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ public static ManagedChannelBuilder<?> newChannelBuilder(
return ManagedChannelRegistry.getDefaultRegistry().newChannelBuilder(target, creds);
}

/**
* Creates a channel builder with a target string, credentials, and a specific
* name resolver registry.
*
* <p>This method uses the {@link ManagedChannelRegistry#getDefaultRegistry()} to
* find an appropriate underlying transport provider based on the target and credentials.
* The provided {@code nameResolverRegistry} is used to resolve the target address
* into physical addresses (e.g., DNS or custom schemes).
*
* @param target the target URI for the channel, such as {@code "localhost:8080"}
* or {@code "dns:///example.com"}
* @param creds the channel credentials to use for secure communication
* @param nameResolverRegistry the registry used to look up {@link NameResolver}
* providers for the target
* @return a {@link ManagedChannelBuilder} instance configured with the given parameters
* @throws IllegalArgumentException if no provider is available for the given target
* or credentials
* @since 1.79.0
*/
public static ManagedChannelBuilder<?> newChannelBuilder(
String target,
ChannelCredentials creds,
NameResolverRegistry nameResolverRegistry) {
return ManagedChannelRegistry.getDefaultRegistry().newChannelBuilder(
nameResolverRegistry,
target,
creds);
}

/**
* Creates a channel builder from a host, port, and credentials. The host and port are combined to
* form an authority string and then passed to {@link #newChannelBuilder(String,
Expand Down
26 changes: 26 additions & 0 deletions api/src/main/java/io/grpc/ManagedChannelProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ protected NewChannelBuilderResult newChannelBuilder(String target, ChannelCreden
return NewChannelBuilderResult.error("ChannelCredentials are unsupported");
}

/**
* Creates a channel builder using the provided target, credentials, and resolution
* components.
*
* <p>This method allows for fine-grained control over name resolution by providing
* both a {@link NameResolverRegistry} and a specific {@link NameResolverProvider}.
* Unlike the public factory methods, this returns a {@link NewChannelBuilderResult},
* which may contain an error string if the provided credentials or target are
* not supported by this provider.
*
* @param target the target URI for the channel
* @param creds the channel credentials to use
* @param nameResolverRegistry the registry used for looking up name resolvers
* @param nameResolverProvider a specific provider to use, or {@code null} to
* search the registry
* @return a {@link NewChannelBuilderResult} containing either the builder or an
* error description
* @since 1.79.0
*/
protected NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds,
NameResolverRegistry nameResolverRegistry,
NameResolverProvider nameResolverProvider) {
// Implementation note: Currently delegates to the simplified version
return newChannelBuilder(target, creds);
}

/**
* Returns the {@link SocketAddress} types this ManagedChannelProvider supports.
*/
Expand Down
3 changes: 1 addition & 2 deletions api/src/main/java/io/grpc/ManagedChannelRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ ManagedChannelBuilder<?> newChannelBuilder(String target, ChannelCredentials cre
return newChannelBuilder(NameResolverRegistry.getDefaultRegistry(), target, creds);
}

@VisibleForTesting
ManagedChannelBuilder<?> newChannelBuilder(NameResolverRegistry nameResolverRegistry,
String target, ChannelCredentials creds) {
NameResolverProvider nameResolverProvider = null;
Expand Down Expand Up @@ -192,7 +191,7 @@ ManagedChannelBuilder<?> newChannelBuilder(NameResolverRegistry nameResolverRegi
continue;
}
ManagedChannelProvider.NewChannelBuilderResult result
= provider.newChannelBuilder(target, creds);
= provider.newChannelBuilder(target, creds, nameResolverRegistry, nameResolverProvider);
if (result.getChannelBuilder() != null) {
return result.getChannelBuilder();
}
Expand Down