Skip to content

Commit ee08f53

Browse files
committed
api: Better explain the executors and how to configure them
1 parent 7fdcde1 commit ee08f53

4 files changed

Lines changed: 31 additions & 13 deletions

File tree

api/src/main/java/io/grpc/ClientInterceptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
* CallCredentials}. But a {@code ClientInterceptor} could set the {@code
3333
* CallCredentials} within the {@link CallOptions}.
3434
*
35+
* <p>From gRPC's perspective, interceptors don't generally exist and are more of a convenience.
36+
* Convenience APIs will use {@link ClientInterceptors} to convert the interceptor into a {@code
37+
* Channel}. Thus interceptors are an extension of the application and run on the same
38+
* threads and receive callbacks using the same executor.
39+
*
3540
* <p>The interceptor may be called for multiple {@link ClientCall calls} by one or more threads
3641
* without completing the previous ones first. Refer to the
3742
* {@link io.grpc.ClientCall.Listener ClientCall.Listener} docs for more details regarding thread

api/src/main/java/io/grpc/ManagedChannelBuilder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
9292
}
9393

9494
/**
95-
* Execute application code directly in the transport thread.
96-
*
97-
* <p>Depending on the underlying transport, using a direct executor may lead to substantial
98-
* performance improvements. However, it also requires the application to not block under
95+
* Execute application code directly in the transport thread. The application must not block under
9996
* any circumstances.
10097
*
98+
* <p>Depending on the underlying transport and the application code, using a direct executor may
99+
* lead to 10s of µs latency reduction but causes a substantial performance degradation when
100+
* misused.
101+
*
101102
* <p>Calling this method is semantically equivalent to calling {@link #executor(Executor)} and
102103
* passing in a direct executor. However, this is the preferred way as it may allow the transport
103104
* to perform special optimizations.
@@ -108,7 +109,10 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
108109
public abstract T directExecutor();
109110

110111
/**
111-
* Provides a custom executor.
112+
* Set the default executor for callbacks. This is used for async and future stub callbacks, but
113+
* can be overridden by {@link CallOptions#withExecutor} and {@code stub.withExecutor()}. Blocking
114+
* stubs specify a per-RPC executor. This is also used for {@link
115+
* ManagedChannel#notifyWhenStateChanged}.
112116
*
113117
* <p>It's an optional parameter. If the user has not provided an executor when the channel is
114118
* built, the builder will use a static cached thread pool.

api/src/main/java/io/grpc/ServerBuilder.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ public static ServerBuilder<?> forPort(int port) {
4545
}
4646

4747
/**
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
48+
* Execute application code directly in the transport thread. The application must not block under
5249
* any circumstances.
5350
*
51+
* <p>Depending on the underlying transport and the application code, using a direct executor may
52+
* lead to 10s of µs latency reduction but causes a substantial performance degradation when
53+
* misused.
54+
*
5455
* <p>Calling this method is semantically equivalent to calling {@link #executor(Executor)} and
5556
* passing in a direct executor. However, this is the preferred way as it may allow the transport
5657
* to perform special optimizations.
@@ -61,10 +62,11 @@ public static ServerBuilder<?> forPort(int port) {
6162
public abstract T directExecutor();
6263

6364
/**
64-
* Provides a custom executor.
65+
* Set the default executor for service callbacks.
6566
*
6667
* <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+
* built, the builder will use a static cached thread pool. Users are encouraged to specify their
69+
* own executor that limits the number of threads.
6870
*
6971
* <p>The server won't take ownership of the given executor. It's caller's responsibility to
7072
* shut down the executor when it's desired.
@@ -85,11 +87,13 @@ public static ServerBuilder<?> forPort(int port) {
8587
* it switches over. But if calling {@link ServerCallExecutorSupplier} returns null, the server
8688
* call is still handled by the default {@link #executor(Executor)} as a fallback.
8789
*
90+
* <p>If your {@code executorSupplier} runs quickly and always returns a non-{@code null}
91+
* executor, then you may want to use {@link #directExecutor} to reduce latency.
92+
*
8893
* @param executorSupplier the server call executor provider
8994
* @return this
9095
* @since 1.39.0
91-
*
92-
* */
96+
*/
9397
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8274")
9498
public T callExecutor(ServerCallExecutorSupplier executorSupplier) {
9599
return thisT();

api/src/main/java/io/grpc/ServerInterceptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
* <li>Delegating calls to other servers</li>
3030
* </ul>
3131
*
32+
* <p>From gRPC's perspective, interceptors don't generally exist and are more of a convenience.
33+
* Convenience APIs will use {@link ServerInterceptors} to convert the interceptor into a {@code
34+
* ServerCallHandler}. Thus interceptors are an extension of the application and run on the same
35+
* threads and receive callbacks using the same executor.
36+
*
3237
* <p>The interceptor may be called for multiple {@link ServerCall calls} by one or more threads
3338
* without completing the previous ones first. Refer to the
3439
* {@link io.grpc.ServerCall.Listener ServerCall.Listener} docs for more details regarding thread

0 commit comments

Comments
 (0)