Class PartitionedShield<TKey, TResult>

Namespace
Kevlar
Assembly
Kevlar.dll

Selects and retains an independent result-aware Shield<TResult> for each partition key.

public sealed class PartitionedShield<TKey, TResult> : IDisposable, IAsyncDisposable where TKey : notnull

Type Parameters

TKey
TResult
Inheritance
PartitionedShield<TKey, TResult>
Implements
Inherited Members

Remarks

Retention is bounded by MaxPartitions. After the eviction callback and active executions complete, disposable strategies owned by an evicted shield are released. Shared strategy instances are disposed after their last partition owner. By default the provider owns every strategy returned by its factory; set OwnsStrategies to false when those instances are also used outside the provider. Do not reuse a shield after its partition is evicted. A later lookup creates a fresh partition with fresh strategy state. Partition keys are never added to metric tags or shield names automatically.

Constructors

PartitionedShield(Func<TKey, Shield<TResult>>, PartitionedShieldOptions<TKey, TResult>?, IEqualityComparer<TKey>?)

Creates a bounded result-aware partition provider.

public PartitionedShield(Func<TKey, Shield<TResult>> factory, PartitionedShieldOptions<TKey, TResult>? options = null, IEqualityComparer<TKey>? comparer = null)

Parameters

factory Func<TKey, Shield<TResult>>
options PartitionedShieldOptions<TKey, TResult>
comparer IEqualityComparer<TKey>

Properties

CapacityEvictionCount

Gets the total number evicted to enforce the capacity bound.

public long CapacityEvictionCount { get; }

Property Value

long

ClearedEvictionCount

Gets the total number removed explicitly through remove or clear operations.

public long ClearedEvictionCount { get; }

Property Value

long

Count

Gets the current number of retained partitions.

public int Count { get; }

Property Value

int

CreatedCount

Gets the total number of partitions created by this provider.

public long CreatedCount { get; }

Property Value

long

EvictionCount

Gets the total number of partitions removed for any reason.

public long EvictionCount { get; }

Property Value

long

ExpirationEvictionCount

Gets the total number evicted after their idle expiration elapsed.

public long ExpirationEvictionCount { get; }

Property Value

long

Methods

Clear()

Removes every retained partition and begins configured lifecycle cleanup.

public void Clear()

ClearAsync()

Asynchronously removes every retained partition.

public ValueTask ClearAsync()

Returns

ValueTask

CreateAsync(Func<TKey, ValueTask<Shield<TResult>>>, PartitionedShieldOptions<TKey, TResult>?, IEqualityComparer<TKey>?)

Creates a bounded result-aware partition provider with an asynchronous factory.

public static PartitionedShield<TKey, TResult> CreateAsync(Func<TKey, ValueTask<Shield<TResult>>> factory, PartitionedShieldOptions<TKey, TResult>? options = null, IEqualityComparer<TKey>? comparer = null)

Parameters

factory Func<TKey, ValueTask<Shield<TResult>>>
options PartitionedShieldOptions<TKey, TResult>
comparer IEqualityComparer<TKey>

Returns

PartitionedShield<TKey, TResult>

Dispose()

Disposes strategies owned by every retained partition.

public void Dispose()

DisposeAsync()

Asynchronously waits for in-flight factories, removals, and executions, then disposes strategies owned by every retained partition. Concurrent callers await the same disposal operation.

public ValueTask DisposeAsync()

Returns

ValueTask

GetShield(TKey)

Gets or creates the result-aware shield for key.

public Shield<TResult> GetShield(TKey key)

Parameters

key TKey

Returns

Shield<TResult>

GetShieldAsync(TKey)

Asynchronously gets or creates the result-aware shield for key.

public ValueTask<Shield<TResult>> GetShieldAsync(TKey key)

Parameters

key TKey

Returns

ValueTask<Shield<TResult>>

PruneExpired()

Removes partitions whose configured idle expiration has elapsed.

public int PruneExpired()

Returns

int

The number removed.

PruneExpiredAsync()

Asynchronously removes partitions whose idle expiration has elapsed.

public ValueTask<int> PruneExpiredAsync()

Returns

ValueTask<int>

TryGetShield(TKey, out Shield<TResult>?)

Gets a retained result-aware shield without creating one.

public bool TryGetShield(TKey key, out Shield<TResult>? shield)

Parameters

key TKey
shield Shield<TResult>

Returns

bool

TryRemove(TKey)

Removes a retained partition and begins its configured lifecycle cleanup.

public bool TryRemove(TKey key)

Parameters

key TKey

Returns

bool

TryRemoveAsync(TKey)

Asynchronously removes a retained partition.

public ValueTask<bool> TryRemoveAsync(TKey key)

Parameters

key TKey

Returns

ValueTask<bool>