-
Notifications
You must be signed in to change notification settings - Fork 173
/
GrpcClientBuilder.java
333 lines (297 loc) · 14.9 KB
/
GrpcClientBuilder.java
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright © 2019 Apple Inc. and the ServiceTalk project 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.servicetalk.grpc.api;
import io.servicetalk.buffer.api.BufferAllocator;
import io.servicetalk.client.api.AutoRetryStrategyProvider;
import io.servicetalk.client.api.ConnectionFactoryFilter;
import io.servicetalk.client.api.ServiceDiscoverer;
import io.servicetalk.client.api.ServiceDiscovererEvent;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.http.api.FilterableStreamingHttpConnection;
import io.servicetalk.http.api.HttpExecutionStrategy;
import io.servicetalk.http.api.HttpLoadBalancerFactory;
import io.servicetalk.http.api.HttpProtocolConfig;
import io.servicetalk.http.api.StreamingHttpClientFilter;
import io.servicetalk.http.api.StreamingHttpClientFilterFactory;
import io.servicetalk.http.api.StreamingHttpConnectionFilterFactory;
import io.servicetalk.http.api.StreamingHttpRequest;
import io.servicetalk.http.api.StreamingHttpRequester;
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.logging.api.LogLevel;
import io.servicetalk.transport.api.ClientSslConfig;
import io.servicetalk.transport.api.IoExecutor;
import java.net.SocketOption;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Predicate;
import static io.servicetalk.concurrent.api.Single.failed;
import static io.servicetalk.grpc.api.GrpcStatus.fromThrowable;
/**
* A builder for building a <a href="https://www.grpc.io">gRPC</a> client.
*
* @param <U> the type of address before resolution (unresolved address)
* @param <R> the type of address after resolution (resolved address)
*/
public abstract class GrpcClientBuilder<U, R>
implements SingleAddressGrpcClientBuilder<U, R, ServiceDiscovererEvent<R>> {
private boolean appendedCatchAllFilter;
@Override
public abstract GrpcClientBuilder<U, R> ioExecutor(IoExecutor ioExecutor);
@Override
public abstract GrpcClientBuilder<U, R> bufferAllocator(BufferAllocator allocator);
@Override
public abstract GrpcClientBuilder<U, R> executionStrategy(GrpcExecutionStrategy strategy);
@Override
public abstract <T> GrpcClientBuilder<U, R> socketOption(SocketOption<T> option, T value);
@Override
@Deprecated
public abstract GrpcClientBuilder<U, R> enableWireLogging(String loggerName);
@Override
public abstract GrpcClientBuilder<U, R> enableWireLogging(String loggerName, LogLevel logLevel,
BooleanSupplier logUserData);
@Override
public abstract GrpcClientBuilder<U, R> protocols(HttpProtocolConfig... protocols);
@Override
public abstract GrpcClientBuilder<U, R> appendConnectionFactoryFilter(
ConnectionFactoryFilter<R, FilterableStreamingHttpConnection> factory);
@Override
public abstract GrpcClientBuilder<U, R> appendConnectionFilter(StreamingHttpConnectionFilterFactory factory);
@Override
public abstract GrpcClientBuilder<U, R> appendConnectionFilter(Predicate<StreamingHttpRequest> predicate,
StreamingHttpConnectionFilterFactory factory);
@Deprecated
@Override
public abstract GrpcClientSecurityConfigurator<U, R> secure();
@Override
public abstract GrpcClientBuilder<U, R> sslConfig(ClientSslConfig sslConfig);
@Override
public abstract GrpcClientBuilder<U, R> autoRetryStrategy(
AutoRetryStrategyProvider autoRetryStrategyProvider);
@Override
public abstract GrpcClientBuilder<U, R> unresolvedAddressToHost(
Function<U, CharSequence> unresolvedAddressToHostFunction);
@Override
public abstract GrpcClientBuilder<U, R> disableHostHeaderFallback();
@Override
public abstract GrpcClientBuilder<U, R> serviceDiscoverer(
ServiceDiscoverer<U, R, ServiceDiscovererEvent<R>> serviceDiscoverer);
@Override
public abstract GrpcClientBuilder<U, R> loadBalancerFactory(HttpLoadBalancerFactory<R> loadBalancerFactory);
/**
* Append the filter to the chain of filters used to decorate the client created by this builder.
* <p>
* The order of execution of these filters are in order of append. If 3 filters are added as follows:
* <pre>
* builder.append(filter1).append(filter2).append(filter3)
* </pre>
* making a request to a client wrapped by this filter chain the order of invocation of these filters will be:
* <pre>
* filter1 => filter2 => filter3 => client
* </pre>
*
* @param factory {@link StreamingHttpClientFilterFactory} to decorate a client for the purpose of filtering.
* @return {@code this}
*/
public final GrpcClientBuilder<U, R> appendHttpClientFilter(StreamingHttpClientFilterFactory factory) {
appendCatchAllFilterIfRequired();
doAppendHttpClientFilter(factory);
return this;
}
/**
* Append the filter to the chain of filters used to decorate the client created by this builder, for every request
* that passes the provided {@link Predicate}.
* <p>
* The order of execution of these filters are in order of append. If 3 filters are added as follows:
* <pre>
* builder.append(filter1).append(filter2).append(filter3)
* </pre>
* making a request to a client wrapped by this filter chain the order of invocation of these filters will be:
* <pre>
* filter1 => filter2 => filter3 => client
* </pre>
*
* @param predicate the {@link Predicate} to test if the filter must be applied.
* @param factory {@link StreamingHttpClientFilterFactory} to decorate a client for the purpose of filtering.
* @return {@code this}
*/
public final GrpcClientBuilder<U, R> appendHttpClientFilter(Predicate<StreamingHttpRequest> predicate,
StreamingHttpClientFilterFactory factory) {
appendCatchAllFilterIfRequired();
doAppendHttpClientFilter(predicate, factory);
return this;
}
/**
* Builds a <a href="https://www.grpc.io">gRPC</a> client.
*
* @param clientFactory {@link GrpcClientFactory} to use.
* @param <Client> <a href="https://www.grpc.io">gRPC</a> service that any client built from
* this factory represents.
* @param <Filter> Type for client filter
* @param <FilterableClient> Type of filterable client.
* @param <FilterFactory> Type of {@link GrpcClientFilterFactory}
*
* @return A <a href="https://www.grpc.io">gRPC</a> client.
*/
public final <Client extends GrpcClient<?>,
Filter extends FilterableClient, FilterableClient extends FilterableGrpcClient,
FilterFactory extends GrpcClientFilterFactory<Filter, FilterableClient>> Client
build(GrpcClientFactory<Client, ?, Filter, FilterableClient, FilterFactory> clientFactory) {
return clientFactory.newClientForCallFactory(newGrpcClientCallFactory());
}
/**
* Builds a blocking <a href="https://www.grpc.io">gRPC</a> client.
*
* @param clientFactory {@link GrpcClientFactory} to use.
* @param <BlockingClient> Blocking <a href="https://www.grpc.io">gRPC</a> service that any
* client built from this builder represents.
* @param <Filter> Type for client filter
* @param <FilterableClient> Type of filterable client.
* @param <FilterFactory> Type of {@link GrpcClientFilterFactory}
*
* @return A blocking <a href="https://www.grpc.io">gRPC</a> client.
*/
public final <BlockingClient extends BlockingGrpcClient<?>,
Filter extends FilterableClient, FilterableClient extends FilterableGrpcClient,
FilterFactory extends GrpcClientFilterFactory<Filter, FilterableClient>> BlockingClient
buildBlocking(GrpcClientFactory<?, BlockingClient, Filter, FilterableClient, FilterFactory> clientFactory) {
return clientFactory.newBlockingClientForCallFactory(newGrpcClientCallFactory());
}
/**
* Returns a {@link MultiClientBuilder} to be used to create multiple clients sharing the same underlying transport
* instance.
*
* @return A blocking <a href="https://www.grpc.io">gRPC</a> client.
*/
public final MultiClientBuilder buildMulti() {
GrpcClientCallFactory callFactory = newGrpcClientCallFactory();
return new MultiClientBuilder() {
@Override
public <Client extends GrpcClient<?>,
Filter extends FilterableClient, FilterableClient extends FilterableGrpcClient,
FilterFactory extends GrpcClientFilterFactory<Filter, FilterableClient>> Client
build(final GrpcClientFactory<Client, ?, Filter, FilterableClient, FilterFactory> clientFactory) {
return clientFactory.newClient(callFactory);
}
@Override
public <BlockingClient extends BlockingGrpcClient<?>,
Filter extends FilterableClient, FilterableClient extends FilterableGrpcClient,
FilterFactory extends GrpcClientFilterFactory<Filter, FilterableClient>> BlockingClient
buildBlocking(
final GrpcClientFactory<?, BlockingClient, Filter, FilterableClient, FilterFactory> clientFactory) {
return clientFactory.newBlockingClient(callFactory);
}
};
}
/**
* Create a new {@link GrpcClientCallFactory}.
*
* @return A new {@link GrpcClientCallFactory}.
*/
protected abstract GrpcClientCallFactory newGrpcClientCallFactory();
/**
* Append the filter to the chain of filters used to decorate the client created by this builder.
* <p>
* The order of execution of these filters are in order of append. If 3 filters are added as follows:
* <pre>
* builder.append(filter1).append(filter2).append(filter3)
* </pre>
* making a request to a client wrapped by this filter chain the order of invocation of these filters will be:
* <pre>
* filter1 => filter2 => filter3 => client
* </pre>
*
* @param factory {@link StreamingHttpClientFilterFactory} to decorate a client for the purpose of filtering.
*/
protected abstract void doAppendHttpClientFilter(StreamingHttpClientFilterFactory factory);
/**
* Append the filter to the chain of filters used to decorate the client created by this builder, for every request
* that passes the provided {@link Predicate}.
* <p>
* The order of execution of these filters are in order of append. If 3 filters are added as follows:
* <pre>
* builder.append(filter1).append(filter2).append(filter3)
* </pre>
* making a request to a client wrapped by this filter chain the order of invocation of these filters will be:
* <pre>
* filter1 => filter2 => filter3 => client
* </pre>
*
* @param predicate the {@link Predicate} to test if the filter must be applied.
* @param factory {@link StreamingHttpClientFilterFactory} to decorate a client for the purpose of filtering.
*/
protected abstract void doAppendHttpClientFilter(Predicate<StreamingHttpRequest> predicate,
StreamingHttpClientFilterFactory factory);
private void appendCatchAllFilterIfRequired() {
if (!appendedCatchAllFilter) {
doAppendHttpClientFilter(client -> new StreamingHttpClientFilter(client) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate,
final HttpExecutionStrategy strategy,
final StreamingHttpRequest request) {
final Single<StreamingHttpResponse> resp;
try {
resp = super.request(delegate, strategy, request);
} catch (Throwable t) {
return failed(toGrpcException(t));
}
return resp.recoverWith(t -> failed(toGrpcException(t)));
}
});
appendedCatchAllFilter = true;
}
}
private static GrpcStatusException toGrpcException(Throwable cause) {
return fromThrowable(cause).asException();
}
/**
* An interface to create multiple <a href="https://www.grpc.io">gRPC</a> clients sharing the
* same underlying transport instance.
*/
public interface MultiClientBuilder {
/**
* Builds a <a href="https://www.grpc.io">gRPC</a> client.
*
* @param clientFactory {@link GrpcClientFactory} to use.
* @param <Client> <a href="https://www.grpc.io">gRPC</a> service that any client built
* from this factory represents.
* @param <Filter> Type for client filter
* @param <FilterableClient> Type of filterable client.
* @param <FilterFactory> Type of {@link GrpcClientFilterFactory}
*
* @return A <a href="https://www.grpc.io">gRPC</a> client.
*/
<Client extends GrpcClient<?>,
Filter extends FilterableClient, FilterableClient extends FilterableGrpcClient,
FilterFactory extends GrpcClientFilterFactory<Filter, FilterableClient>> Client
build(GrpcClientFactory<Client, ?, Filter, FilterableClient, FilterFactory> clientFactory);
/**
* Builds a blocking <a href="https://www.grpc.io">gRPC</a> client.
*
* @param clientFactory {@link GrpcClientFactory} to use.
* @param <BlockingClient> Blocking <a href="https://www.grpc.io">gRPC</a> service that
* any client built from this builder represents.
* @param <Filter> Type for client filter
* @param <FilterableClient> Type of filterable client.
* @param <FilterFactory> Type of {@link GrpcClientFilterFactory}
*
* @return A blocking <a href="https://www.grpc.io">gRPC</a> client.
*/
<BlockingClient extends BlockingGrpcClient<?>,
Filter extends FilterableClient, FilterableClient extends FilterableGrpcClient,
FilterFactory extends GrpcClientFilterFactory<Filter, FilterableClient>> BlockingClient
buildBlocking(GrpcClientFactory<?, BlockingClient, Filter, FilterableClient, FilterFactory> clientFactory);
}
}