StellaOps.Messaging.RateLimiting.Postgres
PostgreSQL rate limiter backend for StellaOps.Messaging.RateLimiting Install Installing Stella Ops packagesRouter downloads and signaturesnet10.01.0.1-20261117T070237Zdotnet add package StellaOps.
Namespaces
| Namespace | Types |
|---|---|
StellaOps. | 3 |
namespace StellaOps.Messaging.RateLimiting.Postgres
| Type | Kind | Description |
|---|---|---|
PostgresRateLimiter | class | PostgreSQL implementation of IRateLimiter. |
PostgresRateLimiterFactory | class | Factory for creating PostgreSQL rate limiter instances. |
PostgresRateLimiterServiceCollectionExtensions | class | Registers the PostgreSQL rate limiter. |
class PostgresRateLimiter
public sealed class PostgresRateLimiter : IRateLimiter
PostgreSQL implementation of IRateLimiter. Fixed window aligned to the Unix epoch (RateLimiterContract). Each key has one row per policy; one INSERT . statement checks and increments it atomically, and a denied attempt updates nothing. The table rate_limit_<name> is created at run time; the limiter name is validated by RateLimiterContract.before it reaches SQL.
Members
| Member | Kind | Summary |
|---|---|---|
PostgresRateLimiter( | constructor | Creates a named limiter whose counters are kept in PostgreSQL. |
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 PostgresRateLimiter(PostgresConnectionFactory, string, ILogger<PostgresRateLimiter>, TimeProvider) #
PostgresRateLimiter(PostgresConnectionFactory, string, ILogger<PostgresRateLimiter>, TimeProvider) #public PostgresRateLimiter(PostgresConnectionFactory connectionFactory, string name, ILogger<PostgresRateLimiter>? logger = null, TimeProvider? timeProvider = null)
Creates a named limiter whose counters are kept in PostgreSQL.
Parameters
connectionFactory: Opens the PostgreSQL connections.name: The limiter name.logger: Receives the diagnostics; none when not set.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 PostgresRateLimiterFactory
public sealed class PostgresRateLimiterFactory : IRateLimiterFactory
Factory for creating PostgreSQL rate limiter instances.
Members
| Member | Kind | Summary |
|---|---|---|
PostgresRateLimiterFactory( | constructor | Creates the factory of PostgreSQL limiters. |
ProviderName | property | Gets the provider name for this factory. |
Create( | method | Creates a rate limiter with the specified name. |
constructor PostgresRateLimiterFactory(PostgresConnectionFactory, ILoggerFactory, TimeProvider) #
PostgresRateLimiterFactory(PostgresConnectionFactory, ILoggerFactory, TimeProvider) #public PostgresRateLimiterFactory(PostgresConnectionFactory connectionFactory, ILoggerFactory? loggerFactory = null, TimeProvider? timeProvider = null)
Creates the factory of PostgreSQL limiters.
Parameters
connectionFactory: Opens the PostgreSQL connections.loggerFactory: Creates the limiter loggers; none when not set.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 PostgresRateLimiterServiceCollectionExtensions
public static class PostgresRateLimiterServiceCollectionExtensions
Registers the PostgreSQL rate limiter.
Members
| Member | Kind | Summary |
|---|---|---|
AddPostgresRateLimiter( | extension method | Registers PostgresRateLimiterFactoryas the IRateLimiterFactory. |
extension method AddPostgresRateLimiter(IServiceCollection) #
AddPostgresRateLimiter(IServiceCollection) #public static IServiceCollection AddPostgresRateLimiter(this IServiceCollection services)
Registers PostgresRateLimiterFactoryas the IRateLimiterFactory. The limiter uses the Postgres (connection and schema) that the PostgreSQL Messaging transport registers, so register that transport too.
Parameters
services: The service collection.
Returns
The service collection.