Skip to content

Commit e982b1c

Browse files
authored
Replace HashSet with ConcurrentDictionary in WorkflowLoggingService (#1556)
This should help with issue #1273 - intermittent exceptions from WorkflowLoggingService. There is no concurrent HashSet in .NET, so we use ConcurrentDictionary with a byte as the value to simulate a set. For this use-case we do not care if the Key already exists, so we can use TryAdd to add items without considering the return value. Signed-off-by: Tomas Ekeli <tomas@eke.li>
1 parent 435d9fa commit e982b1c

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/Dapr.Workflow/WorkflowLoggingService.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@
1313

1414
namespace Dapr.Workflow
1515
{
16-
using System.Collections.Generic;
1716
using System.Threading;
1817
using System.Threading.Tasks;
1918
using Microsoft.Extensions.Hosting;
2019
using Microsoft.Extensions.Logging;
21-
using Microsoft.Extensions.Configuration;
20+
using System.Collections.Concurrent;
2221

2322
/// <summary>
2423
/// Defines runtime options for workflows.
2524
/// </summary>
2625
internal sealed class WorkflowLoggingService : IHostedService
2726
{
2827
private readonly ILogger<WorkflowLoggingService> logger;
29-
private static readonly HashSet<string> registeredWorkflows = new();
30-
private static readonly HashSet<string> registeredActivities = new();
28+
private static readonly ConcurrentDictionary<string, byte> registeredWorkflows = new();
29+
private static readonly ConcurrentDictionary<string, byte> registeredActivities = new();
3130

3231
public WorkflowLoggingService(ILogger<WorkflowLoggingService> logger)
3332
{
@@ -38,13 +37,13 @@ public Task StartAsync(CancellationToken cancellationToken)
3837
this.logger.Log(LogLevel.Information, "WorkflowLoggingService started");
3938

4039
this.logger.Log(LogLevel.Information, "List of registered workflows");
41-
foreach (string item in registeredWorkflows)
40+
foreach (string item in registeredWorkflows.Keys)
4241
{
4342
this.logger.Log(LogLevel.Information, item);
4443
}
4544

4645
this.logger.Log(LogLevel.Information, "List of registered activities:");
47-
foreach (string item in registeredActivities)
46+
foreach (string item in registeredActivities.Keys)
4847
{
4948
this.logger.Log(LogLevel.Information, item);
5049
}
@@ -55,18 +54,18 @@ public Task StartAsync(CancellationToken cancellationToken)
5554
public Task StopAsync(CancellationToken cancellationToken)
5655
{
5756
this.logger.Log(LogLevel.Information, "WorkflowLoggingService stopped");
58-
57+
5958
return Task.CompletedTask;
6059
}
6160

6261
public static void LogWorkflowName(string workflowName)
6362
{
64-
registeredWorkflows.Add(workflowName);
63+
registeredWorkflows.TryAdd(workflowName, 0);
6564
}
6665

6766
public static void LogActivityName(string activityName)
6867
{
69-
registeredActivities.Add(activityName);
68+
registeredActivities.TryAdd(activityName, 0);
7069
}
7170

7271
}

0 commit comments

Comments
 (0)