Skip to content

Commit

Permalink
Fix #351, buildAndListen now fails (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Jun 10, 2024
1 parent ca3486a commit cd23a59
Showing 1 changed file with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import io.opentelemetry.api.OpenTelemetry;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.Http2Settings;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import java.util.*;
import java.util.concurrent.CompletionException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -120,17 +121,29 @@ public RestateHttpEndpointBuilder withRequestIdentityVerifier(
return this;
}

/** Build and listen on the specified port. */
public void buildAndListen(int port) {
build().listen(port).onComplete(RestateHttpEndpointBuilder::handleStart);
/**
* Build and listen on the specified port.
*
* <p>NOTE: this method will block for opening the socket and reserving the port. If you need a
* non-blocking variant, manually {@link #build()} the server and start listening it.
*
* @return The listening port
*/
public int buildAndListen(int port) {
return handleStart(build().listen(port));
}

/**
* Build and listen on the port specified by the environment variable {@code PORT}, or
* alternatively on the default {@code 9080} port.
*
* <p>NOTE: this method will block for opening the socket and reserving the port. If you need a
* non-blocking variant, manually {@link #build()} the server and start listening it.
*
* @return The listening port
*/
public void buildAndListen() {
build().listen().onComplete(RestateHttpEndpointBuilder::handleStart);
public int buildAndListen() {
return handleStart(build().listen());
}

/** Build the {@link HttpServer} serving the Restate service endpoint. */
Expand All @@ -145,11 +158,21 @@ public HttpServer build() {
return server;
}

private static void handleStart(AsyncResult<HttpServer> ar) {
if (ar.succeeded()) {
LOG.info("Restate HTTP Endpoint server started on port {}", ar.result().actualPort());
} else {
LOG.error("Restate HTTP Endpoint server start failed", ar.cause());
private static int handleStart(Future<HttpServer> fut) {
try {
HttpServer server = fut.toCompletionStage().toCompletableFuture().join();
LOG.info("Restate HTTP Endpoint server started on port {}", server.actualPort());
return server.actualPort();
} catch (CompletionException e) {
LOG.error("Restate HTTP Endpoint server start failed", e.getCause());
sneakyThrow(e.getCause());
// This is never reached
return -1;
}
}

@SuppressWarnings("unchecked")
private static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
throw (E) e;
}
}

0 comments on commit cd23a59

Please sign in to comment.