Skip to content

Commit

Permalink
Send a MySQL ping packet by default. Fixes Xabaril#2031
Browse files Browse the repository at this point in the history
Users can opt in to setting a command that will be executed on the server, but the default is now a more efficient ping.
  • Loading branch information
bgrainger committed Nov 17, 2023
1 parent 2824b09 commit e7917f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ namespace Microsoft.Extensions.DependencyInjection;
public static class MySqlHealthCheckBuilderExtensions
{
private const string NAME = "mysql";
internal const string HEALTH_QUERY = "SELECT 1;";

/// <summary>
/// Add a health check for MySQL databases.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="connectionString">The MySQL connection string to be used.</param>
/// <param name="healthQuery">The query to be executed.</param>
/// <param name="healthQuery">The optional query to be executed. If this is <c>null</c>, a MySQL "ping" packet will be sent to the server instead of a query.</param>
/// <param name="configure">An optional action to allow additional MySQL specific configuration.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'mysql' will be used for the name.</param>
/// <param name="failureStatus">
Expand All @@ -30,7 +29,7 @@ public static class MySqlHealthCheckBuilderExtensions
public static IHealthChecksBuilder AddMySql(
this IHealthChecksBuilder builder,
string connectionString,
string healthQuery = HEALTH_QUERY,
string? healthQuery = null,
Action<MySqlConnection>? configure = null,
string? name = default,
HealthStatus? failureStatus = default,
Expand All @@ -45,7 +44,7 @@ public static IHealthChecksBuilder AddMySql(
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="connectionStringFactory">A factory to build the MySQL connection string to use.</param>
/// <param name="healthQuery">The query to be executed.</param>
/// <param name="healthQuery">The optional query to be executed. If this is <c>null</c>, a MySQL "ping" packet will be sent to the server instead of a query.</param>
/// <param name="configure">An optional action to allow additional MySQL specific configuration.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'mysql' will be used for the name.</param>
/// <param name="failureStatus">
Expand All @@ -58,7 +57,7 @@ public static IHealthChecksBuilder AddMySql(
public static IHealthChecksBuilder AddMySql(
this IHealthChecksBuilder builder,
Func<IServiceProvider, string> connectionStringFactory,
string healthQuery = HEALTH_QUERY,
string? healthQuery = null,
Action<MySqlConnection>? configure = null,
string? name = default,
HealthStatus? failureStatus = default,
Expand Down
23 changes: 16 additions & 7 deletions src/HealthChecks.MySql/MySqlHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class MySqlHealthCheck : IHealthCheck
public MySqlHealthCheck(MySqlHealthCheckOptions options)
{
Guard.ThrowIfNull(options.ConnectionString, true);
Guard.ThrowIfNull(options.CommandText, true);
_options = options;
}

Expand All @@ -27,13 +26,23 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
_options.Configure?.Invoke(connection);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

using var command = connection.CreateCommand();
command.CommandText = _options.CommandText;
object? result = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
if (_options.CommandText is { } commandText)
{
using var command = connection.CreateCommand();
command.CommandText = _options.CommandText;
object? result = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);

return _options.HealthCheckResultBuilder == null
? HealthCheckResult.Healthy()
: _options.HealthCheckResultBuilder(result);
return _options.HealthCheckResultBuilder == null
? HealthCheckResult.Healthy()
: _options.HealthCheckResultBuilder(result);
}
else
{
var success = await connection.PingAsync(cancellationToken).ConfigureAwait(false);
return _options.HealthCheckResultBuilder is null
? (success ? HealthCheckResult.Healthy() : new HealthCheckResult(context.Registration.FailureStatus)) :
_options.HealthCheckResultBuilder(success);
}
}
catch (Exception ex)
{
Expand Down
3 changes: 1 addition & 2 deletions src/HealthChecks.MySql/MySqlHealthCheckOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using MySqlConnector;

Expand All @@ -17,7 +16,7 @@ public class MySqlHealthCheckOptions
/// <summary>
/// The query to be executed.
/// </summary>
public string CommandText { get; set; } = MySqlHealthCheckBuilderExtensions.HEALTH_QUERY;
public string? CommandText { get; set; }

/// <summary>
/// An optional action executed before the connection is opened in the health check.
Expand Down

0 comments on commit e7917f8

Please sign in to comment.