Skip to content

Commit

Permalink
Refactor DaprWorkflowClientBuilderFactory and WorkflowRuntimeOptions
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
humandigital-michiel committed Oct 23, 2024
1 parent ee8be67 commit a7fbd22
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Dapr.Workflow/DaprWorkflowClientBuilderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public DaprWorkflowClientBuilderFactory(IConfiguration configuration, IHttpClien
_httpClientFactory = httpClientFactory;
_services = services;
}

/// <summary>
/// Responsible for building the client itself.
/// </summary>
Expand All @@ -50,17 +50,25 @@ public void CreateClientBuilder(Action<WorkflowRuntimeOptions> configure)
{
_services.AddDurableTaskClient(builder =>
{
WorkflowRuntimeOptions options = new();
configure?.Invoke(options);
var apiToken = DaprDefaults.GetDefaultDaprApiToken(_configuration);
var grpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint(_configuration);
var httpClient = _httpClientFactory.CreateClient();
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();
});

Expand All @@ -81,8 +89,12 @@ public void CreateClientBuilder(Action<WorkflowRuntimeOptions> configure)
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
{
Expand Down
16 changes: 16 additions & 0 deletions src/Dapr.Workflow/WorkflowRuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// limitations under the License.
// ------------------------------------------------------------------------

using Grpc.Net.Client;

namespace Dapr.Workflow
{
using System;
Expand All @@ -29,6 +31,11 @@ public sealed class WorkflowRuntimeOptions
/// </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>
Expand Down Expand Up @@ -117,6 +124,15 @@ public void RegisterActivity<TActivity>() where TActivity : class, IWorkflowActi
WorkflowLoggingService.LogActivityName(name);
});
}

/// <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.
Expand Down
20 changes: 20 additions & 0 deletions test/Dapr.E2E.Test.App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Dapr.E2E.Test
using System;
using Microsoft.Extensions.Logging;
using Serilog;
using Grpc.Net.Client;

/// <summary>
/// Startup class.
Expand Down Expand Up @@ -98,6 +99,25 @@ public void ConfigureServices(IServiceCollection services)
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;
Expand Down
1 change: 1 addition & 0 deletions test/Dapr.E2E.Test/DaprTestApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public DaprTestApp(ITestOutputHelper output, string appId)
"--components-path", componentsPath,
"--config", configPath,
"--log-level", "debug",
"--dapr-http-max-request-size", "32",

};

Expand Down

0 comments on commit a7fbd22

Please sign in to comment.