Fix database connection pool contention (maybe)
Instead of acquiring a connection per service per request, we acquire connections more often but at a more granular level, meaning they're also disposed of more quickly instead of staying for a long time in case of long-running commands or leaks.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -32,8 +33,8 @@ namespace PluralKit.Bot
|
||||
using (var services = BuildServiceProvider())
|
||||
{
|
||||
Console.WriteLine("- Connecting to database...");
|
||||
var connection = services.GetRequiredService<IDbConnection>() as NpgsqlConnection;
|
||||
await Schema.CreateTables(connection);
|
||||
using (var conn = services.GetRequiredService<DbConnectionFactory>().Obtain())
|
||||
await Schema.CreateTables(conn);
|
||||
|
||||
Console.WriteLine("- Connecting to Discord...");
|
||||
var client = services.GetRequiredService<IDiscordClient>() as DiscordSocketClient;
|
||||
@@ -51,13 +52,7 @@ namespace PluralKit.Bot
|
||||
.AddTransient(_ => _config.GetSection("PluralKit").Get<CoreConfig>() ?? new CoreConfig())
|
||||
.AddTransient(_ => _config.GetSection("PluralKit").GetSection("Bot").Get<BotConfig>() ?? new BotConfig())
|
||||
|
||||
.AddTransient<IDbConnection>(svc =>
|
||||
{
|
||||
|
||||
var conn = new NpgsqlConnection(svc.GetRequiredService<CoreConfig>().Database);
|
||||
conn.Open();
|
||||
return conn;
|
||||
})
|
||||
.AddTransient(svc => new DbConnectionFactory(svc.GetRequiredService<CoreConfig>().Database))
|
||||
|
||||
.AddSingleton<IDiscordClient, DiscordSocketClient>()
|
||||
.AddSingleton<Bot>()
|
||||
@@ -170,9 +165,10 @@ namespace PluralKit.Bot
|
||||
// If it does, fetch the sender's system (because most commands need that) into the context,
|
||||
// and start command execution
|
||||
// Note system may be null if user has no system, hence `OrDefault`
|
||||
var connection = serviceScope.ServiceProvider.GetService<IDbConnection>();
|
||||
var system = await connection.QueryFirstOrDefaultAsync<PKSystem>("select systems.* from systems, accounts where accounts.uid = @Id and systems.id = accounts.system", new { Id = arg.Author.Id });
|
||||
await _commands.ExecuteAsync(new PKCommandContext(_client, arg, connection, system), argPos, serviceScope.ServiceProvider);
|
||||
PKSystem system;
|
||||
using (var conn = serviceScope.ServiceProvider.GetService<DbConnectionFactory>().Obtain())
|
||||
system = await conn.QueryFirstOrDefaultAsync<PKSystem>("select systems.* from systems, accounts where accounts.uid = @Id and systems.id = accounts.system", new { Id = arg.Author.Id });
|
||||
await _commands.ExecuteAsync(new PKCommandContext(_client, arg, system), argPos, serviceScope.ServiceProvider);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -11,13 +11,13 @@ namespace PluralKit.Bot {
|
||||
|
||||
public class LogChannelService {
|
||||
private IDiscordClient _client;
|
||||
private IDbConnection _connection;
|
||||
private DbConnectionFactory _conn;
|
||||
private EmbedService _embed;
|
||||
|
||||
public LogChannelService(IDiscordClient client, IDbConnection connection, EmbedService embed)
|
||||
public LogChannelService(IDiscordClient client, DbConnectionFactory conn, EmbedService embed)
|
||||
{
|
||||
this._client = client;
|
||||
this._connection = connection;
|
||||
this._conn = conn;
|
||||
this._embed = embed;
|
||||
}
|
||||
|
||||
@@ -30,9 +30,14 @@ namespace PluralKit.Bot {
|
||||
}
|
||||
|
||||
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
|
||||
var server = await _connection.QueryFirstOrDefaultAsync<ServerDefinition>("select * from servers where id = @Id", new { Id = guild.Id });
|
||||
if (server?.LogChannel == null) return null;
|
||||
return await _client.GetChannelAsync(server.LogChannel.Value) as ITextChannel;
|
||||
using (var conn = _conn.Obtain())
|
||||
{
|
||||
var server =
|
||||
await conn.QueryFirstOrDefaultAsync<ServerDefinition>("select * from servers where id = @Id",
|
||||
new {Id = guild.Id});
|
||||
if (server?.LogChannel == null) return null;
|
||||
return await _client.GetChannelAsync(server.LogChannel.Value) as ITextChannel;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetLogChannel(IGuild guild, ITextChannel newLogChannel) {
|
||||
@@ -41,7 +46,12 @@ namespace PluralKit.Bot {
|
||||
LogChannel = newLogChannel?.Id
|
||||
};
|
||||
|
||||
await _connection.QueryAsync("insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel", def);
|
||||
using (var conn = _conn.Obtain())
|
||||
{
|
||||
await conn.QueryAsync(
|
||||
"insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel",
|
||||
def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,17 +28,17 @@ namespace PluralKit.Bot
|
||||
|
||||
class ProxyService {
|
||||
private IDiscordClient _client;
|
||||
private IDbConnection _connection;
|
||||
private DbConnectionFactory _conn;
|
||||
private LogChannelService _logger;
|
||||
private WebhookCacheService _webhookCache;
|
||||
private MessageStore _messageStorage;
|
||||
private EmbedService _embeds;
|
||||
|
||||
public ProxyService(IDiscordClient client, WebhookCacheService webhookCache, IDbConnection connection, LogChannelService logger, MessageStore messageStorage, EmbedService embeds)
|
||||
public ProxyService(IDiscordClient client, WebhookCacheService webhookCache, DbConnectionFactory conn, LogChannelService logger, MessageStore messageStorage, EmbedService embeds)
|
||||
{
|
||||
_client = client;
|
||||
_webhookCache = webhookCache;
|
||||
_connection = connection;
|
||||
_conn = conn;
|
||||
_logger = logger;
|
||||
_messageStorage = messageStorage;
|
||||
_embeds = embeds;
|
||||
@@ -76,11 +76,16 @@ namespace PluralKit.Bot
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task HandleMessageAsync(IMessage message) {
|
||||
var results = await _connection.QueryAsync<PKMember, PKSystem, ProxyDatabaseResult>(
|
||||
"select members.*, systems.* from members, systems, accounts where members.system = systems.id and accounts.system = systems.id and accounts.uid = @Uid",
|
||||
(member, system) =>
|
||||
new ProxyDatabaseResult { Member = member, System = system }, new { Uid = message.Author.Id });
|
||||
public async Task HandleMessageAsync(IMessage message)
|
||||
{
|
||||
IEnumerable<ProxyDatabaseResult> results;
|
||||
using (var conn = _conn.Obtain())
|
||||
{
|
||||
results = await conn.QueryAsync<PKMember, PKSystem, ProxyDatabaseResult>(
|
||||
"select members.*, systems.* from members, systems, accounts where members.system = systems.id and accounts.system = systems.id and accounts.uid = @Uid",
|
||||
(member, system) =>
|
||||
new ProxyDatabaseResult {Member = member, System = system}, new {Uid = message.Author.Id});
|
||||
}
|
||||
|
||||
// Find a member with proxy tags matching the message
|
||||
var match = GetProxyTagMatch(message.Content, results);
|
||||
|
||||
@@ -152,14 +152,12 @@ namespace PluralKit.Bot
|
||||
/// Subclass of ICommandContext with PK-specific additional fields and functionality
|
||||
public class PKCommandContext : SocketCommandContext
|
||||
{
|
||||
public IDbConnection Connection { get; }
|
||||
public PKSystem SenderSystem { get; }
|
||||
|
||||
private object _entity;
|
||||
|
||||
public PKCommandContext(DiscordSocketClient client, SocketUserMessage msg, IDbConnection connection, PKSystem system) : base(client, msg)
|
||||
public PKCommandContext(DiscordSocketClient client, SocketUserMessage msg, PKSystem system) : base(client, msg)
|
||||
{
|
||||
Connection = connection;
|
||||
SenderSystem = system;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user