InMemoryQuotaStore
AgentPrism.Core.dllThe default implementation that keeps quota rules and counters in process memory.
public sealed class InMemoryQuotaStore : IQuotaStoreInheritance
Section titled “Inheritance”Implements
Section titled “Implements”Inherited Members
Section titled “Inherited Members”object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Remarks
Section titled “Remarks”For single-process deployments and tests. In a multi-instance deployment,
UsePostgreSql replaces this with PostgresQuotaStore. Otherwise,
each instance keeps its own counter and the quota is split between instances.
Sayac artirma ConcurrentDictionary.AddOrUpdate
is atomic. It provides the same contract as ON CONFLICT DO UPDATE in the
PostgreSQL implementation, so no increment is lost.
Constructors
Section titled “Constructors”InMemoryQuotaStore()
Section titled “ InMemoryQuotaStore()”public InMemoryQuotaStore()Methods
Section titled “Methods”AddUsageAsync(QuotaConsumption, IReadOnlyDictionary<QuotaPeriod, DateOnly>, CancellationToken)
Section titled “ AddUsageAsync(QuotaConsumption, IReadOnlyDictionary<QuotaPeriod, DateOnly>, CancellationToken)”Adds a consumption to both the agent counter and the tenant-wide counter.
public ValueTask AddUsageAsync(QuotaConsumption consumption, IReadOnlyDictionary<QuotaPeriod, DateOnly> periodStarts, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”consumption QuotaConsumption
The consumption.
periodStarts IReadOnlyDictionary<QuotaPeriod, DateOnly>
For each period, the first day of the period the consumption falls into. The caller computes this from the configured time zone; the store does not know time zones.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”The completion task.
Remarks
Section titled “Remarks”The store carries no time zone: the caller computes the period boundary and passes it ready-made. This lets the same store behave correctly for consumers running in different time zones.
DeleteAsync(string, Guid, CancellationToken)
Section titled “ DeleteAsync(string, Guid, CancellationToken)”Deletes a rule.
public ValueTask<bool> DeleteAsync(string tenantId, Guid id, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”tenantId string
The tenant identifier.
id Guid
The rule identifier.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”true if the delete happened.
GetAsync(string, Guid, CancellationToken)
Section titled “ GetAsync(string, Guid, CancellationToken)”Fetches a single rule.
public ValueTask<QuotaDefinition?> GetAsync(string tenantId, Guid id, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”tenantId string
The tenant identifier.
id Guid
The rule identifier.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”The rule; null if it does not exist or belongs to another tenant.
GetUsageAsync(QuotaUsageQuery, CancellationToken)
Section titled “ GetUsageAsync(QuotaUsageQuery, CancellationToken)”Fetches the current period’s counters.
public ValueTask<IReadOnlyList<QuotaUsageRecord>> GetUsageAsync(QuotaUsageQuery query, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”query QuotaUsageQuery
The filter.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”ValueTask<IReadOnlyList<QuotaUsageRecord>>
The counters. An empty list when there is no consumption at all.
ListAsync(string, CancellationToken)
Section titled “ ListAsync(string, CancellationToken)”Lists a tenant’s quota rules.
public ValueTask<IReadOnlyList<QuotaDefinition>> ListAsync(string tenantId, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”tenantId string
The tenant identifier.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”ValueTask<IReadOnlyList<QuotaDefinition>>
The rules.
SaveAsync(QuotaDefinition, CancellationToken)
Section titled “ SaveAsync(QuotaDefinition, CancellationToken)”Saves a rule. Overwrites an existing rule for the same scope (tenant + agent + period), if one already exists.
public ValueTask<QuotaDefinition> SaveAsync(QuotaDefinition definition, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”definition QuotaDefinition
The rule.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”The saved rule.
Remarks
Section titled “Remarks”Scope uniqueness is built over COALESCE(agent_name, ''); a plain
UNIQUE constraint treats NULLs as distinct from each other and
would let the same rule be added an unlimited number of times.