Skip to content

Commit

Permalink
use FrozenDictionary to store channels
Browse files Browse the repository at this point in the history
  • Loading branch information
suraciii committed Jul 20, 2024
1 parent 9c427e4 commit 9fa754d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="OpenTelemetry.Exporter.InMemory" Version="$(OtelVersion)" />
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="$(OtelVersion)" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="$(OtelVersion)" />
<PackageVersion Include="System.Collections.Immutable" Version="$(MExVersion)" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="[4.5.4,)" />
Expand Down
9 changes: 3 additions & 6 deletions src/DCA.Extensions.BackgroundTask/BackgroundTaskDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.ObjectModel;
using System.Collections.Frozen;
using System.Diagnostics;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -31,14 +31,11 @@ ValueTask DispatchAsync<TContext>(
}

public sealed class BackgroundTaskDispatcher(
ReadOnlyCollection<BackgroundTaskChannel> channels,
FrozenDictionary<string, BackgroundTaskChannel> channels,
ILogger<BackgroundTaskDispatcher> logger,
IServiceProvider serviceProvider)
: IBackgroundTaskDispatcher
{
private readonly ReadOnlyDictionary<string, BackgroundTaskChannel> _channels
= new(channels.ToDictionary(x => x.Key, x => x));

public IServiceProvider ServiceProvider { get; } = serviceProvider;

public ValueTask DispatchAsync<TContext>(
Expand All @@ -56,6 +53,6 @@ public ValueTask DispatchAsync<TContext>(
);

channel ??= Constants.DefaultChannelKey;
return _channels[channel].DispatchAsync(task, startNow);
return channels[channel].DispatchAsync(task, startNow);
}
}
16 changes: 8 additions & 8 deletions src/DCA.Extensions.BackgroundTask/BackgroundTaskHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using System.Collections.ObjectModel;
using System.Collections.Frozen;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace DCA.Extensions.BackgroundTask;

/// <summary>
/// A hosted service to manage task channels' lifetime
/// </summary>
/// <param name="logger"></param>
/// <summary>
/// A hosted service to manage task channels' lifetime
/// </summary>
/// <param name="logger"></param>
/// <param name="channels"></param>
public class BackgroundTaskHostedService(
ILogger<BackgroundTaskHostedService> logger,
ReadOnlyCollection<BackgroundTaskChannel> channels) : IHostedService
FrozenDictionary<string, BackgroundTaskChannel> channels) : IHostedService
{
private readonly ILogger _logger = logger;

public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting channels...");
await Task.WhenAll(channels.Select(x => x.StartAsync()));
await Task.WhenAll(channels.Values.Select(x => x.StartAsync()));
_logger.LogInformation("Started channels...");
}

public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping background channels...");
await Task.WhenAny(channels.Select(x => x.StopAsync()));
await Task.WhenAny(channels.Values.Select(x => x.StopAsync()));
_logger.LogInformation("Stopped background channels...");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.ObjectModel;
using System.Collections.Frozen;
using DCA.Extensions.BackgroundTask;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -34,8 +34,7 @@ public static IServiceCollection AddBackgroundTask(
var options = sp.GetRequiredService<IOptions<BackgroundTaskOptions>>().Value;
return options.Channels
.Select(opt => ActivatorUtilities.CreateInstance<BackgroundTaskChannel>(sp, opt))
.ToList()
.AsReadOnly();
.ToFrozenDictionary(x => x.Key, x => x);
});
services.AddSingleton<IBackgroundTaskDispatcher, BackgroundTaskDispatcher>();
services.AddHostedService<BackgroundTaskHostedService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageReadmeFile>readme.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<None Include="readme.md" Pack="true" PackagePath="\"/>
<None Include="readme.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
Expand All @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="System.Collections.Immutable" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
<PackageReference Include="System.Threading.Channels" />
<PackageReference Include="System.Threading.Tasks.Extensions" />
Expand Down
6 changes: 3 additions & 3 deletions test/DCA.Extensions.BackgroundTask.Test/ProcessTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.ObjectModel;
using System.Collections.Frozen;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -15,8 +15,8 @@ public async Task Should_RecordCheckpoints()
services.AddBackgroundTask();
var provider = services.BuildServiceProvider();

var channels = provider.GetRequiredService<ReadOnlyCollection<BackgroundTaskChannel>>();
var defaultChannel = channels.Single(ch => ch.Key == Constants.DefaultChannelKey);
var channels = provider.GetRequiredService<FrozenDictionary<string, BackgroundTaskChannel>>();
var defaultChannel = channels[Constants.DefaultChannelKey];
var host = (BackgroundTaskHostedService)provider.GetRequiredService<IHostedService>();
await host.StartAsync(default);

Expand Down

0 comments on commit 9fa754d

Please sign in to comment.