Refactor DaprWorkflowClientBuilderFactory and WorkflowRuntimeOptions (#1244)

This commit refactors the DaprWorkflowClientBuilderFactory and WorkflowRuntimeOptions classes.

In DaprWorkflowClientBuilderFactory:
- Added a new method, UseGrpcChannelOptions, to allow the use of custom GrpcChannelOptions for creating the GrpcChannel.
- Updated the UseGrpc method to use the GrpcChannelOptions provided by the WorkflowRuntimeOptions.

In WorkflowRuntimeOptions:
- Added a new property, GrpcChannelOptions, to store the custom GrpcChannelOptions.
- Added a new method, UseGrpcChannelOptions, to set the GrpcChannelOptions.

These changes improve the flexibility and customization options for the Dapr workflow client.

Signed-off-by: Michiel van Praat <michiel.vanpraat@humandigital.nl>
Co-authored-by: Michiel van Praat <michiel.vanpraat@humandigital.nl>
This commit is contained in:
Ruud van Falier 2024-11-04 21:00:26 +01:00 committed by GitHub
parent 7356c9dea2
commit e7d3c47615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 6 deletions

View File

@ -50,6 +50,9 @@ internal sealed class DaprWorkflowClientBuilderFactory
{
_services.AddDurableTaskClient(builder =>
{
WorkflowRuntimeOptions options = new();
configure?.Invoke(options);
var apiToken = DaprDefaults.GetDefaultDaprApiToken(_configuration);
var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(_configuration);
@ -57,10 +60,15 @@ internal sealed class DaprWorkflowClientBuilderFactory
if (!string.IsNullOrWhiteSpace(apiToken))
{
httpClient.DefaultRequestHeaders.Add( "Dapr-Api-Token", apiToken);
httpClient.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
}
builder.UseGrpc(GrpcChannel.ForAddress(grpcEndpoint, new GrpcChannelOptions { HttpClient = httpClient }));
var channelOptions = options.GrpcChannelOptions ?? new GrpcChannelOptions
{
HttpClient = httpClient
};
builder.UseGrpc(GrpcChannel.ForAddress(grpcEndpoint, channelOptions));
builder.RegisterDirectly();
});
@ -81,8 +89,12 @@ internal sealed class DaprWorkflowClientBuilderFactory
httpClient.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken);
}
builder.UseGrpc(
GrpcChannel.ForAddress(grpcEndpoint, new GrpcChannelOptions { HttpClient = httpClient }));
var channelOptions = options.GrpcChannelOptions ?? new GrpcChannelOptions
{
HttpClient = httpClient
};
builder.UseGrpc(GrpcChannel.ForAddress(grpcEndpoint, channelOptions));
}
else
{

View File

@ -11,6 +11,8 @@
// limitations under the License.
// ------------------------------------------------------------------------
using Grpc.Net.Client;
namespace Dapr.Workflow
{
using System;
@ -29,6 +31,11 @@ namespace Dapr.Workflow
/// </summary>
readonly Dictionary<string, Action<DurableTaskRegistry>> factories = new();
/// <summary>
/// Override GrpcChannelOptions.
/// </summary>
internal GrpcChannelOptions? GrpcChannelOptions { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="WorkflowRuntimeOptions"/> class.
/// </summary>
@ -118,6 +125,15 @@ namespace Dapr.Workflow
});
}
/// <summary>
/// Uses the provided <paramref name="grpcChannelOptions" /> for creating the <see cref="GrpcChannel" />.
/// </summary>
/// <param name="grpcChannelOptions">The <see cref="GrpcChannelOptions" /> to use for creating the <see cref="GrpcChannel" />.</param>
public void UseGrpcChannelOptions(GrpcChannelOptions grpcChannelOptions)
{
this.GrpcChannelOptions = grpcChannelOptions;
}
/// <summary>
/// Method to add workflows and activities to the registry.
/// </summary>

View File

@ -31,6 +31,7 @@ namespace Dapr.E2E.Test
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Serilog;
using Grpc.Net.Client;
/// <summary>
/// Startup class.
@ -96,6 +97,25 @@ namespace Dapr.E2E.Test
return Task.FromResult($"We are shipping {input} to the customer using our hoard of drones!");
});
});
services.AddDaprWorkflow(options =>
{
// Example of registering a "StartOrder" workflow function
options.RegisterWorkflow<string, string>("StartLargeOrder", implementation: async (context, input) =>
{
var itemToPurchase = input;
itemToPurchase = await context.WaitForExternalEventAsync<string>("FinishLargeOrder");
return itemToPurchase;
});
options.RegisterActivity<string, string>("FinishLargeOrder", implementation: (context, input) =>
{
return Task.FromResult($"We are finishing, it's huge!");
});
options.UseGrpcChannelOptions(new GrpcChannelOptions
{
MaxReceiveMessageSize = 32 * 1024 * 1024,
MaxSendMessageSize = 32 * 1024 * 1024
});
});
services.AddActors(options =>
{
options.UseJsonSerialization = JsonSerializationEnabled;

View File

@ -58,6 +58,7 @@ namespace Dapr.E2E.Test
"--components-path", componentsPath,
"--config", configPath,
"--log-level", "debug",
"--dapr-http-max-request-size", "32",
};