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

grpc / grpc-java / #18802

18 Aug 2023 05:13PM UTC coverage: 88.293% (+0.02%) from 88.27%
#18802

push

github-actions

web-flow
stablize ServerBuilder.handshakeTimeout (#10499)

30349 of 34373 relevant lines covered (88.29%)

0.88 hits per line

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

7.41
/../api/src/main/java/io/grpc/ServerBuilder.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 static com.google.common.base.Preconditions.checkNotNull;
20

21
import com.google.common.base.Preconditions;
22
import java.io.File;
23
import java.io.InputStream;
24
import java.util.List;
25
import java.util.concurrent.Executor;
26
import java.util.concurrent.TimeUnit;
27
import javax.annotation.Nullable;
28

29
/**
30
 * A builder for {@link Server} instances.
31
 *
32
 * @param <T> The concrete type of this builder.
33
 * @since 1.0.0
34
 */
35
public abstract class ServerBuilder<T extends ServerBuilder<T>> {
1✔
36

37
  /**
38
   * Static factory for creating a new ServerBuilder.
39
   *
40
   * @param port the port to listen on
41
   * @since 1.0.0
42
   */
43
  public static ServerBuilder<?> forPort(int port) {
44
    return ServerProvider.provider().builderForPort(port);
1✔
45
  }
46

47
  /**
48
   * Execute application code directly in the transport thread.
49
   *
50
   * <p>Depending on the underlying transport, using a direct executor may lead to substantial
51
   * performance improvements. However, it also requires the application to not block under
52
   * any circumstances.
53
   *
54
   * <p>Calling this method is semantically equivalent to calling {@link #executor(Executor)} and
55
   * passing in a direct executor. However, this is the preferred way as it may allow the transport
56
   * to perform special optimizations.
57
   *
58
   * @return this
59
   * @since 1.0.0
60
   */
61
  public abstract T directExecutor();
62

63
  /**
64
   * Provides a custom executor.
65
   *
66
   * <p>It's an optional parameter. If the user has not provided an executor when the server is
67
   * built, the builder will use a static cached thread pool.
68
   *
69
   * <p>The server won't take ownership of the given executor. It's caller's responsibility to
70
   * shut down the executor when it's desired.
71
   *
72
   * @return this
73
   * @since 1.0.0
74
   */
75
  public abstract T executor(@Nullable Executor executor);
76

77

78
  /**
79
   * Allows for defining a way to provide a custom executor to handle the server call.
80
   * This executor is the result of calling
81
   * {@link ServerCallExecutorSupplier#getExecutor(ServerCall, Metadata)} per RPC.
82
   *
83
   * <p>It's an optional parameter. If it is provided, the {@link #executor(Executor)} would still
84
   * run necessary tasks before the {@link ServerCallExecutorSupplier} is ready to be called, then
85
   * it switches over. But if calling {@link ServerCallExecutorSupplier} returns null, the server
86
   * call is still handled by the default {@link #executor(Executor)} as a fallback.
87
   *
88
   * @param executorSupplier the server call executor provider
89
   * @return this
90
   * @since 1.39.0
91
   *
92
   * */
93
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/8274")
94
  public T callExecutor(ServerCallExecutorSupplier executorSupplier) {
95
    return thisT();
×
96
  }
97

98
  /**
99
   * Adds a service implementation to the handler registry.
100
   *
101
   * @param service ServerServiceDefinition object
102
   * @return this
103
   * @since 1.0.0
104
   */
105
  public abstract T addService(ServerServiceDefinition service);
106

107
  /**
108
   * Adds a service implementation to the handler registry.
109
   *
110
   * @param bindableService BindableService object
111
   * @return this
112
   * @since 1.0.0
113
   */
114
  public abstract T addService(BindableService bindableService);
115

116
  /**
117
   * Adds a list of service implementations to the handler registry together.
118
   *
119
   * @param services the list of ServerServiceDefinition objects
120
   * @return this
121
   * @since 1.37.0
122
   */
123
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/7925")
124
  public final T addServices(List<ServerServiceDefinition> services) {
125
    checkNotNull(services, "services");
×
126
    for (ServerServiceDefinition service : services) {
×
127
      addService(service);
×
128
    }
×
129
    return thisT();
×
130
  }
131

132
  /**
133
   * Adds a {@link ServerInterceptor} that is run for all services on the server.  Interceptors
134
   * added through this method always run before per-service interceptors added through {@link
135
   * ServerInterceptors}.  Interceptors run in the reverse order in which they are added, just as
136
   * with consecutive calls to {@code ServerInterceptors.intercept()}.
137
   *
138
   * @param interceptor the all-service interceptor
139
   * @return this
140
   * @since 1.5.0
141
   */
142
  public T intercept(ServerInterceptor interceptor) {
143
    throw new UnsupportedOperationException();
×
144
  }
145

146
  /**
147
   * Adds a {@link ServerTransportFilter}. The order of filters being added is the order they will
148
   * be executed.
149
   *
150
   * @return this
151
   * @since 1.2.0
152
   */
153
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2132")
154
  public T addTransportFilter(ServerTransportFilter filter) {
155
    throw new UnsupportedOperationException();
×
156
  }
157

158
  /**
159
   * Adds a {@link ServerStreamTracer.Factory} to measure server-side traffic.  The order of
160
   * factories being added is the order they will be executed.
161
   *
162
   * @return this
163
   * @since 1.3.0
164
   */
165
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861")
166
  public T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
167
    throw new UnsupportedOperationException();
×
168
  }
169

170
  /**
171
   * Sets a fallback handler registry that will be looked up in if a method is not found in the
172
   * primary registry. The primary registry (configured via {@code addService()}) is faster but
173
   * immutable. The fallback registry is more flexible and allows implementations to mutate over
174
   * time and load services on-demand.
175
   *
176
   * @return this
177
   * @since 1.0.0
178
   */
179
  public abstract T fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry);
