Skip to content

Commit

Permalink
Added option to override GrpcChannelOptions when adding DaprWorkflow …
Browse files Browse the repository at this point in the history
…(#7218)

Signed-off-by: Ruud van Falier <ruud.vanfalier@humandigital.nl>
Signed-off-by: Michiel van Praat <michiel.vanpraat@humandigital.nl>
  • Loading branch information
humandigital-ruud authored and humandigital-michiel committed Aug 26, 2024
1 parent 74f6b01 commit 6f5033c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
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
52 changes: 25 additions & 27 deletions src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace Dapr.Workflow
/// </summary>
public static class WorkflowServiceCollectionExtensions
{

/// <summary>
/// Adds Dapr Workflow support to the service collection.
/// </summary>
Expand Down Expand Up @@ -61,18 +60,15 @@ public static IServiceCollection AddDaprWorkflow(
if (TryGetGrpcAddress(out string address))
{
var client = new HttpClient();
var daprApiToken = DaprDefaults.GetDefaultDaprApiToken();
if (!string.IsNullOrEmpty(daprApiToken))
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Dapr-Api-Token", daprApiToken);
builder.UseGrpc(CreateChannel(address, client));
}
else
{
builder.UseGrpc(address);
}
builder.UseGrpc(CreateChannel(address, client, options.GrpcChannelOptions));
}
else
{
Expand Down Expand Up @@ -111,7 +107,6 @@ public static IServiceCollection AddDaprWorkflowClient(this IServiceCollection s
{
builder.UseGrpc(address);
}
}
else
{
Expand All @@ -131,7 +126,8 @@ static bool TryGetGrpcAddress(out string address)
// 2. DaprDefaults.cs doesn't compile when the project has C# nullable reference types enabled.
// If the above issues are fixed (ensuring we don't regress anything) we should switch to using the logic in DaprDefaults.cs.
var daprEndpoint = DaprDefaults.GetDefaultGrpcEndpoint();
if (!String.IsNullOrEmpty(daprEndpoint)) {
if (!String.IsNullOrEmpty(daprEndpoint))
{
address = daprEndpoint;
return true;
}
Expand All @@ -153,31 +149,33 @@ static bool TryGetGrpcAddress(out string address)
return false;
}

static GrpcChannel CreateChannel(string address, HttpClient client)
static GrpcChannel CreateChannel(string address, HttpClient client, GrpcChannelOptions? grpcChannelOptions = null)
{

GrpcChannelOptions options = new() { HttpClient = client};
var daprEndpoint = DaprDefaults.GetDefaultGrpcEndpoint();
if (!String.IsNullOrEmpty(daprEndpoint)) {
return GrpcChannel.ForAddress(daprEndpoint, options);
}
GrpcChannelOptions options = grpcChannelOptions ?? new();
options.HttpClient ??= client;

var daprPortStr = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
if (int.TryParse(daprPortStr, out int daprGrpcPort))
{
// If there is no address passed in, we default to localhost
if (String.IsNullOrEmpty(address))
var daprEndpoint = DaprDefaults.GetDefaultGrpcEndpoint();
if (!String.IsNullOrEmpty(daprEndpoint))
{
// There is a bug in the Durable Task SDK that requires us to change the format of the address
// depending on the version of .NET that we're targeting. For now, we work around this manually.
#if NET6_0_OR_GREATER
return GrpcChannel.ForAddress(daprEndpoint, options);
}

var daprPortStr = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT");
if (int.TryParse(daprPortStr, out int daprGrpcPort))
{
// If there is no address passed in, we default to localhost
if (String.IsNullOrEmpty(address))
{
// There is a bug in the Durable Task SDK that requires us to change the format of the address
// depending on the version of .NET that we're targeting. For now, we work around this manually.
#if NET6_0_OR_GREATER
address = $"http://localhost:{daprGrpcPort}";
#else
#else
address = $"localhost:{daprGrpcPort}";
#endif
#endif
}
}

}
return GrpcChannel.ForAddress(address, options);
}
}
Expand Down

0 comments on commit 6f5033c

Please sign in to comment.