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:
@@ -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);
|
||||
|
Reference in New Issue
Block a user