TaskMuxer 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package TaskMuxer --version 1.0.4
                    
NuGet\Install-Package TaskMuxer -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TaskMuxer" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TaskMuxer" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="TaskMuxer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TaskMuxer --version 1.0.4
                    
#r "nuget: TaskMuxer, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TaskMuxer@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TaskMuxer&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=TaskMuxer&version=1.0.4
                    
Install as a Cake Tool

TaskMuxer

NuGet Version NuGet Downloads License: MIT

Task Multiplexer.

The purpose of this package is to provide the ability to reduce the amount of parallel repetitive work.

In practical terms, imagine a service that requests data from a slow external API serving multiple requests in parallel.

By using this library, for any number of parallel requests only one will be executed.

Tasks are being identified / segmented by a key and their respective return type.

Nuget

Usage

Registration

Locate the services registration and append one of:

  • .AddInstanceTaskMultiplexerNoLogger - use when no logging is required
  • .AddInstanceTaskMultiplexerWithILogger - use when the ILogger provider is available
  • .AddInstanceTaskMultiplexerWithILoggerFactory - use when the ILoggerFactory provider is available

ex.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // ...
    services
        .AddInstanceTaskMultiplexerWithILogger();
    // ...
}

📝 If further customization is required, consider wiring up any of the ITaskMultiplexer implementations as required.

Code

Please check the ITaskMultiplexer interface for the API surface provided.

ex.

public class ItemsCountService
{
    readonly ITaskMultiplexer _taskMultiplexer;

    public ItemsCountService(ITaskMultiplexer taskMultiplexer) => _taskMultiplexer = taskMultiplexer;

    public async Task<long> GetCountOfItems(CancellationToken cancellationToken = default) =>
        await _taskMultiplexer.AddTask(
            "items_count",
            async ct =>
            {
                await Task.Delay(250, ct);
                return Random.Shared.Next();
            },
            cancellationToken
        );
}

Thread Safety

To achieve thread safety in cases like entity framework core accessing and / or writing data at the same time through parallel threads consider using semaphores or some other form of locking.

ex.

public record SampleEntity
{
    public int Id { get; set; }

    public string Text { get; set; } = string.Empty;
}

public class SampleDataContext : DbContext
{
    public SampleDataContext(DbContextOptions<SampleDataContext> options) : base(options) { }

    public DbSet<SampleEntity> Samples { get; set; } = default!;
}

public class SampleService
{
    readonly ITaskMultiplexer _taskMultiplexer;
    readonly SampleDataContext _dbContext;

    private SemaphoreSlim _semaphoreSlim = new(1);

    public SampleService(ITaskMultiplexer taskMultiplexer, SampleDataContext dbContext) =>
        (_taskMultiplexer, _dbContext) = (taskMultiplexer, dbContext);

    public async Task<SampleEntity?> GetById(int id, CancellationToken cancellationToken = default) =>
        await _taskMultiplexer.AddTask(
            "get_by_id_with_semaphore",
            async ct =>
            {
                await _semaphoreSlim.WaitAsync(ct);

                try
                {
                    return await _dbContext.Samples.FirstOrDefaultAsync(x => x.Id == id, ct);
                }
                finally
                {
                    _semaphoreSlim.Release();
                }
            },
            cancellationToken
        );

    public async Task<SampleEntity?> Add(SampleEntity obj, CancellationToken cancellationToken = default) =>
        await _taskMultiplexer.AddTask(
            "add_with_semaphore",
            async ct =>
            {
                await _semaphoreSlim.WaitAsync(ct);

                try
                {
                    _dbContext.Add(obj);
                    await _dbContext.SaveChangesAsync(ct);
                }
                finally
                {
                    _semaphoreSlim.Release();
                }

                return obj;
            },
            cancellationToken
        );
}

var service = new SampleService(
    new InstanceTaskMultiplexer(),
    new SampleDataContext(
        new DbContextOptionsBuilder<SampleDataContext>()
            .UseInMemoryDatabase("sample_database")
            .Options
    )
);

await Task.WhenAll(
    service.GetById(1),
    service.Add(
        new()
        {
            Text = "Random " + Random.Shared.Next()
        }
    ),
    service.GetById(1)
);

Similar Projects

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.1 240 11/16/2024
2.1.0 120 11/16/2024
2.0.1 5,171 11/22/2023
2.0.0 129 11/21/2023
1.0.13 1,946 9/15/2023
1.0.12 184 9/14/2023
1.0.11 419 9/13/2023
1.0.10 241 9/11/2023
1.0.9 190 9/10/2023
1.0.8 152 9/4/2023
1.0.7 175 9/1/2023
1.0.6 176 9/1/2023
1.0.5 185 8/31/2023
1.0.4 179 8/30/2023
1.0.3 187 8/28/2023
1.0.2 188 8/28/2023
1.0.1 193 8/27/2023
1.0.0 177 8/26/2023