180

181
  /**
182
   * Makes the server use TLS.
183
   *
184
   * @param certChain file containing the full certificate chain
185
   * @param privateKey file containing the private key
186
   *
187
   * @return this
188
   * @throws UnsupportedOperationException if the server does not support TLS.
189
   * @since 1.0.0
190
   */
191
  public abstract T useTransportSecurity(File certChain, File privateKey);
192

193
  /**
194
   * Makes the server use TLS.
195
   *
196
   * @param certChain InputStream containing the full certificate chain
197
   * @param privateKey InputStream containing the private key
198
   *
199
   * @return this
200
   * @throws UnsupportedOperationException if the server does not support TLS, or does not support
201
   *         reading these files from an InputStream.
202
   * @since 1.12.0
203
   */
204
  public T useTransportSecurity(InputStream certChain, InputStream privateKey) {
205
    throw new UnsupportedOperationException();
×
206
  }
207

208

209
  /**
210
   * Set the decompression registry for use in the channel.  This is an advanced API call and
211
   * shouldn't be used unless you are using custom message encoding.   The default supported
212
   * decompressors are in {@code DecompressorRegistry.getDefaultInstance}.
213
   *
214
   * @return this
215
   * @since 1.0.0
216
   */
217
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
218
  public abstract T decompressorRegistry(@Nullable DecompressorRegistry registry);
219

220
  /**
221
   * Set the compression registry for use in the channel.  This is an advanced API call and
222
   * shouldn't be used unless you are using custom message encoding.   The default supported
223
   * compressors are in {@code CompressorRegistry.getDefaultInstance}.
224
   *
225
   * @return this
226
   * @since 1.0.0
227
   */
228
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
229
  public abstract T compressorRegistry(@Nullable CompressorRegistry registry);
230

231
  /**
232
   * Sets the permitted time for new connections to complete negotiation handshakes before being
233
   * killed. The default value is 2 minutes.
234
   *
235
   * @return this
236
   * @throws IllegalArgumentException if timeout is negative
237
   * @throws UnsupportedOperationException if unsupported
238
   * @since 1.8.0
239
   */
240
  public T handshakeTimeout(long timeout, TimeUnit unit) {
241
    throw new UnsupportedOperationException();
×
242
  }
243

