StellaOps.Messaging.RateLimiting.InMemory
In-process rate limiter backend for StellaOps.Messaging.RateLimiting (for testing and development) Install Installing Stella Ops packagesRouter downloads and signaturesnet10.01.0.1-20261117T070237Zdotnet add package StellaOps.
Namespaces
| Namespace | Types |
|---|---|
StellaOps. | 5 |
namespace StellaOps.Messaging.RateLimiting.InMemory
| Type | Kind | Description |
|---|---|---|
InMemoryRateLimitRegistry | class | The in-process rate limit state: one RateLimitBucketper limiter key, holding one window per policy. |
InMemoryRateLimiter | class | In-memory implementation of IRateLimiter. |
InMemoryRateLimiterFactory | class | Factory for creating in-memory rate limiter instances. |
InMemoryRateLimiterServiceCollectionExtensions | class | Registers the in-process rate limiter. |
RateLimitBucket | class | Fixed-window state of one in-memory rate limit key: one window per policy, identified by its RateLimitPolicy.and window length (the algorithm is defined by RateLimiterContract). |
class InMemoryRateLimitRegistry
public sealed class InMemoryRateLimitRegistry
The in-process rate limit state: one RateLimitBucketper limiter key, holding one window per policy. One registry is shared by every limiter its InMemoryRateLimiterFactorycreates, so limiters with the same name share their counts. Counts are not shared between processes.
Members
| Member | Kind | Summary |
|---|---|---|
InMemoryRateLimitRegistry( | constructor | Creates an instance of In. |
GetOrCreateRateLimitBucket( | method | Gets or creates a rate limit bucket for the specified key. |
RemoveRateLimitBucket( | method | Removes a rate limit bucket. |
TryGetRateLimitBucket( | method | Gets the rate limit bucket for the specified key without creating one. |
constructor InMemoryRateLimitRegistry() #
InMemoryRateLimitRegistry() #public InMemoryRateLimitRegistry()
Creates an instance of In.
method GetOrCreateRateLimitBucket(string) #
GetOrCreateRateLimitBucket(string) #public RateLimitBucket GetOrCreateRateLimitBucket(string key)
Gets or creates a rate limit bucket for the specified key.
method RemoveRateLimitBucket(string) #
RemoveRateLimitBucket(string) #public RateLimitBucket? RemoveRateLimitBucket(string key)
Removes a rate limit bucket.
Returns
The removed bucket, or null when the key had none.
method TryGetRateLimitBucket(string) #
TryGetRateLimitBucket(string) #public RateLimitBucket? TryGetRateLimitBucket(string key)
Gets the rate limit bucket for the specified key without creating one.
class InMemoryRateLimiter
public sealed class InMemoryRateLimiter : IRateLimiter
In-memory implementation of IRateLimiter. Fixed window aligned to the Unix epoch (RateLimiterContract), one window per key and policy; each key’s check and increment run under that key’s bucket lock, and a denied attempt does not consume a permit.
Members
| Member | Kind | Summary |
|---|---|---|
InMemoryRateLimiter( | constructor | Creates a named limiter over the process-local counters of a registry. |
ProviderName | property | Gets the provider name for diagnostics (e.g., “valkey”, “postgres”, “inmemory”). |
GetStatusAsync( | method | Gets current usage for a key under policy without consuming a permit. |
ResetAsync( | method | Resets the rate limit counters for a key under every policy. |
TryAcquireAsync( | method | Attempts to acquire a permit from the rate limiter under policy. |
constructor InMemoryRateLimiter(InMemoryRateLimitRegistry, string, TimeProvider) #
InMemoryRateLimiter(InMemoryRateLimitRegistry, string, TimeProvider) #public InMemoryRateLimiter(InMemoryRateLimitRegistry registry, string name, TimeProvider? timeProvider = null)
Creates a named limiter over the process-local counters of a registry.
Parameters
registry: Holds the counters.name: The limiter name.timeProvider: The clock; the system clock when not set.
Exceptions
ArgumentException:namebreaksRateLimiterContract..ValidateName( string)
property ProviderName #
ProviderName #public string ProviderName { get; }
Gets the provider name for diagnostics (e.g., “valkey”, “postgres”, “inmemory”).
method GetStatusAsync(string, RateLimitPolicy, CancellationToken) #
GetStatusAsync(string, RateLimitPolicy, CancellationToken) #public ValueTask<RateLimitStatus> GetStatusAsync(string key, RateLimitPolicy policy, CancellationToken cancellationToken = default)
Gets current usage for a key under policy without consuming a permit.
Parameters
key: The rate limit key.policy: The rate limit policy.cancellationToken: Cancellation token.
Returns
The current rate limit status.
method ResetAsync(string, CancellationToken) #
ResetAsync(string, CancellationToken) #public ValueTask<bool> ResetAsync(string key, CancellationToken cancellationToken = default)
Resets the rate limit counters for a key under every policy.
Parameters
key: The rate limit key.cancellationToken: Cancellation token.
Returns
True if the key had permits counted, under any policy, in a window that had not ended, and was reset.
method TryAcquireAsync(string, RateLimitPolicy, CancellationToken) #
TryAcquireAsync(string, RateLimitPolicy, CancellationToken) #public ValueTask<RateLimitResult> TryAcquireAsync(string key, RateLimitPolicy policy, CancellationToken cancellationToken = default)
Attempts to acquire a permit from the rate limiter under policy. When the key’s current window for that policy is full the attempt is denied, nothing is counted, and RateLimitResult.is the time until that window ends. The key’s windows for other policies are not changed: a caller that applies two policies calls once per policy.
Parameters
key: The rate limit key (e.g., user ID, IP address, resource identifier): 1 toRateLimiterContract.characters, compared case-sensitively.MaxKeyLength policy: The rate limit policy defining max permits and window.cancellationToken: Cancellation token.
Returns
The result of the rate limit check.
class InMemoryRateLimiterFactory
public sealed class InMemoryRateLimiterFactory : IRateLimiterFactory
Factory for creating in-memory rate limiter instances.
Members
| Member | Kind | Summary |
|---|---|---|
InMemoryRateLimiterFactory( | constructor | Creates the factory of in-memory limiters. |
ProviderName | property | Gets the provider name for this factory. |
Create( | method | Creates a rate limiter with the specified name. |
constructor InMemoryRateLimiterFactory(InMemoryRateLimitRegistry, TimeProvider) #
InMemoryRateLimiterFactory(InMemoryRateLimitRegistry, TimeProvider) #public InMemoryRateLimiterFactory(InMemoryRateLimitRegistry registry, TimeProvider? timeProvider = null)
Creates the factory of in-memory limiters.
Parameters
registry: Holds the counters of every limiter.timeProvider: The clock; the system clock when not set.
property ProviderName #
ProviderName #public string ProviderName { get; }
Gets the provider name for this factory.
method Create(string) #
Create(string) #public IRateLimiter Create(string name)
Creates a rate limiter with the specified name.
Parameters
name: The rate limiter name (used as key prefix, and in PostgreSQL as part of the table name): 1 toRateLimiterContract.characters fromMaxNameLength a-z,0-9and_, starting with a letter. Checked byRateLimiterContract..ValidateName( string)
Returns
A configured rate limiter instance.
Exceptions
ArgumentException: The name breaks the rule.
class InMemoryRateLimiterServiceCollectionExtensions
public static class InMemoryRateLimiterServiceCollectionExtensions
Registers the in-process rate limiter.
Members
| Member | Kind | Summary |
|---|---|---|
AddInMemoryRateLimiter( | extension method | Registers InMemoryRateLimiterFactoryas the IRateLimiterFactory, with one InMemoryRateLimitRegistryfor the process. |
extension method AddInMemoryRateLimiter(IServiceCollection) #
AddInMemoryRateLimiter(IServiceCollection) #public static IServiceCollection AddInMemoryRateLimiter(this IServiceCollection services)
Registers InMemoryRateLimiterFactoryas the IRateLimiterFactory, with one InMemoryRateLimitRegistryfor the process. Needs no Messaging transport.
Parameters
services: The service collection.
Returns
The service collection.
class RateLimitBucket
public sealed class RateLimitBucket
Fixed-window state of one in-memory rate limit key: one window per policy, identified by its RateLimitPolicy.and window length (the algorithm is defined by RateLimiterContract). Every operation holds the bucket lock, so check and increment are one atomic step.
Members
| Member | Kind | Summary |
|---|---|---|
RateLimitBucket( | constructor | Creates an instance of RateLimitBucket. |
HasLiveWindow( | method | Whether the bucket counted permits, under any policy, in a window that has not ended at nowMs. |
Read( | method | Reads the key’s effective window and count for the policy (maxPermits, current length) without counting a permit. |
TryAcquire( | method | Counts one permit in the key’s effective window for the policy (maxPermits, current length) unless that window already holds maxPermits permits. |
constructor RateLimitBucket() #
RateLimitBucket() #public RateLimitBucket()
Creates an instance of RateLimitBucket.
method HasLiveWindow(long) #
HasLiveWindow(long) #public bool HasLiveWindow(long nowMs)
Whether the bucket counted permits, under any policy, in a window that has not ended at nowMs.
method Read(RateLimitWindow, int) #
Read(RateLimitWindow, int) #public RateLimitWindowState Read(RateLimitWindow current, int maxPermits)
Reads the key’s effective window and count for the policy (maxPermits, current length) without counting a permit.
method TryAcquire(RateLimitWindow, int) #
TryAcquire(RateLimitWindow, int) #public (bool Allowed, RateLimitWindowState State) TryAcquire(RateLimitWindow current, int maxPermits)
Counts one permit in the key’s effective window for the policy (maxPermits, current length) unless that window already holds maxPermits permits. A denied attempt changes nothing; the windows of other policies are not read or changed.
Parameters
current: The caller’s window.maxPermits: The policy’s permit limit.
Returns
Whether the permit was counted, and the effective window with its count afterwards.