Table of Contents

Class DatabaseHealthMonitor

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

Monitors database health by performing connectivity checks and measuring response times. Thread-safe implementation suitable for Unity headless servers. Follows Single Responsibility Principle: solely responsible for database health monitoring. Follows Dependency Inversion Principle: depends on INpgsqlDbContextFactory abstraction.

public sealed class DatabaseHealthMonitor
Inheritance
DatabaseHealthMonitor
Inherited Members

Constructors

DatabaseHealthMonitor(INpgsqlDbContextFactory, int, int, double, double)

Initializes a new instance of the DatabaseHealthMonitor class.

public DatabaseHealthMonitor(INpgsqlDbContextFactory dbContextFactory, int warningThresholdMs = 100, int criticalThresholdMs = 500, double poolWarningThreshold = 70, double poolCriticalThreshold = 85)

Parameters

dbContextFactory INpgsqlDbContextFactory

Factory for creating database contexts.

warningThresholdMs int

Response time warning threshold in milliseconds (default: 100ms).

criticalThresholdMs int

Response time critical threshold in milliseconds (default: 500ms).

poolWarningThreshold double

Pool utilization warning threshold percentage (default: 70%).

poolCriticalThreshold double

Pool utilization critical threshold percentage (default: 85%).

Exceptions

ArgumentNullException

Thrown when dbContextFactory is null.

Properties

LastCheckTime

Gets the time of the last health check. Thread-safe property access.

public DateTime LastCheckTime { get; }

Property Value

DateTime

LastMessage

Gets the last health check message. Thread-safe property access.

public string LastMessage { get; }

Property Value

string

LastStatus

Gets the last known health status. Thread-safe property access.

public HealthStatus LastStatus { get; }

Property Value

HealthStatus

Methods

CheckHealth()

Performs a synchronous health check on the database. Safe to call from Unity main thread.

public HealthCheckResult CheckHealth()

Returns

HealthCheckResult

Health check result containing status and diagnostic information.

Remarks

This method performs a truly synchronous database call (no sync-over-async) to avoid thread-pool starvation under load.

Security Warning: The returned HealthCheckResult contains sensitive infrastructure details including database names, server addresses, and connection pool information. If exposing this result via public APIs or external systems, call Sanitize() to redact sensitive information.

Example: var sanitizedResult = CheckHealth().Sanitize();

CheckHealthAsync(CancellationToken)

Performs an asynchronous health check on the database. Executes a simple SELECT 1 query to verify connectivity and measure response time.

public Task<HealthCheckResult> CheckHealthAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token to cancel the operation.

Returns

Task<HealthCheckResult>

Health check result containing status and diagnostic information.

Remarks

Security Warning: The returned HealthCheckResult contains sensitive infrastructure details including database names, server addresses, and connection pool information. If exposing this result via public APIs or external systems, call Sanitize() to redact sensitive information.

Example: var sanitizedResult = await CheckHealthAsync().Sanitize();

GetPoolHealth()

Gets the current pool health status without performing a full health check. Useful for lightweight monitoring and alerting.

public PoolHealthResult GetPoolHealth()

Returns

PoolHealthResult

The current pool health result.