forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerBuilder.java
More file actions
283 lines (260 loc) · 9.72 KB
/
ServerBuilder.java
File metadata and controls
283 lines (260 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright 2015 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.InputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/**
* A builder for {@link Server} instances.
*
* @param <T> The concrete type of this builder.
* @since 1.0.0
*/
public abstract class ServerBuilder<T extends ServerBuilder<T>> {
/**
* Static factory for creating a new ServerBuilder.
*
* @param port the port to listen on
* @since 1.0.0
*/
public static ServerBuilder<?> forPort(int port) {
return ServerProvider.provider().builderForPort(port);
}
/**
* Execute application code directly in the transport thread.
*
* <p>Depending on the underlying transport, using a direct executor may lead to substantial
* performance improvements. However, it also requires the application to not block under
* any circumstances.
*
* <p>Calling this method is semantically equivalent to calling {@link #executor(Executor)} and
* passing in a direct executor. However, this is the preferred way as it may allow the transport
* to perform special optimizations.
*
* @return this
* @since 1.0.0
*/
public abstract T directExecutor();
/**
* Provides a custom executor.
*
* <p>It's an optional parameter. If the user has not provided an executor when the server is
* built, the builder will use a static cached thread pool.
*
* <p>The server won't take ownership of the given executor. It's caller's responsibility to
* shut down the executor when it's desired.
*
* @return this
* @since 1.0.0
*/
public abstract T executor(@Nullable Executor executor);
/**
* Adds a service implementation to the handler registry.
*
* @param service ServerServiceDefinition object
* @return this
* @since 1.0.0
*/
public abstract T addService(ServerServiceDefinition service);
/**
* Adds a service implementation to the handler registry. If bindableService implements
* {@link InternalNotifyOnServerBuild}, the service will receive a reference to the generated
* server instance upon build().
*
* @param bindableService BindableService object
* @return this
* @since 1.0.0
*/
public abstract T addService(BindableService bindableService);
/**
* Adds a {@link ServerInterceptor} that is run for all services on the server. Interceptors
* added through this method always run before per-service interceptors added through {@link
* ServerInterceptors}. Interceptors run in the reverse order in which they are added, just as
* with consecutive calls to {@code ServerInterceptors.intercept()}.
*
* @param interceptor the all-service interceptor
* @return this
* @since 1.5.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3117")
public T intercept(ServerInterceptor interceptor) {
throw new UnsupportedOperationException();
}
/**
* Adds a {@link ServerTransportFilter}. The order of filters being added is the order they will
* be executed.
*
* @return this
* @since 1.2.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2132")
public T addTransportFilter(ServerTransportFilter filter) {
throw new UnsupportedOperationException();
}
/**
* Adds a {@link ServerStreamTracer.Factory} to measure server-side traffic. The order of
* factories being added is the order they will be executed. Tracers should not
*
* @return this
* @since 1.3.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861")
public T addStreamTracerFactory(ServerStreamTracer.Factory factory) {
throw new UnsupportedOperationException();
}
/**
* Sets a fallback handler registry that will be looked up in if a method is not found in the
* primary registry. The primary registry (configured via {@code addService()}) is faster but
* immutable. The fallback registry is more flexible and allows implementations to mutate over
* time and load services on-demand.
*
* @return this
* @since 1.0.0
*/
public abstract T fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry);
/**
* Makes the server use TLS.
*
* @param certChain file containing the full certificate chain
* @param privateKey file containing the private key
*
* @return this
* @throws UnsupportedOperationException if the server does not support TLS.
* @since 1.0.0
*/
public abstract T useTransportSecurity(File certChain, File privateKey);
/**
* Makes the server use TLS.
*
* @param certChain InputStream containing the full certificate chain
* @param privateKey InputStream containing the private key
*
* @return this
* @throws UnsupportedOperationException if the server does not support TLS, or does not support
* reading these files from an InputStream.
* @since 1.12.0
*/
public T useTransportSecurity(InputStream certChain, InputStream privateKey) {
throw new UnsupportedOperationException();
}
/**
* Set the decompression registry for use in the channel. This is an advanced API call and
* shouldn't be used unless you are using custom message encoding. The default supported
* decompressors are in {@code DecompressorRegistry.getDefaultInstance}.
*
* @return this
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
public abstract T decompressorRegistry(@Nullable DecompressorRegistry registry);
/**
* Set the compression registry for use in the channel. This is an advanced API call and
* shouldn't be used unless you are using custom message encoding. The default supported
* compressors are in {@code CompressorRegistry.getDefaultInstance}.
*
* @return this
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
public abstract T compressorRegistry(@Nullable CompressorRegistry registry);
/**
* Sets the permitted time for new connections to complete negotiation handshakes before being
* killed.
*
* @return this
* @throws IllegalArgumentException if timeout is negative
* @throws UnsupportedOperationException if unsupported
* @since 1.8.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3706")
public T handshakeTimeout(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
/**
* Sets the maximum message size allowed to be received on the server. If not called,
* defaults to 4 MiB. The default provides protection to servers who haven't considered the
* possibility of receiving large messages while trying to be large enough to not be hit in normal
* usage.
*
* <p>This method is advisory, and implementations may decide to not enforce this. Currently,
* the only known transport to not enforce this is {@code InProcessServer}.
*
* @param bytes the maximum number of bytes a single message can be.
* @return this
* @throws IllegalArgumentException if bytes is negative.
* @throws UnsupportedOperationException if unsupported.
* @since 1.13.0
*/
public T maxInboundMessageSize(int bytes) {
// intentional noop rather than throw, this method is only advisory.
Preconditions.checkArgument(bytes >= 0, "bytes must be >= 0");
return thisT();
}
/**
* Sets the maximum size of metadata allowed to be received. {@code Integer.MAX_VALUE} disables
* the enforcement. The default is implementation-dependent, but is not generally less than 8 KiB
* and may be unlimited.
*
* <p>This is cumulative size of the metadata. The precise calculation is
* implementation-dependent, but implementations are encouraged to follow the calculation used for
* <a href="http://httpwg.org/specs/rfc7540.html#rfc.section.6.5.2">
* HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE</a>. It sums the bytes from each entry's key and value,
* plus 32 bytes of overhead per entry.
*
* @param bytes the maximum size of received metadata
* @return this
* @throws IllegalArgumentException if bytes is non-positive
* @since 1.17.0
*/
public T maxInboundMetadataSize(int bytes) {
Preconditions.checkArgument(bytes > 0, "maxInboundMetadataSize must be > 0");
// intentional noop rather than throw, this method is only advisory.
return thisT();
}
/**
* Sets the BinaryLog object that this server should log to. The server does not take
* ownership of the object, and users are responsible for calling {@link BinaryLog#close()}.
*
* @param binaryLog the object to provide logging.
* @return this
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017")
public T setBinaryLog(BinaryLog binaryLog) {
throw new UnsupportedOperationException();
}
/**
* Builds a server using the given parameters.
*
* <p>The returned service will not been started or be bound a port. You will need to start it
* with {@link Server#start()}.
*
* @return a new Server
* @since 1.0.0
*/
public abstract Server build();
/**
* Returns the correctly typed version of the builder.
*/
private T thisT() {
@SuppressWarnings("unchecked")
T thisT = (T) this;
return thisT;
}
}