From 74cce13847e48c391c9a0f6ac2a8bfe16b911381 Mon Sep 17 00:00:00 2001 From: Ty Conner Date: Wed, 18 Dec 2024 12:17:00 -0500 Subject: [PATCH] Cache ServerVersion.AutoDetect (#4256) * Cache ServerVersion.AutoDetect Adjusted database connections so that they cache ServerVersion.AutoDetect result on first hit and reuse. This resolves a current bug that exhausts or times out connections to database. * Change to dictionary * Update Source/ACE.Database/DatabaseManager.cs Co-authored-by: gmriggs * Update DatabaseManager.cs * Update DatabaseManager.cs --------- Co-authored-by: gmriggs --- Source/ACE.Database/DatabaseManager.cs | 15 +++++++++++++++ Source/ACE.Database/Models/Auth/AuthDbContext.cs | 2 +- .../ACE.Database/Models/Shard/ShardDbContext.cs | 2 +- .../ACE.Database/Models/World/WorldDbContext.cs | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Source/ACE.Database/DatabaseManager.cs b/Source/ACE.Database/DatabaseManager.cs index 0c1288140b..50cee2c1bd 100644 --- a/Source/ACE.Database/DatabaseManager.cs +++ b/Source/ACE.Database/DatabaseManager.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Concurrent; + +using Microsoft.EntityFrameworkCore; using log4net; @@ -69,5 +72,17 @@ public static void Stop() if (serializedShardDb != null) serializedShardDb.Stop(); } + + private static readonly ConcurrentDictionary cachedServerVersions = new(); + + public static ServerVersion CachedServerVersionAutoDetect(string database, string connectionString) + { + if (!cachedServerVersions.TryGetValue(database, out ServerVersion serverVersion)) + { + serverVersion = ServerVersion.AutoDetect(connectionString); + cachedServerVersions[database] = serverVersion; + } + return serverVersion; + } } } diff --git a/Source/ACE.Database/Models/Auth/AuthDbContext.cs b/Source/ACE.Database/Models/Auth/AuthDbContext.cs index 134fe9aeb7..560415adf9 100644 --- a/Source/ACE.Database/Models/Auth/AuthDbContext.cs +++ b/Source/ACE.Database/Models/Auth/AuthDbContext.cs @@ -28,7 +28,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}"; - optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), builder => + optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder => { builder.EnableRetryOnFailure(10); }); diff --git a/Source/ACE.Database/Models/Shard/ShardDbContext.cs b/Source/ACE.Database/Models/Shard/ShardDbContext.cs index bceb8b1374..58b3919283 100644 --- a/Source/ACE.Database/Models/Shard/ShardDbContext.cs +++ b/Source/ACE.Database/Models/Shard/ShardDbContext.cs @@ -104,7 +104,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}"; - optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), builder => + optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder => { builder.EnableRetryOnFailure(10); }); diff --git a/Source/ACE.Database/Models/World/WorldDbContext.cs b/Source/ACE.Database/Models/World/WorldDbContext.cs index 38a4f1ff48..3d112ca388 100644 --- a/Source/ACE.Database/Models/World/WorldDbContext.cs +++ b/Source/ACE.Database/Models/World/WorldDbContext.cs @@ -132,7 +132,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}"; - optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), builder => + optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder => { builder.EnableRetryOnFailure(10); });