Skip to content

InMemoryQuotaStore

Namespace AgentPrism · Assembly AgentPrism.Core.dll

The default implementation that keeps quota rules and counters in process memory.

public sealed class InMemoryQuotaStore : IQuotaStore

objectInMemoryQuotaStore

IQuotaStore

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

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.

public InMemoryQuotaStore()

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)

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.

ValueTask

The completion task.

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)

tenantId string

The tenant identifier.

id Guid

The rule identifier.

cancellationToken CancellationToken

The cancellation token.

ValueTask<bool>

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)

tenantId string

The tenant identifier.

id Guid

The rule identifier.

cancellationToken CancellationToken

The cancellation token.

ValueTask<QuotaDefinition?>

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)

query QuotaUsageQuery

The filter.

cancellationToken CancellationToken

The cancellation token.

ValueTask<IReadOnlyList<QuotaUsageRecord>>

The counters. An empty list when there is no consumption at all.

Lists a tenant’s quota rules.

public ValueTask<IReadOnlyList<QuotaDefinition>> ListAsync(string tenantId, CancellationToken cancellationToken = default)

tenantId string

The tenant identifier.

cancellationToken CancellationToken

The cancellation token.

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)

definition QuotaDefinition

The rule.

cancellationToken CancellationToken

The cancellation token.

ValueTask<QuotaDefinition>

The saved rule.

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.