-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathRestateHttpEndpointBean.java
More file actions
202 lines (177 loc) · 6.78 KB
/
Copy pathRestateHttpEndpointBean.java
File metadata and controls
202 lines (177 loc) · 6.78 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
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.springboot;
import dev.restate.sdk.auth.signing.RestateRequestIdentityVerifier;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.http.vertx.HttpEndpointRequestHandler;
import dev.restate.sdk.http.vertx.RestateHttpServer;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
/** Restate HTTP Endpoint serving {@link Endpoint} */
public class RestateHttpEndpointBean implements InitializingBean, SmartLifecycle {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final RestateHttpServerProperties restateHttpServerProperties;
private volatile boolean running;
private final @Nullable HttpEndpointRequestHandler handler;
private volatile @Nullable Vertx vertx;
private volatile int actualPort = -1;
public RestateHttpEndpointBean(
Endpoint endpoint, RestateHttpServerProperties restateHttpServerProperties) {
this.restateHttpServerProperties = restateHttpServerProperties;
this.handler =
HttpEndpointRequestHandler.fromEndpoint(
endpoint, restateHttpServerProperties.isDisableBidirectionalStreaming());
}
@Deprecated
public RestateHttpEndpointBean(
ApplicationContext applicationContext,
RestateEndpointProperties restateEndpointProperties,
RestateHttpServerProperties restateHttpServerProperties) {
this.restateHttpServerProperties = restateHttpServerProperties;
Map<String, Object> restateComponents =
applicationContext.getBeansWithAnnotation(RestateComponent.class);
if (restateComponents.isEmpty()) {
logger.info("No @RestateComponent discovered");
this.handler = null;
// Don't start anything, if no service is registered
return;
}
var builder = Endpoint.builder();
for (var componentEntry : restateComponents.entrySet()) {
// Get configurator, if any
RestateComponent restateComponent =
applicationContext.findAnnotationOnBean(componentEntry.getKey(), RestateComponent.class);
RestateServiceConfigurator configurator = c -> {};
if (restateComponent != null && !restateComponent.configuration().isEmpty()) {
configurator =
applicationContext.getBean(
restateComponent.configuration(), RestateServiceConfigurator.class);
}
builder = builder.bind(componentEntry.getValue(), configurator);
}
if (restateEndpointProperties.isEnablePreviewContext()) {
builder = builder.enablePreviewContext();
}
if (restateEndpointProperties.getIdentityKey() != null) {
builder.withRequestIdentityVerifier(
RestateRequestIdentityVerifier.fromKey(restateEndpointProperties.getIdentityKey()));
}
Endpoint endpoint = builder.build();
this.handler =
HttpEndpointRequestHandler.fromEndpoint(
endpoint, restateHttpServerProperties.isDisableBidirectionalStreaming());
}
@Deprecated
@Override
public void afterPropertiesSet() {}
@Override
public void start() {
HttpEndpointRequestHandler handler = this.handler;
if (handler == null) {
// No service registered, nothing to start.
this.running = true;
return;
}
int instances =
this.restateHttpServerProperties.getEventLoops() > 0
? this.restateHttpServerProperties.getEventLoops()
: VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE;
Vertx vertx = Vertx.vertx(new VertxOptions().setEventLoopPoolSize(instances));
this.vertx = vertx;
// A negative port makes all the server instances share the same random port, whereas port 0
// would make each instance bind a different random port.
int listenPort =
this.restateHttpServerProperties.getPort() == 0
? -1
: this.restateHttpServerProperties.getPort();
AtomicInteger actualPortHolder = new AtomicInteger();
try {
// Deploy one HttpServer (and one handler) per event loop, spreading connections across them.
vertx
.deployVerticle(
() -> new ServerVerticle(handler, listenPort, actualPortHolder),
new DeploymentOptions().setInstances(instances))
.toCompletionStage()
.toCompletableFuture()
.get();
this.actualPort = actualPortHolder.get();
logger.info(
"Started Restate Spring HTTP server on port {} with {} event loop(s)",
this.actualPort,
instances);
} catch (Exception e) {
logger.error(
"Error when starting Restate Spring HTTP server on port {}",
this.restateHttpServerProperties.getPort(),
e);
}
this.running = true;
}
@Override
public void stop() {
try {
Vertx vertx = this.vertx;
if (vertx != null) {
vertx.close().toCompletionStage().toCompletableFuture().get();
this.vertx = null;
}
logger.info("Stopped Restate Spring HTTP server");
} catch (Exception e) {
logger.error("Error when stopping the Restate Spring HTTP server", e);
}
this.actualPort = -1;
this.running = false;
}
@Override
public boolean isRunning() {
return this.running;
}
public int actualPort() {
if (!this.isRunning()) {
return -1;
}
return this.actualPort;
}
/**
* Verticle deployed once per event loop, each creating its own {@link
* HttpEndpointRequestHandler}.
*/
private static final class ServerVerticle extends AbstractVerticle {
private final HttpEndpointRequestHandler handler;
private final int port;
private final AtomicInteger actualPort;
private ServerVerticle(HttpEndpointRequestHandler handler, int port, AtomicInteger actualPort) {
this.handler = handler;
this.port = port;
this.actualPort = actualPort;
}
@Override
public void start(Promise<Void> startPromise) {
RestateHttpServer.fromHandler(vertx, handler)
.listen(port)
.onSuccess(
server -> {
actualPort.set(server.actualPort());
startPromise.complete();
})
.onFailure(startPromise::fail);
}
}
}