Table of Contents

Class ConnectionPoolMetrics

Namespace
FishMMO.Database.Npgsql.Monitoring.Metrics
Assembly
FishMMO-DB.dll

Tracks runtime connection pool metrics for monitoring and diagnostics. NOTE: Active connection counts are derived from DbConnection open/close events (via EF Core connection interceptors), not DbContext creation/disposal. Thread-safe implementation using atomic operations.

public sealed class ConnectionPoolMetrics
Inheritance
ConnectionPoolMetrics
Inherited Members

Properties

ActiveConnections

Gets the current number of active connections.

public long ActiveConnections { get; }

Property Value

long

ConnectionErrors

Gets the total number of connection errors encountered.

public long ConnectionErrors { get; }

Property Value

long

PeakActiveConnections

Gets the peak number of concurrent active connections.

public long PeakActiveConnections { get; }

Property Value

long

PoolExhaustionCount

Gets the number of times the pool was exhausted (all connections in use).

public long PoolExhaustionCount { get; }

Property Value

long

TotalConnectionsCreated

Gets the total number of connections created since startup.

public long TotalConnectionsCreated { get; }

Property Value

long

TotalConnectionsDisposed

Gets the total number of connections disposed since startup.

public long TotalConnectionsDisposed { get; }

Property Value

long

Methods

GetPoolHealth(int, double, double)

Assesses the health of the connection pool based on utilization and error metrics.

public PoolHealthResult GetPoolHealth(int maxPoolSize, double warningThreshold = 70, double criticalThreshold = 85)

Parameters

maxPoolSize int

The configured maximum pool size.

warningThreshold double

Utilization percentage threshold for warning status (default: 70%).

criticalThreshold double

Utilization percentage threshold for critical status (default: 85%).

Returns

PoolHealthResult

A PoolHealthResult containing the health assessment and recommendations.

Remarks

Health status determination logic:

  • Healthy: Utilization below warning threshold, no recent exhaustion events
  • Warning: Utilization between warning and critical thresholds
  • Critical: Utilization above critical threshold or frequent exhaustion events
  • Unhealthy: Active exhaustion events or high connection error rate

GetUtilizationPercentage(int)

Gets the approximate pool utilization percentage (0-100). Returns -1 if max pool size is unknown.

public double GetUtilizationPercentage(int maxPoolSize)

Parameters

maxPoolSize int

The configured maximum pool size.

Returns

double

Pool utilization percentage or -1 if cannot calculate.

RecordConnectionClosed()

Records a connection being closed (returned to the pool). Thread-safe operation.

public void RecordConnectionClosed()

RecordConnectionError()

Records a connection error. Thread-safe operation.

public void RecordConnectionError()

RecordConnectionOpened()

Records a connection being opened (checked out from the pool). Thread-safe operation.

public void RecordConnectionOpened()

RecordPoolExhaustion()

Records a pool exhaustion event. Thread-safe operation.

public void RecordPoolExhaustion()

Reset()

Resets all metrics to zero. Use with caution - typically only for testing or maintenance windows.

public void Reset()

ToString()

Returns a formatted string representation of the current metrics.

public override string ToString()

Returns

string

Formatted metrics summary.