StellaOps.Messaging.RateLimiting.InMemory

In-process rate limiter backend for StellaOps.Messaging.RateLimiting (for testing and development)

library
Component
Router
Licence
BUSL-1.1 (Router licence)
Target framework
net10.0
Latest release
1.0.1-20261117T070237Z

Install

dotnet add package StellaOps.Messaging.RateLimiting.InMemory --version 1.0.1-20261117T070237Z

Namespaces

NamespaceTypes
StellaOps.Messaging.RateLimiting.InMemory5

namespace StellaOps.Messaging.RateLimiting.InMemory

TypeKindDescription
InMemoryRateLimitRegistryclassThe in-process rate limit state: one RateLimitBucketper limiter key, holding one window per policy.
InMemoryRateLimiterclassIn-memory implementation of IRateLimiter.
InMemoryRateLimiterFactoryclassFactory for creating in-memory rate limiter instances.
InMemoryRateLimiterServiceCollectionExtensionsclassRegisters the in-process rate limiter.
RateLimitBucketclassFixed-window state of one in-memory rate limit key: one window per policy, identified by its RateLimitPolicy.MaxPermitsand 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

MemberKindSummary
InMemoryRateLimitRegistry()constructorCreates an instance of InMemoryRateLimitRegistry.
GetOrCreateRateLimitBucket(string)methodGets or creates a rate limit bucket for the specified key.
RemoveRateLimitBucket(string)methodRemoves a rate limit bucket.
TryGetRateLimitBucket(string)methodGets the rate limit bucket for the specified key without creating one.

constructor InMemoryRateLimitRegistry() #

public InMemoryRateLimitRegistry()

Creates an instance of InMemoryRateLimitRegistry.

method GetOrCreateRateLimitBucket(string) #

public RateLimitBucket GetOrCreateRateLimitBucket(string key)

Gets or creates a rate limit bucket for the specified key.

method 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) #

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

MemberKindSummary
InMemoryRateLimiter(InMemoryRateLimitRegistry, string, TimeProvider)constructorCreates a named limiter over the process-local counters of a registry.
ProviderNamepropertyGets the provider name for diagnostics (e.g., “valkey”, “postgres”, “inmemory”).
GetStatusAsync(string, RateLimitPolicy, CancellationToken)methodGets current usage for a key under policy without consuming a permit.
ResetAsync(string, CancellationToken)methodResets the rate limit counters for a key under every policy.
TryAcquireAsync(string, RateLimitPolicy, CancellationToken)methodAttempts to acquire a permit from the rate limiter under policy.

constructor 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

property ProviderName #

public string ProviderName { get; }

Gets the provider name for diagnostics (e.g., “valkey”, “postgres”, “inmemory”).

method 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) #

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) #

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.RetryAfteris 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 to RateLimiterContract.MaxKeyLengthcharacters, compared case-sensitively.
  • 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

MemberKindSummary
InMemoryRateLimiterFactory(InMemoryRateLimitRegistry, TimeProvider)constructorCreates the factory of in-memory limiters.
ProviderNamepropertyGets the provider name for this factory.
Create(string)methodCreates a rate limiter with the specified name.

constructor 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 #

public string ProviderName { get; }

Gets the provider name for this factory.

method Create(string) #

public IRateLimiter Create(string name)

Creates a rate limiter with the specified name.

Parameters

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

MemberKindSummary
AddInMemoryRateLimiter(IServiceCollection)extension methodRegisters InMemoryRateLimiterFactoryas the IRateLimiterFactory, with one InMemoryRateLimitRegistryfor the process.

extension method 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.MaxPermitsand window length (the algorithm is defined by RateLimiterContract). Every operation holds the bucket lock, so check and increment are one atomic step.

Members

MemberKindSummary
RateLimitBucket()constructorCreates an instance of RateLimitBucket.
HasLiveWindow(long)methodWhether the bucket counted permits, under any policy, in a window that has not ended at nowMs.
Read(RateLimitWindow, int)methodReads the key’s effective window and count for the policy (maxPermits, current length) without counting a permit.
TryAcquire(RateLimitWindow, int)methodCounts one permit in the key’s effective window for the policy (maxPermits, current length) unless that window already holds maxPermits permits.

constructor RateLimitBucket() #

public RateLimitBucket()

Creates an instance of RateLimitBucket.

method 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) #

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) #

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.