244
  /**
245
   * Sets the time without read activity before sending a keepalive ping. An unreasonably small
246
   * value might be increased, and {@code Long.MAX_VALUE} nano seconds or an unreasonably large
247
   * value will disable keepalive. The typical default is two hours when supported.
248
   *
249
   * @throws IllegalArgumentException if time is not positive
250
   * @throws UnsupportedOperationException if unsupported
251
   * @see <a href="https://github.com/grpc/proposal/blob/master/A9-server-side-conn-mgt.md">gRFC A9
252
   *     Server-side Connection Management</a>
253
   * @since 1.47.0
254
   */
255
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
256
  public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
257
    throw new UnsupportedOperationException();
×
258
  }
259

260
  /**
261
   * Sets a time waiting for read activity after sending a keepalive ping. If the time expires
262
   * without any read activity on the connection, the connection is considered dead. An unreasonably
263
   * small value might be increased. Defaults to 20 seconds when supported.
264
   *
265
   * <p>This value should be at least multiple times the RTT to allow for lost packets.
266
   *
267
   * @throws IllegalArgumentException if timeout is not positive
268
   * @throws UnsupportedOperationException if unsupported
269
   * @see <a href="https://github.com/grpc/proposal/blob/master/A9-server-side-conn-mgt.md">gRFC A9
270
   *     Server-side Connection Management</a>
271
   * @since 1.47.0
272
   */
273
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
274
  public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
275
    throw new UnsupportedOperationException();
×
276
  }
277

278
  /**
279
   * Sets the maximum connection idle time, connections being idle for longer than which will be
280
   * gracefully terminated. Idleness duration is defined since the most recent time the number of
281
   * outstanding RPCs became zero or the connection establishment. An unreasonably small value might
282
   * be increased. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
283
   * max connection idle.
284
   *
285
   * @throws IllegalArgumentException if idle is not positive
286
   * @throws UnsupportedOperationException if unsupported
287
   * @see <a href="https://github.com/grpc/proposal/blob/master/A9-server-side-conn-mgt.md">gRFC A9
288
   *     Server-side Connection Management</a>
289
   * @since 1.47.0
290
   */
291
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
292
  public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
293
    throw new UnsupportedOperationException();
×
294
  }
295

296
  /**
297
   * Sets the maximum connection age, connections lasting longer than which will be gracefully
298
   * terminated. An unreasonably small value might be increased. A random jitter of +/-10% will be
299
   * added to it. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
300
   * max connection age.
301
   *
302
   * @throws IllegalArgumentException if age is not positive
303
   * @throws UnsupportedOperationException if unsupported
304
   * @see <a href="https://github.com/grpc/proposal/blob/master/A9-server-side-conn-mgt.md">gRFC A9
305
   *     Server-side Connection Management</a>
306
   * @since 1.47.0
307
   */
308
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
309
  public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
310
    throw new UnsupportedOperationException();
×
311
  }
312

313
  /**
314
   * Sets the grace time for the graceful connection termination. Once the max connection age
315
   * is reached, RPCs have the grace time to complete. RPCs that do not complete in time will be
316
   * cancelled, allowing the connection to terminate. {@code Long.MAX_VALUE} nano seconds or an
317
   * unreasonably large value are considered infinite.
318
   *
319
   * @throws IllegalArgumentException if grace is negative
320
   * @throws UnsupportedOperationException if unsupported
321
   * @see #maxConnectionAge(long, TimeUnit)
322
   * @see <a href="https://github.com/grpc/proposal/blob/master/A9-server-side-conn-mgt.md">gRFC A9
323
   *     Server-side Connection Management</a>
324
   * @since 1.47.0
325
   */
326
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
327
  public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
328
    throw new UnsupportedOperationException();
×
329
  }
330

331
  /**
332
   * Specify the most aggressive keep-alive time clients are permitted to configure. The server will
333
   * try to detect clients exceeding this rate and when detected will forcefully close the
334
   * connection. The typical default is 5 minutes when supported.
335
   *
336
   * <p>Even though a default is defined that allows some keep-alives, clients must not use
337
   * keep-alive without approval from the service owner. Otherwise, they may experience failures in
338
   * the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a
339
   * significant amount of traffic and CPU usage, so clients and servers should be conservative in
340
   * what they use and accept.
341
   *
342
   * @throws IllegalArgumentException if time is negative
343
   * @throws UnsupportedOperationException if unsupported
344
   * @see #permitKeepAliveWithoutCalls(boolean)
345
   * @see <a href="https://github.com/grpc/proposal/blob/master/A8-client-side-keepalive.md">gRFC A8
346
   *     Client-side Keepalive</a>
347
   * @since 1.47.0
348
   */
