Wiaoj.RateLimiting.Abstractions 0.0.1-alpha.99-preview

This is a prerelease version of Wiaoj.RateLimiting.Abstractions.
dotnet add package Wiaoj.RateLimiting.Abstractions --version 0.0.1-alpha.99-preview
                    
NuGet\Install-Package Wiaoj.RateLimiting.Abstractions -Version 0.0.1-alpha.99-preview
                    
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="Wiaoj.RateLimiting.Abstractions" Version="0.0.1-alpha.99-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wiaoj.RateLimiting.Abstractions" Version="0.0.1-alpha.99-preview" />
                    
Directory.Packages.props
<PackageReference Include="Wiaoj.RateLimiting.Abstractions" />
                    
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 Wiaoj.RateLimiting.Abstractions --version 0.0.1-alpha.99-preview
                    
#r "nuget: Wiaoj.RateLimiting.Abstractions, 0.0.1-alpha.99-preview"
                    
#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 Wiaoj.RateLimiting.Abstractions@0.0.1-alpha.99-preview
                    
#: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=Wiaoj.RateLimiting.Abstractions&version=0.0.1-alpha.99-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Wiaoj.RateLimiting.Abstractions&version=0.0.1-alpha.99-preview&prerelease
                    
Install as a Cake Tool

Wiaoj.RateLimiting.Abstractions

Core contracts, interfaces, and value primitives for the Wiaoj.RateLimiting library.

This package defines the public API surface, decision primitives, and builder contracts required to consume, implement, or extend rate limiting components without referencing concrete algorithm implementations, distributed storage engines, or ASP.NET Core transport layers.


Installation

dotnet add package Wiaoj.RateLimiting.Abstractions

Core Types and Contracts

1. RateLimitDecision (readonly record struct)

Represents the immutable result of a rate limit check:

  • IsAllowed (bool): Indicates whether the request is allowed to proceed.
  • RetryAfter (TimeSpan?): The mandatory backoff duration before retrying if the request was denied.
  • Remaining (long?): The remaining capacity or token units for the key.
Static Factory Methods:
  • RateLimitDecision.Allowed(): Permitted with unknown remaining quota.
  • RateLimitDecision.Allowed(long remaining): Permitted with explicit remaining units. Throws ArgumentOutOfRangeException if remaining < 0.
  • RateLimitDecision.Denied(TimeSpan retryAfter): Denied with retry duration, sets remaining to 0. Throws ArgumentOutOfRangeException if retryAfter < TimeSpan.Zero.
  • RateLimitDecision.Denied(TimeSpan retryAfter, long remaining): Denied with retry duration and remaining units. Throws ArgumentOutOfRangeException if retryAfter < TimeSpan.Zero or remaining < 0.

2. IRateLimitAlgorithm

The fundamental contract implemented by all rate limiting algorithms and decorators:

public interface IRateLimitAlgorithm {
    ValueTask<RateLimitDecision> TryAcquireAsync(
        string key, 
        int cost, 
        CancellationToken cancellationToken = default);
}

Convenience overloads (e.g. TryAcquireAsync(key) with default cost 1) are provided via RateLimitingExtensions.


3. IRateLimiter

The primary service contract used by applications, middleware, and services to evaluate rate limit rules against named or default policies:

public interface IRateLimiter {
    ValueTask<RateLimitDecision> TryAcquireAsync(
        string policyName, 
        string key, 
        int cost, 
        CancellationToken cancellationToken = default);

    ValueTask<RateLimitDecision> TryAcquireAsync(
        string key, 
        int cost, 
        CancellationToken cancellationToken = default);

    IRateLimitAlgorithm GetPolicy(string policyName);
}

4. IRateLimiter<TPolicy>

A strongly-typed, marker-based wrapper for Dependency Injection:

public interface IRateLimiter<TPolicy> where TPolicy : notnull {
    ValueTask<RateLimitDecision> TryAcquireAsync(
        string key, 
        int cost, 
        CancellationToken cancellationToken = default);
}

Calls are automatically routed to the policy matching typeof(TPolicy).Name.


5. IRateLimitPolicyBuilder

A minimal builder contract for configuring an individual rate limit policy within the dependency injection container:

public interface IRateLimitPolicyBuilder {
    IServiceCollection Services { get; }
    string PolicyName { get; }
    IRateLimitPolicyBuilder UseAlgorithm(Func<IServiceProvider, IRateLimitAlgorithm> factory);
    IRateLimitPolicyBuilder AddDecorator(Func<IServiceProvider, IRateLimitAlgorithm, IRateLimitAlgorithm> decorator);
}

Usage Examples

Consuming IRateLimiter with a Named Policy

using Wiaoj.RateLimiting;

public sealed class PaymentService(IRateLimiter rateLimiter) {

    public async Task<bool> ProcessPaymentAsync(string customerId, CancellationToken cancellationToken) {
        RateLimitDecision decision = await rateLimiter.TryAcquireAsync(
            "payments", 
            customerId, 
            cost: 1, 
            cancellationToken: cancellationToken);

        if (!decision.IsAllowed) {
            TimeSpan? waitDuration = decision.RetryAfter;
            return false;
        }

        return true;
    }
}

Consuming IRateLimiter<TPolicy> with a Strongly-Typed Tag

using Wiaoj.RateLimiting;

public sealed class LoginController(IRateLimiter<AuthRateLimitPolicy> rateLimiter) {

    public async Task<bool> AuthenticateAsync(string clientIp, CancellationToken cancellationToken) {
        RateLimitDecision decision = await rateLimiter.TryAcquireAsync(
            clientIp, 
            cost: 1, 
            cancellationToken: cancellationToken);

        return decision.IsAllowed;
    }
}

public sealed class AuthRateLimitPolicy;

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on Wiaoj.RateLimiting.Abstractions:

Package Downloads
Wiaoj.RateLimiting

Package Description

Wiaoj.RateLimiting.AspNetCore

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1-alpha.99-preview 72 9/6/2026
0.0.1-alpha.98-preview 60 9/6/2026
0.0.1-alpha.97-preview 60 9/6/2026
0.0.1-alpha.96-preview 60 9/6/2026
0.0.1-alpha.95-preview 76 9/6/2026
0.0.1-alpha.109-preview 0 9/11/2026
0.0.1-alpha.108-preview 54 9/8/2026
0.0.1-alpha.107-preview 64 9/8/2026
0.0.1-alpha.106-preview 51 9/8/2026
0.0.1-alpha.105-preview 46 9/8/2026
0.0.1-alpha.104-preview 72 9/7/2026
0.0.1-alpha.103-preview 57 9/7/2026
0.0.1-alpha.102-preview 67 9/6/2026
0.0.1-alpha.101-preview 65 9/6/2026
0.0.1-alpha.100-preview 62 9/6/2026