Class PartitionedShield<TKey>
- Namespace
- Kevlar
- Assembly
- Kevlar.dll
Selects and retains an independent untyped Shield for each partition key.
public sealed class PartitionedShield<TKey> : IDisposable, IAsyncDisposable where TKey : notnull
Type Parameters
TKey
- Inheritance
-
PartitionedShield<TKey>
- 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>, PartitionedShieldOptions<TKey>?, IEqualityComparer<TKey>?)
Creates a bounded partition provider.
public PartitionedShield(Func<TKey, Shield> factory, PartitionedShieldOptions<TKey>? options = null, IEqualityComparer<TKey>? comparer = null)
Parameters
factoryFunc<TKey, Shield>optionsPartitionedShieldOptions<TKey>comparerIEqualityComparer<TKey>
Properties
CapacityEvictionCount
Gets the total number evicted to enforce the capacity bound.
public long CapacityEvictionCount { get; }
Property Value
ClearedEvictionCount
Gets the total number removed explicitly through remove or clear operations.
public long ClearedEvictionCount { get; }
Property Value
Count
Gets the current number of retained partitions.
public int Count { get; }
Property Value
CreatedCount
Gets the total number of partitions created by this provider.
public long CreatedCount { get; }
Property Value
EvictionCount
Gets the total number of partitions removed for any reason.
public long EvictionCount { get; }
Property Value
ExpirationEvictionCount
Gets the total number evicted after their idle expiration elapsed.
public long ExpirationEvictionCount { get; }
Property Value
Methods
Clear()
Removes every retained partition and begins configured lifecycle cleanup.
public void Clear()
ClearAsync()
Asynchronously removes every retained partition.
public ValueTask ClearAsync()
Returns
CreateAsync(Func<TKey, ValueTask<Shield>>, PartitionedShieldOptions<TKey>?, IEqualityComparer<TKey>?)
Creates a bounded partition provider with an asynchronous factory.
public static PartitionedShield<TKey> CreateAsync(Func<TKey, ValueTask<Shield>> factory, PartitionedShieldOptions<TKey>? options = null, IEqualityComparer<TKey>? comparer = null)
Parameters
factoryFunc<TKey, ValueTask<Shield>>optionsPartitionedShieldOptions<TKey>comparerIEqualityComparer<TKey>
Returns
- PartitionedShield<TKey>
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
GetShield(TKey)
Gets or creates the shield for key.
public Shield GetShield(TKey key)
Parameters
keyTKey
Returns
GetShieldAsync(TKey)
Asynchronously gets or creates the shield for key.
public ValueTask<Shield> GetShieldAsync(TKey key)
Parameters
keyTKey
Returns
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
TryGetShield(TKey, out Shield?)
Gets a retained shield without creating one.
public bool TryGetShield(TKey key, out Shield? shield)
Parameters
keyTKeyshieldShield
Returns
TryRemove(TKey)
Removes a retained partition and begins its configured lifecycle cleanup.
public bool TryRemove(TKey key)
Parameters
keyTKey
Returns
TryRemoveAsync(TKey)
Asynchronously removes a retained partition.
public ValueTask<bool> TryRemoveAsync(TKey key)
Parameters
keyTKey