349
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
350
  public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
351
    throw new UnsupportedOperationException();
×
352
  }
353

354
  /**
355
   * Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding
356
   * RPCs on the connection. Defaults to {@code false} when supported.
357
   *
358
   * @throws UnsupportedOperationException if unsupported
359
   * @see #permitKeepAliveTime(long, TimeUnit)
360
   * @see <a href="https://github.com/grpc/proposal/blob/master/A8-client-side-keepalive.md">gRFC A8
361
   *     Client-side Keepalive</a>
362
   * @since 1.47.0
363
   */
364
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
365
  public T permitKeepAliveWithoutCalls(boolean permit) {
366
    throw new UnsupportedOperationException();
×
367
  }
368

369
  /**
370
   * Sets the maximum message size allowed to be received on the server. If not called,
371
   * defaults to 4 MiB. The default provides protection to servers who haven't considered the
372
   * possibility of receiving large messages while trying to be large enough to not be hit in normal
373
   * usage.
374
   *
375
   * <p>This method is advisory, and implementations may decide to not enforce this.  Currently,
376
   * the only known transport to not enforce this is {@code InProcessServer}.
377
   *
378
   * @param bytes the maximum number of bytes a single message can be.
379
   * @return this
380
   * @throws IllegalArgumentException if bytes is negative.
381
   * @throws UnsupportedOperationException if unsupported.
382
   * @since 1.13.0
383
   */
384
  public T maxInboundMessageSize(int bytes) {
385
    // intentional noop rather than throw, this method is only advisory.
386
    Preconditions.checkArgument(bytes >= 0, "bytes must be >= 0");
×
387
    return thisT();
×
388
  }
389

390
  /**
391
   * Sets the maximum size of metadata allowed to be received. {@code Integer.MAX_VALUE} disables
392
   * the enforcement. The default is implementation-dependent, but is not generally less than 8 KiB
393
   * and may be unlimited.
394
   *
395
   * <p>This is cumulative size of the metadata. The precise calculation is
396
   * implementation-dependent, but implementations are encouraged to follow the calculation used for
397
   * <a href="http://httpwg.org/specs/rfc7540.html#rfc.section.6.5.2">
398
   * HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE</a>. It sums the bytes from each entry's key and value,
399
   * plus 32 bytes of overhead per entry.
400
   *
401
   * @param bytes the maximum size of received metadata
402
   * @return this
403
   * @throws IllegalArgumentException if bytes is non-positive
404
   * @since 1.17.0
405
   */
406
  public T maxInboundMetadataSize(int bytes) {
407
    Preconditions.checkArgument(bytes > 0, "maxInboundMetadataSize must be > 0");
×
408
    // intentional noop rather than throw, this method is only advisory.
409
    return thisT();
×
410
  }
411

412
  /**
413
   * Sets the BinaryLog object that this server should log to. The server does not take
414
   * ownership of the object, and users are responsible for calling {@link BinaryLog#close()}.
415
   *
416
   * @param binaryLog the object to provide logging.
417
   * @return this
418
   * @since 1.13.0
419
   */
420
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017")
421
  public T setBinaryLog(BinaryLog binaryLog) {
422
    throw new UnsupportedOperationException();
×
423
  }
424

425
  /**
426
   * Builds a server using the given parameters.
427
   *
428
   * <p>The returned service will not been started or be bound a port. You will need to start it
429
   * with {@link Server#start()}.
430
   *
431
   * @return a new Server
432
   * @since 1.0.0
433
   */
434
  public abstract Server build();
435

436
  /**
437
   * Returns the correctly typed version of the builder.
438
   */
439
  private T thisT() {
440
    @SuppressWarnings("unchecked")
441
    T thisT = (T) this;
×
442
    return thisT;
×
443
  }
444
}
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