Indrivo.Utilities.ConcurrentConditionalAccessChecker 1.0.0

dotnet add package Indrivo.Utilities.ConcurrentConditionalAccessChecker --version 1.0.0                
NuGet\Install-Package Indrivo.Utilities.ConcurrentConditionalAccessChecker -Version 1.0.0                
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="Indrivo.Utilities.ConcurrentConditionalAccessChecker" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Indrivo.Utilities.ConcurrentConditionalAccessChecker --version 1.0.0                
#r "nuget: Indrivo.Utilities.ConcurrentConditionalAccessChecker, 1.0.0"                
#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.
// Install Indrivo.Utilities.ConcurrentConditionalAccessChecker as a Cake Addin
#addin nuget:?package=Indrivo.Utilities.ConcurrentConditionalAccessChecker&version=1.0.0

// Install Indrivo.Utilities.ConcurrentConditionalAccessChecker as a Cake Tool
#tool nuget:?package=Indrivo.Utilities.ConcurrentConditionalAccessChecker&version=1.0.0                

Indrivo.Utilities.ConcurrentConditionalAccessChecker

Name

CCAC (Conditional Concurrent Access Checker) code library

Description

This code library should be used in case if

  1. There are multiple concurrent request to the same resource object from a single or multiple clients;
  2. In order to eliminate duplicated idempotent requests;
  3. Skip execution of a code block, if already exists a thread which executes the corresponding code block.

*The de veloper may use this code library in order to eliminate some POST requests, indicating the necessary condition (or other non idempotent ones, if it is necessary)...

This functionality is used in such projects as:

  • DDWC.

Usage

  1. Eliminate duplicated HTTP requests at the Controller's filters level. The events are considered duplicated if there is a matching condition between them:
//The filter applied abothe the endpoint:
[ConcurrencyAccessFilter(HttpMethod.Put, nameof(DossiersController), nameof(CompleteDossier))]
public async Task<IActionResult> CompleteDossier([FromBody] FinishCompleteDossierCommand command, CancellationToken cancellationToken)
{
    //...
}

//The implementation of the filter
//...
switch (endpoint)
{
    case "Put_DossiersController_CompleteDossier":
    {
        var request = JsonConvert.DeserializeObject<FinishCompleteDossierCommand>(requestContent);
        if (request != null)
        {
            var searchItem = new Wrapper<FinishCompleteDossierCommand>(request, DateTime.UtcNow.AddSeconds(15));
            bool SearchClause(FinishCompleteDossierCommand command) => command.DossierId == request.DossierId;

            var timeRemaining = await Utility.ConcurrentConditionalAccessChecker.VerifyThreadAsync(searchItem, SearchClause, LockRoom.FinishCompleteDossierLock);
            if (timeRemaining != null)
            {
                timeRemaining = timeRemaining < 5 ? 5 : timeRemaining;
                throw new ConditionalConcurrencyException(timeRemaining.Value, default);
            }
        }

        break;
    }
    //other endpoints here
    //...
}

  1. Conditionally check if there is already a thread applied on the code block and skip it, if necessary:
// Let suppose that the action has been occurred on this dossier's Id in a previous action
var dossierId = Guid.NewGuid();

bool SearchClause(FinishCompleteDossierCommand item) => item.DossierId == dossierId;

if (await Utility.ConcurrentConditionalAccessChecker.VerifyThreadAsync(new Wrapper<FinishCompleteDossierCommand>(_command), SearchClause, LockRoom.FinishCompleteDossierLock, cancellationToken) == null)
{
    //Thread safe zone
    await Task.Run(() => Debug.WriteLine("Do Some action here"), cancellationToken);
}
//...
  1. Do not forget to erase expired registrations from this utility (using cron jobs):
public class ConcurrencyJob : IAsyncJob
{
    public async Task ExecuteAsync()
    {
        await Utility.ConcurrentConditionalAccessChecker.DeleteExpiredAsync();
    }
}

*Use different objects from LockRoom, if you are dealing in code with distinct event types.

Tests

See examples from the ConcurrentConditionalAccessChecker.Tests code library.

Additional

For more examples see the ReferenceApplication, ConcurrentConditionalAccessChecker.CronJobs and ConcurrentConditionalAccessChecker.Usage code libraries from this repository.

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. 
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
1.0.0 1,055 6/27/2023