StellaOps.Messaging.RateLimiting.Postgres

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

Namespaces

NamespaceTypes
StellaOps.Messaging.RateLimiting.Postgres3

namespace StellaOps.Messaging.RateLimiting.Postgres

TypeKindDescription
PostgresRateLimiterclassPostgreSQL implementation of IRateLimiter.
PostgresRateLimiterFactoryclassFactory for creating PostgreSQL rate limiter instances.
PostgresRateLimiterServiceCollectionExtensionsclassRegisters 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 ... ON CONFLICT DO UPDATE ... WHERE 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.ValidateName(string)before it reaches SQL.

Members

MemberKindSummary
PostgresRateLimiter(PostgresConnectionFactory, string, ILogger<PostgresRateLimiter>, TimeProvider)constructorCreates a named limiter whose counters are kept in PostgreSQL.
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 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

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 PostgresRateLimiterFactory

public sealed class PostgresRateLimiterFactory : IRateLimiterFactory

Factory for creating PostgreSQL rate limiter instances.

Members

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

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

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 PostgresRateLimiterServiceCollectionExtensions

public static class PostgresRateLimiterServiceCollectionExtensions

Registers the PostgreSQL rate limiter.

Members

MemberKindSummary
AddPostgresRateLimiter(IServiceCollection)extension methodRegisters PostgresRateLimiterFactoryas the IRateLimiterFactory.

extension method AddPostgresRateLimiter(IServiceCollection) #

public static IServiceCollection AddPostgresRateLimiter(this IServiceCollection services)

Registers PostgresRateLimiterFactoryas the IRateLimiterFactory. The limiter uses the PostgresConnectionFactory (connection and schema) that the PostgreSQL Messaging transport registers, so register that transport too.

Parameters

  • services: The service collection.

Returns

The service collection.