/
GrpcServiceFactory.java
133 lines (120 loc) · 5.54 KB
/
GrpcServiceFactory.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
/*
* Copyright © 2019, 2021 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.concurrent.api.Single;
import io.servicetalk.http.api.BlockingHttpService;
import io.servicetalk.http.api.BlockingStreamingHttpService;
import io.servicetalk.http.api.HttpServerContext;
import io.servicetalk.http.api.HttpService;
import io.servicetalk.http.api.StreamingHttpService;
import io.servicetalk.transport.api.ExecutionContext;
/**
* A factory for binding a <a href="https://www.grpc.io">gRPC</a> service to a server using a {@link ServerBinder}.
*
* @param <Service> Type for service
*/
public abstract class GrpcServiceFactory<Service extends GrpcService> {
private final GrpcRoutes<Service> routes;
/**
* Creates new instance.
*
* @param routes {@link GrpcRoutes} that will hold the routes for the constructed service.
*/
protected GrpcServiceFactory(final GrpcRoutes<Service> routes) {
this.routes = routes;
}
/**
* Merges multiple {@link GrpcServiceFactory factories} into a single instance.
* @param factories instanes of {@link GrpcServiceFactory} to merge.
* @return An aggregate {@link GrpcServiceFactory}.
*/
@SuppressWarnings("unchecked")
public static GrpcServiceFactory<?> merge(final GrpcServiceFactory<?>... factories) {
if (factories.length == 1) {
return factories[0];
}
final GrpcRoutes<?>[] routes = new GrpcRoutes[factories.length];
for (int i = 0; i < factories.length; i++) {
final GrpcServiceFactory<?> factory = factories[i];
routes[i] = factory.routes;
}
return new MergedServiceFactory(routes);
}
/**
* Use the passed {@link ServerBinder} to bind an appropriate
* <a href="https://www.grpc.io">gRPC</a> service for the server.
*
* @param binder {@link ServerBinder} to bind <a href="https://www.grpc.io">gRPC</a> service to the server.
* @param executionContext {@link ExecutionContext} to use for the service.
* @return A {@link Single} that completes when the server is successfully started or terminates with an error if
* the server could not be started.
*/
public final Single<GrpcServerContext> bind(final ServerBinder binder, final ExecutionContext<?> executionContext) {
return routes.bind(binder, DefaultGrpcExecutionContext.from(executionContext));
}
/**
* A utility to bind an HTTP service for <a href="https://www.grpc.io">gRPC</a> with an
* appropriate programming model.
*/
public interface ServerBinder {
/**
* Binds an {@link HttpService} to the associated server.
* <p>
* If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on {@code address}.
*
* @param service {@link HttpService} to bind.
* @return A {@link Single} that completes when the server is successfully started or terminates with an error
* if the server could not be started.
*/
Single<HttpServerContext> bind(HttpService service);
/**
* Binds a {@link StreamingHttpService} to the associated server.
* <p>
* If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on {@code address}.
*
* @param service {@link StreamingHttpService} to bind.
* @return A {@link Single} that completes when the server is successfully started or terminates with an error
* if the server could not be started.
*/
Single<HttpServerContext> bindStreaming(StreamingHttpService service);
/**
* Binds a {@link BlockingHttpService} to the associated server.
* <p>
* If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on {@code address}.
*
* @param service {@link BlockingHttpService} to bind.
* @return A {@link Single} that completes when the server is successfully started or terminates with an error
* if the server could not be started.
*/
Single<HttpServerContext> bindBlocking(BlockingHttpService service);
/**
* Binds a {@link BlockingStreamingHttpService} to the associated server.
* <p>
* If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on {@code address}.
*
* @param service {@link BlockingStreamingHttpService} to bind.
* @return A {@link Single} that completes when the server is successfully started or terminates with an error
* if the server could not be started.
*/
Single<HttpServerContext> bindBlockingStreaming(BlockingStreamingHttpService service);
}
private static final class MergedServiceFactory extends GrpcServiceFactory {
@SuppressWarnings("unchecked")
MergedServiceFactory(final GrpcRoutes... routes) {
super(GrpcRoutes.merge(routes));
}
}
}