Skip to content
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
import io.grpc.protobuf.ProtoUtils;
import io.grpc.stub.ClientCalls;
Expand All @@ -36,10 +38,13 @@
import io.serverlessworkflow.impl.executors.CallableTask;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class GrpcExecutor implements CallableTask {

public static final String GRPC_CHANNEL_PROVIDER = "grpcChannelProvider";

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is public on purpose so upstream can resuse without copy pasting the string value


private final GrpcRequestContext requestContext;
private final WorkflowValueResolver<Map<String, Object>> arguments;
private final FileDescriptorProto fileDescriptorProto;
Expand All @@ -63,8 +68,6 @@ public CompletableFuture<WorkflowModel> apply(
private CompletableFuture<WorkflowModel> buildGrpcCallExecutor(
WorkflowContext workflowContext, TaskContext taskContext, Map<String, Object> arguments) {

Channel channel = GrpcChannelResolver.channel(workflowContext, taskContext, requestContext);

try {
Descriptors.FileDescriptor fileDescriptor =
Descriptors.FileDescriptor.buildFrom(
Expand All @@ -82,19 +85,33 @@ private CompletableFuture<WorkflowModel> buildGrpcCallExecutor(

MethodDescriptor.MethodType methodType = ProtobufMessageUtils.getMethodType(methodDescriptor);

Optional<Channel> providedChannel =
workflowContext
.definition()
.application()
.<Channel>additionalObject(GRPC_CHANNEL_PROVIDER, workflowContext, taskContext);
Channel channel =
providedChannel.orElseGet(
() ->
ManagedChannelBuilder.forAddress(requestContext.address(), requestContext.port())
.usePlaintext()
.build());
ClientCall<Message, Message> call =
buildClientCall(channel, methodType, serviceDescriptor, methodDescriptor);

return switch (methodType) {
case CLIENT_STREAMING ->
handleClientStreaming(workflowContext, arguments, methodDescriptor, call);
case BIDI_STREAMING ->
handleBidiStreaming(workflowContext, arguments, methodDescriptor, call);
case SERVER_STREAMING ->
handleServerStreaming(workflowContext, methodDescriptor, arguments, call);
case UNARY, UNKNOWN -> handleAsyncUnary(workflowContext, methodDescriptor, arguments, call);
};

CompletableFuture<WorkflowModel> result =
switch (methodType) {
case CLIENT_STREAMING ->
handleClientStreaming(workflowContext, arguments, methodDescriptor, call);
case BIDI_STREAMING ->
handleBidiStreaming(workflowContext, arguments, methodDescriptor, call);
case SERVER_STREAMING ->
handleServerStreaming(workflowContext, methodDescriptor, arguments, call);
case UNARY, UNKNOWN ->
handleAsyncUnary(workflowContext, methodDescriptor, arguments, call);
};
return providedChannel.isEmpty() && channel instanceof ManagedChannel managedChannel
? result.whenComplete((__, ___) -> managedChannel.shutdown())
: result;
Comment thread
fjtirado marked this conversation as resolved.
} catch (Descriptors.DescriptorValidationException | InvalidProtocolBufferException e) {
throw new WorkflowException(WorkflowError.runtime(taskContext, e).build());
}
Expand Down
Loading