StellaOps.Messaging.RateLimiting.Valkey

Valkey/Redis rate limiter backend for StellaOps.Messaging.RateLimiting

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.Valkey --version 1.0.1-20261117T070237Z

Namespaces

NamespaceTypes
StellaOps.Messaging.RateLimiting.Valkey3

namespace StellaOps.Messaging.RateLimiting.Valkey

TypeKindDescription
ValkeyRateLimiterclassValkey/Redis implementation of IRateLimiter.
ValkeyRateLimiterFactoryclassFactory for creating Valkey rate limiter instances.
ValkeyRateLimiterServiceCollectionExtensionsclassRegisters the Valkey rate limiter.

class ValkeyRateLimiter

public sealed class ValkeyRateLimiter : IRateLimiter

Valkey/Redis implementation of IRateLimiter. Fixed window aligned to the Unix epoch (RateLimiterContract). Each key is one hash (ratelimit:<name>:<key>) with three fields per policy, named after RateLimiterContract.PolicyIdentity(RateLimitPolicy): ws:<policy> window start, wl:<policy> window length and c:<policy> count (for example c:10/60000ms); one Lua script checks and increments one policy’s fields atomically, and a denied attempt writes nothing. Window arithmetic uses the caller’s clock; the key’s TTL, the longest one any policy needs, only bounds how long an ended window is kept.

Members

MemberKindSummary
ValkeyRateLimiter(ValkeyConnectionFactory, string, ILogger<ValkeyRateLimiter>, TimeProvider)constructorCreates a named limiter whose counters are kept in Valkey.
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 ValkeyRateLimiter(ValkeyConnectionFactory, string, ILogger<ValkeyRateLimiter>, TimeProvider) #

public ValkeyRateLimiter(ValkeyConnectionFactory connectionFactory, string name, ILogger<ValkeyRateLimiter>? logger = null, TimeProvider? timeProvider = null)

Creates a named limiter whose counters are kept in Valkey.

Parameters

  • connectionFactory: Opens the Valkey connections.
  • name: The limiter name.
  • logger: Receives the diagnostics; none when not set.
  • 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 ValkeyRateLimiterFactory

public sealed class ValkeyRateLimiterFactory : IRateLimiterFactory

Factory for creating Valkey rate limiter instances.

Members

MemberKindSummary
ValkeyRateLimiterFactory(ValkeyConnectionFactory, ILoggerFactory, TimeProvider)constructorCreates the factory of Valkey limiters.
ProviderNamepropertyGets the provider name for this factory.
Create(string)methodCreates a rate limiter with the specified name.

constructor ValkeyRateLimiterFactory(ValkeyConnectionFactory, ILoggerFactory, TimeProvider) #

public ValkeyRateLimiterFactory(ValkeyConnectionFactory connectionFactory, ILoggerFactory? loggerFactory = null, TimeProvider? timeProvider = null)

Creates the factory of Valkey limiters.

Parameters

  • connectionFactory: Opens the Valkey connections.
  • loggerFactory: Creates the limiter loggers; none when not set.
  • 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 ValkeyRateLimiterServiceCollectionExtensions

public static class ValkeyRateLimiterServiceCollectionExtensions

Registers the Valkey rate limiter.

Members

MemberKindSummary
AddValkeyRateLimiter(IServiceCollection)extension methodRegisters ValkeyRateLimiterFactoryas the IRateLimiterFactory.

extension method AddValkeyRateLimiter(IServiceCollection) #

public static IServiceCollection AddValkeyRateLimiter(this IServiceCollection services)

Registers ValkeyRateLimiterFactoryas the IRateLimiterFactory. The limiter uses the ValkeyConnectionFactory that the Valkey Messaging transport registers, so register that transport too.

Parameters

  • services: The service collection.

Returns

The service collection.