Skip to content

Commit

Permalink
Add resolve/reject awakeables from ingress. Fix #246 (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Mar 21, 2024
1 parent 4205690 commit cae0d3b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.http.HttpResponse;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.jspecify.annotations.NonNull;

public class DefaultIngressClient implements IngressClient {

Expand Down Expand Up @@ -92,6 +93,75 @@ public <Req> CompletableFuture<String> sendAsync(
});
}

@Override
public AwakeableHandle awakeableHandle(String id) {
return new AwakeableHandle() {
@Override
public <T> CompletableFuture<Void> resolve(Serde<T> serde, @NonNull T payload) {
// Prepare request
var reqBuilder =
HttpRequest.newBuilder().uri(URI.create("/restate/awakeables/" + id + "/resolve"));

// Add content-type
if (serde.contentType() != null) {
reqBuilder.header("content-type", serde.contentType());
}

// Add headers
headers.forEach(reqBuilder::header);

// Build and Send request
HttpRequest request =
reqBuilder
.POST(HttpRequest.BodyPublishers.ofByteArray(serde.serialize(payload)))
.build();
return httpClient
.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray())
.handle(
(response, throwable) -> {
if (throwable != null) {
throw new IngressException("Error when executing the request", throwable);
}

if (response.statusCode() >= 300) {
handleNonSuccessResponse(response);
}

return null;
});
}

@Override
public CompletableFuture<Void> reject(String reason) {
// Prepare request
var reqBuilder =
HttpRequest.newBuilder()
.uri(URI.create("/restate/awakeables/" + id + "/reject"))
.header("content-type", "text-plain");

// Add headers
headers.forEach(reqBuilder::header);

// Build and Send request
HttpRequest request = reqBuilder.POST(HttpRequest.BodyPublishers.ofString(reason)).build();
return httpClient
.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray())
.handle(
(response, throwable) -> {
if (throwable != null) {
throw new IngressException("Error when executing the request", throwable);
}

if (response.statusCode() >= 300) {
handleNonSuccessResponse(response);
}

return null;
});
}
};
}

private URI toRequestURI(Target target, boolean isSend) {
StringBuilder builder = new StringBuilder();
builder.append("/").append(target.getComponent());
Expand Down
29 changes: 29 additions & 0 deletions sdk-common/src/main/java/dev/restate/sdk/client/IngressClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.jspecify.annotations.NonNull;

public interface IngressClient {

Expand Down Expand Up @@ -67,6 +68,34 @@ default <Req> String send(Target target, Serde<Req> reqSerde, Req req) throws In
return send(target, reqSerde, req, RequestOptions.DEFAULT);
}

/**
* Create a new {@link AwakeableHandle} for the provided identifier. You can use it to {@link
* AwakeableHandle#resolve(Serde, Object)} or {@link AwakeableHandle#reject(String)} an Awakeable
* from the ingress.
*/
AwakeableHandle awakeableHandle(String id);

/**
* This class represents a handle to an Awakeable. It can be used to complete awakeables from the
* ingress
*/
interface AwakeableHandle {
/**
* Complete with success the Awakeable.
*
* @param serde used to serialize the Awakeable result payload.
* @param payload the result payload. MUST NOT be null.
*/
<T> CompletableFuture<Void> resolve(Serde<T> serde, @NonNull T payload);

/**
* Complete with failure the Awakeable.
*
* @param reason the rejection reason. MUST NOT be null.
*/
CompletableFuture<Void> reject(String reason);
}

static IngressClient defaultClient(String baseUri) {
return defaultClient(baseUri, Collections.emptyMap());
}
Expand Down

0 comments on commit cae0d3b

Please sign in to comment.