4141import javax .net .ssl .TrustManagerFactory ;
4242
4343import com .google .common .annotations .VisibleForTesting ;
44+ import com .google .common .base .Preconditions ;
4445import com .google .common .base .Predicate ;
4546import com .google .common .base .Predicates ;
4647import com .google .common .collect .ImmutableSet ;
6465
6566import io .grpc .CallCredentials ;
6667import io .grpc .CallOptions ;
68+ import io .grpc .Deadline ;
6769import io .grpc .ManagedChannel ;
6870import io .grpc .Metadata ;
6971import io .grpc .Metadata .Key ;
8284import io .netty .channel .epoll .EpollSocketChannel ;
8385import io .netty .channel .nio .NioEventLoopGroup ;
8486import io .netty .channel .socket .nio .NioSocketChannel ;
87+ import io .netty .handler .ssl .SslContext ;
8588import io .netty .handler .ssl .SslContextBuilder ;
8689import io .netty .util .concurrent .FastThreadLocalThread ;
8790
@@ -131,18 +134,34 @@ public static class Builder {
131134 this .chanBuilder = chanBuilder ;
132135 }
133136
137+ /**
138+ * Set etcd credentials to use from {@link ByteString}s
139+ *
140+ * @param name
141+ * @param password
142+ */
134143 public Builder withCredentials (ByteString name , ByteString password ) {
135144 this .name = name ;
136145 this .password = password ;
137146 return this ;
138147 }
139148
149+ /**
150+ * Set etcd credentials to use from {@link String}s as UTF-8.
151+ *
152+ * @param name
153+ * @param password
154+ */
140155 public Builder withCredentials (String name , String password ) {
141156 this .name = ByteString .copyFromUtf8 (name );
142157 this .password = ByteString .copyFromUtf8 (password );
143158 return this ;
144159 }
145160
161+ /**
162+ * Attempt authentication immediately rather than if/when required.
163+ * Applies only if etcd credentials have been provided.
164+ */
146165 public Builder withImmediateAuth () {
147166 preemptAuth = true ;
148167 return this ;
@@ -156,16 +175,41 @@ public Builder withThreadCount(int threads) {
156175 return this ;
157176 }
158177
178+ /**
179+ * Control whether all RPC requests are sent via the underlying
180+ * IO event loop. This helps to limit overall memory use due to
181+ * internal thread-local buffer pooling.
182+ * <p>
183+ * Default is <code>true</code>
184+ *
185+ * @param sendViaEventLoop
186+ */
159187 public Builder sendViaEventLoop (boolean sendViaEventLoop ) {
160188 this .sendViaEventLoop = sendViaEventLoop ;
161189 return this ;
162190 }
163191
192+ /**
193+ * Provide executor to use for user call-backs. A default
194+ * client-scoped executor will be used if not set.
195+ *
196+ * @param executor
197+ */
164198 public Builder withUserExecutor (Executor executor ) {
165199 this .executor = executor ;
166200 return this ;
167201 }
168202
203+ /**
204+ * Provide a default timeout to use for requests made by this client.
205+ * This timeout value is used per-attempt, unlike {@link Deadline}s
206+ * used per-call which also cover any/all retry attempts.
207+ *
208+ * The default for this default is 10 seconds
209+ *
210+ * @param value
211+ * @param unit
212+ */
169213 public Builder withDefaultTimeout (long value , TimeUnit unit ) {
170214 this .defaultTimeoutMs = TimeUnit .MILLISECONDS .convert (value , unit );
171215 return this ;
@@ -176,43 +220,80 @@ private SslContextBuilder sslBuilder() {
176220 : (sslContextBuilder = GrpcSslContexts .forClient ());
177221 }
178222
223+ /**
224+ * Disable TLS - to connect to insecure servers in development contexts
225+ */
179226 public Builder withPlainText () {
180227 chanBuilder .usePlaintext ();
181228 return this ;
182229 }
183230
231+ /**
232+ * Provide CA certificate to use for TLS connection
233+ *
234+ * @param certSource
235+ * @throws IOException if there is an error reading from the provided {@link ByteSource}
236+ * @throws SSLException
237+ */
184238 public Builder withCaCert (ByteSource certSource ) throws IOException , SSLException {
185239 try (InputStream cert = certSource .openStream ()) {
186240 chanBuilder .sslContext (sslBuilder ().trustManager (cert ).build ());
187241 }
188242 return this ;
189243 }
190244
245+ /**
246+ * Provide custom {@link TrustManagerFactory} to use for this
247+ * client's TLS connection.
248+ *
249+ * @param tmf
250+ * @throws SSLException
251+ */
191252 public Builder withTrustManager (TrustManagerFactory tmf ) throws SSLException {
192253 chanBuilder .sslContext (sslBuilder ().trustManager (tmf ).build ());
193254 return this ;
194255 }
195256
257+ /**
258+ * Configure the netty {@link SslContext} to be used by this client.
259+ * The provided {@link Consumer} should make updates to the passed
260+ * {@link SslContextBuilder} as needed, but should not call build().
261+ *
262+ * @param contextBuilder
263+ * @throws SSLException
264+ */
196265 public Builder withTlsConfig (Consumer <SslContextBuilder > contextBuilder ) throws SSLException {
197266 SslContextBuilder sslBuilder = sslBuilder ();
198267 contextBuilder .accept (sslBuilder );
199268 chanBuilder .sslContext (sslBuilder .build ());
200269 return this ;
201270 }
202271
272+ /**
273+ * Set the session timeout in seconds - this corresponds to the TTL of the
274+ * session lease, see {@link EtcdClient#getSessionLease()}.
275+ *
276+ * @param timeoutSecs
277+ */
203278 public Builder withSessionTimeoutSecs (int timeoutSecs ) {
204- if (timeoutSecs < 1 ) {
205- throw new IllegalArgumentException ("invalid session timeout: " + timeoutSecs );
206- }
279+ Preconditions .checkArgument (timeoutSecs < 1 , "invalid session timeout: %s" , timeoutSecs );
207280 this .sessTimeoutSecs = timeoutSecs ;
208281 return this ;
209282 }
210283
284+ /**
285+ * Set the maximum inbound message size in bytes
286+ *
287+ * @param sizeInBytes
288+ */
211289 public Builder withMaxInboundMessageSize (int sizeInBytes ) {
212290 chanBuilder .maxInboundMessageSize (sizeInBytes );
213291 return this ;
214292 }
215293
294+ /**
295+ * @return the built {@link EtcdClient} instance
296+ */
216297 public EtcdClient build () {
217298 return new EtcdClient (chanBuilder , defaultTimeoutMs , name , password ,
218299 preemptAuth , threads , executor , sendViaEventLoop , sessTimeoutSecs );
@@ -223,18 +304,34 @@ private static int defaultThreadCount() {
223304 return Math .min (6 , Runtime .getRuntime ().availableProcessors ());
224305 }
225306
307+ /**
308+ *
309+ * @param host
310+ * @param port
311+ * @return
312+ */
226313 public static Builder forEndpoint (String host , int port ) {
227314 String target = GrpcUtil .authorityFromHostAndPort (host , port );
228315 return new Builder (NettyChannelBuilder .forTarget (target ));
229316 }
230317
318+ /**
319+ *
320+ * @param endpoints
321+ * @return
322+ */
231323 public static Builder forEndpoints (List <String > endpoints ) {
232324 NettyChannelBuilder ncb = NettyChannelBuilder
233325 .forTarget (StaticEtcdNameResolverFactory .ETCD )
234326 .nameResolverFactory (new StaticEtcdNameResolverFactory (endpoints ));
235327 return new Builder (ncb );
236328 }
237329
330+ /**
331+ *
332+ * @param endpoints
333+ * @return
334+ */
238335 public static Builder forEndpoints (String endpoints ) {
239336 return forEndpoints (Arrays .asList (endpoints .split ("," )));
240337 }
0 commit comments