Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolve/reject awakeables from ingress #254

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading