Retry database connections

This commit is contained in:
TidusJar 2024-06-17 20:56:53 +01:00
commit 7dd0f05e6f

View file

@ -3,11 +3,14 @@ using System.IO;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks;
using MySqlConnector;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Store.Context; using Ombi.Store.Context;
using Ombi.Store.Context.MySql; using Ombi.Store.Context.MySql;
using Ombi.Store.Context.Sqlite; using Ombi.Store.Context.Sqlite;
using Polly;
using Pomelo.EntityFrameworkCore.MySql.Storage.Internal;
using SQLitePCL; using SQLitePCL;
namespace Ombi.Extensions namespace Ombi.Extensions
@ -120,13 +123,40 @@ namespace Ombi.Extensions
public static void ConfigureMySql(DbContextOptionsBuilder options, PerDatabaseConfiguration config) public static void ConfigureMySql(DbContextOptionsBuilder options, PerDatabaseConfiguration config)
{ {
options.UseMySql(config.ConnectionString, ServerVersion.AutoDetect(config.ConnectionString), b => if (string.IsNullOrEmpty(config.ConnectionString))
{
throw new ArgumentNullException("ConnectionString for the MySql/Mariadb database is empty");
}
options.UseMySql(config.ConnectionString, GetServerVersion(config.ConnectionString), b =>
{ {
//b.CharSetBehavior(Pomelo.EntityFrameworkCore.MySql.Infrastructure.CharSetBehavior.NeverAppend); // ##ISSUE, link to migrations? //b.CharSetBehavior(Pomelo.EntityFrameworkCore.MySql.Infrastructure.CharSetBehavior.NeverAppend); // ##ISSUE, link to migrations?
b.EnableRetryOnFailure(); b.EnableRetryOnFailure();
}); });
} }
private static ServerVersion GetServerVersion(string connectionString)
{
// Workaround Windows bug, that can lead to the following exception:
//
// MySqlConnector.MySqlException (0x80004005): SSL Authentication Error
// ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
// ---> System.ComponentModel.Win32Exception (0x8009030F): The message or signature supplied for verification has been altered
//
// See https://github.com/dotnet/runtime/issues/17005#issuecomment-305848835
//
// Also workaround for the fact, that ServerVersion.AutoDetect() does not use any retrying strategy.
ServerVersion serverVersion = null;
#pragma warning disable EF1001
var retryPolicy = Policy.Handle<Exception>(exception => MySqlTransientExceptionDetector.ShouldRetryOn(exception))
#pragma warning restore EF1001
.WaitAndRetry(3, (count, context) => TimeSpan.FromMilliseconds(count * 250));
serverVersion = retryPolicy.Execute(() => serverVersion = ServerVersion.AutoDetect(connectionString));
return serverVersion;
}
public class DatabaseConfiguration public class DatabaseConfiguration
{ {
public DatabaseConfiguration() public DatabaseConfiguration()