• 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

76.47
/../api/src/main/java/io/grpc/ManagedChannelProvider.java
1
/*
2
 * Copyright 2015 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 com.google.common.base.Preconditions;
20
import java.net.SocketAddress;
21
import java.util.Collection;
22

23
/**
24
 * Provider of managed channels for transport agnostic consumption.
25
 *
26
 * <p>Implementations can be automatically discovered by gRPC via Java's SPI mechanism. For
27
 * automatic discovery, the implementation must have a zero-argument constructor and include
28
 * a resource named {@code META-INF/services/io.grpc.ManagedChannelProvider} in their JAR. The
29
 * file's contents should be the implementation's class name.
30
 *
31
 * <p>Implementations <em>should not</em> throw. If they do, it may interrupt class loading. If
32
 * exceptions may reasonably occur for implementation-specific reasons, implementations should
33
 * generally handle the exception gracefully and return {@code false} from {@link #isAvailable()}.
34
 */
35
@Internal
36
public abstract class ManagedChannelProvider {
1✔
37
  /**
38
   * Returns the ClassLoader-wide default channel.
39
   *
40
   * @throws ProviderNotFoundException if no provider is available
41
   */
42
  public static ManagedChannelProvider provider() {
43
    ManagedChannelProvider provider = ManagedChannelRegistry.getDefaultRegistry().provider();
1✔
44
    if (provider == null) {
1✔
45
      throw new ProviderNotFoundException("No functional channel service provider found. "
×
46
          + "Try adding a dependency on the grpc-okhttp, grpc-netty, or grpc-netty-shaded "
47
          + "artifact");
48
    }
49
    return provider;
1✔
50
  }
51

52
  /**
53
   * Whether this provider is available for use, taking the current environment into consideration.
54
   * If {@code false}, no other methods are safe to be called.
55
   */
56
  protected abstract boolean isAvailable();
57

58
  /**
59
   * A priority, from 0 to 10 that this provider should be used, taking the current environment into
60
   * consideration. 5 should be considered the default, and then tweaked based on environment
61
   * detection. A priority of 0 does not imply that the provider wouldn't work; just that it should
62
   * be last in line.
63
   */
64
  protected abstract int priority();
65

66
  /**
67
   * Creates a new builder with the given host and port.
68
   */
69
  protected abstract ManagedChannelBuilder<?> builderForAddress(String name, int port);
70

71
  /**
72
   * Creates a new builder with the given target URI.
73
   */
74
  protected abstract ManagedChannelBuilder<?> builderForTarget(String target);
75

76
  /**
77
   * Creates a new builder with the given target URI and credentials. Returns an error-string result
78
   * if unable to understand the credentials.
79
   */
80
  protected NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds) {
81
    return NewChannelBuilderResult.error("ChannelCredentials are unsupported");
×
82
  }
83

84
  /**
85
   * Creates a channel builder using the provided target, credentials, and resolution
86
   * components.
87
   *
88
   * <p>This method allows for fine-grained control over name resolution by providing
89
   * both a {@link NameResolverRegistry} and a specific {@link NameResolverProvider}.
90
   * This returns a {@link NewChannelBuilderResult},
91
   * which may contain an error string if the provided credentials or target are
92
   * not supported by this provider.
93
   *
94
   * @param target the target URI for the channel
95
   * @param creds the channel credentials to use
96
   * @param nameResolverRegistry the registry used for looking up name resolvers
97
   * @param nameResolverProvider a specific provider to use, or {@code null} to
98
   *     search the registry
99
   * @return a {@link NewChannelBuilderResult} containing either the builder or an
100
   *     error description
101
   * @since 1.83.0
102
   */
103
  protected NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds,
104
                                                      NameResolverRegistry nameResolverRegistry,
105
                                                      NameResolverProvider nameResolverProvider) {
106
    return newChannelBuilder(target, creds);
1✔
107
  }
108

109
  /**
110
   * Returns the {@link SocketAddress} types this ManagedChannelProvider supports.
111
   */
112
  protected abstract Collection<Class<? extends SocketAddress>> getSupportedSocketAddressTypes();
113

114
  public static final class NewChannelBuilderResult {
115
    private final ManagedChannelBuilder<?> channelBuilder;
116
    private final String error;
117

118
    private NewChannelBuilderResult(ManagedChannelBuilder<?> channelBuilder, String error) {
1✔
119
      this.channelBuilder = channelBuilder;
1✔
120
      this.error = error;
1✔
121
    }
1✔
122

123
    public static NewChannelBuilderResult channelBuilder(ManagedChannelBuilder<?> builder) {
124
      return new NewChannelBuilderResult(Preconditions.checkNotNull(builder), null);
1✔
125
    }
126

127
    public static NewChannelBuilderResult error(String error) {
128
      return new NewChannelBuilderResult(null, Preconditions.checkNotNull(error));
1✔
129
    }
130

131
    public ManagedChannelBuilder<?> getChannelBuilder() {
132
      return channelBuilder;
1✔
133
    }
134

135
    public String getError() {
136
      return error;
1✔
137
    }
138
  }
139

140
  /**
141
   * Thrown when no suitable {@link ManagedChannelProvider} objects can be found.
142
   */
143
  public static final class ProviderNotFoundException extends RuntimeException {
144
    private static final long serialVersionUID = 1;
145

146
    public ProviderNotFoundException(String msg) {
147
      super(msg);
×
148
    }
×
149
  }
150
}
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