Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
fix: LagSpike, plugin not load fixed fully
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Feb 12, 2024
1 parent 09ed72d commit 51018ea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
6 changes: 1 addition & 5 deletions src/Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public void OnConfigParsed(PluginConfig config)
userId: databaseSettings.Username,
password: databaseSettings.Password,
port: databaseSettings.Port,
sslMode: databaseSettings.Sslmode,
usePooling: true,
minPoolSize: 2,
maxPoolSize: 2);
sslMode: databaseSettings.Sslmode);

//** ? Save Config */

Expand Down Expand Up @@ -89,7 +86,6 @@ public override void Load(bool hotReload)
//** ? Load Player Caches */

LoadAllPlayersCache();
Database.Instance.AdjustDatabasePooling();

GameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules;
}
Expand Down
1 change: 0 additions & 1 deletion src/Plugin/PluginBasics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public void Initialize_Events()
{
AddTimer(1.0f, () =>
{
Database.Instance.AdjustDatabasePooling();
GameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules;
});
});
Expand Down
37 changes: 12 additions & 25 deletions src/Plugin/PluginDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,12 @@ public sealed class Database

private Database() { }

public void Initialize(string server, string database, string userId, string password, int port = 3306, string sslMode = "None", bool usePooling = true, uint minPoolSize = 10, uint maxPoolSize = 50)
public void Initialize(string server, string database, string userId, string password, int port = 3306, string sslMode = "None")
{
connectionString = BuildConnectionString(server, database, userId, password, port, sslMode, usePooling, minPoolSize, maxPoolSize);
connectionString = BuildConnectionString(server, database, userId, password, port, sslMode);
}

public void AdjustDatabasePooling()
{
if (connectionString == null)
throw new InvalidOperationException("Database has not been initialized");

var builder = new MySqlConnectionStringBuilder(connectionString)
{
MinimumPoolSize = (uint)Math.Max(5, Server.MaxPlayers / 2.5),
MaximumPoolSize = (uint)Math.Max(10, Server.MaxPlayers + 1),
};

connectionString = builder.ConnectionString;
}

private static string BuildConnectionString(string server, string database, string userId, string password, int port, string sslMode, bool usePooling, uint minPoolSize, uint maxPoolSize)
private static string BuildConnectionString(string server, string database, string userId, string password, int port, string sslMode)
{
var builder = new MySqlConnectionStringBuilder
{
Expand All @@ -43,9 +29,6 @@ private static string BuildConnectionString(string server, string database, stri
Password = password,
Port = (uint)port,
SslMode = Enum.Parse<MySqlSslMode>(sslMode, true),
Pooling = usePooling,
MinimumPoolSize = 10,
MaximumPoolSize = 24,
};

return builder.ConnectionString;
Expand Down Expand Up @@ -79,12 +62,16 @@ public async Task ExecuteNonQueryAsync(string query, params MySqlParameter[] par

public async Task<MySqlDataReader> ExecuteReaderAsync(string query, params MySqlParameter[] parameters)
{
var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
using (var connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();

var command = new MySqlCommand(query, connection);
command.Parameters.AddRange(parameters);
return await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
using (var command = new MySqlCommand(query, connection))
{
command.Parameters.AddRange(parameters);
return await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
}
}
}

public async Task ExecuteWithTransactionAsync(Func<MySqlConnection, MySqlTransaction, Task> executeActions)
Expand Down

0 comments on commit 51018ea

Please sign in to comment.