• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #20320

17 Jun 2026 05:11PM UTC coverage: 88.886% (+0.01%) from 88.874%
#20320

push

github

web-flow
api: Add Grpc.newChannelBuilder accepting NameResolverRegistry (#11901)

This introduces a new `Grpc.newChannelBuilder` overload that allows callers to pass an explicit `NameResolverRegistry`. Fixes #11055

36550 of 41120 relevant lines covered (88.89%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/../api/src/main/java/io/grpc/Grpc.java
1
/*
2
 * Copyright 2016 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc;
18

19
import java.lang.annotation.Documented;
20
import java.lang.annotation.Retention;
21
import java.lang.annotation.RetentionPolicy;
22
import java.net.SocketAddress;
23
import java.net.URI;
24
import java.net.URISyntaxException;
25
import javax.net.ssl.SSLSession;
26

27
/**
28
 * Stuff that are part of the public API but are not bound to particular classes, e.g., static
29
 * methods, constants, attribute and context keys.
30
 */
31
public final class Grpc {
32
  private Grpc() {
33
  }
34

35
  /**
36
   * Attribute key for the remote address of a transport.
37
   */
38
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
39
  @TransportAttr
40
  public static final Attributes.Key<SocketAddress> TRANSPORT_ATTR_REMOTE_ADDR =
1✔
41
      Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_REMOTE_ADDR");
1✔
42

43
  /**
44
   * Attribute key for the local address of a transport.
45
   */
46
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
47
  @TransportAttr
48
  public static final Attributes.Key<SocketAddress> TRANSPORT_ATTR_LOCAL_ADDR =
1✔
49
      Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_LOCAL_ADDR");
1✔
50

51
  /**
52
   * Attribute key for SSL session of a transport.
53
   */
54
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1710")
55
  @TransportAttr
56
  public static final Attributes.Key<SSLSession> TRANSPORT_ATTR_SSL_SESSION =
1✔
57
      Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_SSL_SESSION");
1✔
58

59
  /**
60
   * The value for the custom label of per-RPC metrics. Defaults to empty string when unset. Must
61
   * not be set to {@code null}.
62
   */
63
  public static final CallOptions.Key<String> CALL_OPTION_CUSTOM_LABEL =
1✔
64
      CallOptions.Key.createWithDefault("io.grpc.Grpc.CALL_OPTION_CUSTOM_LABEL", "");
1✔
65

66
  /**
67
   * Annotation for transport attributes. It follows the annotation semantics defined
68
   * by {@link Attributes}.
69
   */
70
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4972")
71
  @Retention(RetentionPolicy.SOURCE)
72
  @Documented
73
  public @interface TransportAttr {}
74

75
  /**
76
   * Creates a channel builder with a target string and credentials. The target can be either a
77
   * valid {@link NameResolver}-compliant URI, or an authority string.
78
   *
79
   * <p>A {@code NameResolver}-compliant URI is an absolute hierarchical URI as defined by {@link
80
   * java.net.URI}. Example URIs:
81
   * <ul>
82
   *   <li>{@code "dns:///foo.googleapis.com:8080"}</li>
83
   *   <li>{@code "dns:///foo.googleapis.com"}</li>
84
   *   <li>{@code "dns:///%5B2001:db8:85a3:8d3:1319:8a2e:370:7348%5D:443"}</li>
85
   *   <li>{@code "dns://8.8.8.8/foo.googleapis.com:8080"}</li>
86
   *   <li>{@code "dns://8.8.8.8/foo.googleapis.com"}</li>
87
   *   <li>{@code "zookeeper://zk.example.com:9900/example_service"}</li>
88
   * </ul>
89
   *
90
   * <p>An authority string will be converted to a {@code NameResolver}-compliant URI, which has
91
   * the scheme from the name resolver with the highest priority (e.g. {@code "dns"}),
92
   * no authority, and the original authority string as its path after properly escaped.
93
   * We recommend libraries to specify the schema explicitly if it is known, since libraries cannot
94
   * know which NameResolver will be default during runtime.
95
   * Example authority strings:
96
   * <ul>
97
   *   <li>{@code "localhost"}</li>
98
   *   <li>{@code "127.0.0.1"}</li>
99
   *   <li>{@code "localhost:8080"}</li>
100
   *   <li>{@code "foo.googleapis.com:8080"}</li>
101
   *   <li>{@code "127.0.0.1:8080"}</li>
102
   *   <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]"}</li>
103
   *   <li>{@code "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"}</li>
104
   * </ul>
105
   */
106
  public static ManagedChannelBuilder<?> newChannelBuilder(
107
      String target, ChannelCredentials creds) {
108
    return ManagedChannelRegistry.getDefaultRegistry().newChannelBuilder(target, creds);
1✔
109
  }
110

111
  /**
112
   * Creates a channel builder with a target string, credentials, and a specific
113
   * name resolver registry.
114
   *
115
   * <p>The provided {@code nameResolverRegistry} is used to resolve the target address
116
   * into physical addresses (e.g., DNS or custom schemes).
117
   *
118
   * @param target the target URI for the channel, such as {@code "localhost:8080"}
119
   *     or {@code "dns:///example.com"}
120
   * @param creds the channel credentials to use for secure communication
121
   * @param nameResolverRegistry the registry used to look up {@link NameResolver}
122
   *     providers for the target
123
   * @return a {@link ManagedChannelBuilder} instance configured with the given parameters
124
   * @throws IllegalArgumentException if no provider is available for the given target
125
   *     or credentials
126
   * @since 1.83.0
127
   */
128
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/12694")
129
  public static ManagedChannelBuilder<?> newChannelBuilder(
130
      String target,
131
      ChannelCredentials creds,
132
      NameResolverRegistry nameResolverRegistry) {
133
    return ManagedChannelRegistry.getDefaultRegistry().newChannelBuilder(
×
134
        nameResolverRegistry,
135
        target,
136
        creds);
137
  }
138

139
  /**
140
   * Creates a channel builder from a host, port, and credentials. The host and port are combined to
141
   * form an authority string and then passed to {@link #newChannelBuilder(String,
142
   * ChannelCredentials)}. IPv6 addresses are properly surrounded by square brackets ("[]").
143
   */
144
  public static ManagedChannelBuilder<?> newChannelBuilderForAddress(
145
      String host, int port, ChannelCredentials creds) {
146
    return newChannelBuilder(authorityFromHostAndPort(host, port), creds);
1✔
147
  }
148

149
  /**
150
   * Combine a host and port into an authority string.
151
   */
152
  // A copy of GrpcUtil.authorityFromHostAndPort
153
  private static String authorityFromHostAndPort(String host, int port) {
154
    try {
155
      return new URI(null, null, host, port, null, null, null).getAuthority();
1✔
156
    } catch (URISyntaxException ex) {
×
157
      throw new IllegalArgumentException("Invalid host or port: " + host + " " + port, ex);
×
158
    }
159
  }
160

161
  /**
162
   * Static factory for creating a new ServerBuilder.
163
   *
164
   * @param port the port to listen on
165
   * @param creds the server identity
166
   */
167
  public static ServerBuilder<?> newServerBuilderForPort(int port, ServerCredentials creds) {
168
    return ServerRegistry.getDefaultRegistry().newServerBuilderForPort(port, creds);
1✔
169
  }
170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc