Reuse a singleton HttpClient for proxying

This commit is contained in:
Ske 2019-12-23 00:29:04 +01:00
parent c70556f5f9
commit 05282fd167
3 changed files with 5 additions and 14 deletions

View File

@ -96,6 +96,7 @@ namespace PluralKit.Bot
// LogLevel = LogSeverity.Debug // We filter log levels in Serilog, so just pass everything through (Debug is lower than Verbose) // LogLevel = LogSeverity.Debug // We filter log levels in Serilog, so just pass everything through (Debug is lower than Verbose)
})) }))
.AddSingleton<Bot>() .AddSingleton<Bot>()
.AddSingleton(_ => new HttpClient { Timeout = TimeSpan.FromSeconds(5) })
.AddTransient<CommandTree>() .AddTransient<CommandTree>()
.AddTransient<SystemCommands>() .AddTransient<SystemCommands>()

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
@ -21,7 +20,7 @@ namespace PluralKit.Bot
public string InnerText; public string InnerText;
} }
class ProxyService: IDisposable { class ProxyService {
private IDiscordClient _client; private IDiscordClient _client;
private LogChannelService _logChannel; private LogChannelService _logChannel;
private IDataStore _data; private IDataStore _data;
@ -30,8 +29,6 @@ namespace PluralKit.Bot
private WebhookExecutorService _webhookExecutor; private WebhookExecutorService _webhookExecutor;
private ProxyCacheService _cache; private ProxyCacheService _cache;
private HttpClient _httpClient;
public ProxyService(IDiscordClient client, LogChannelService logChannel, IDataStore data, EmbedService embeds, ILogger logger, ProxyCacheService cache, WebhookExecutorService webhookExecutor) public ProxyService(IDiscordClient client, LogChannelService logChannel, IDataStore data, EmbedService embeds, ILogger logger, ProxyCacheService cache, WebhookExecutorService webhookExecutor)
{ {
_client = client; _client = client;
@ -41,8 +38,6 @@ namespace PluralKit.Bot
_cache = cache; _cache = cache;
_webhookExecutor = webhookExecutor; _webhookExecutor = webhookExecutor;
_logger = logger.ForContext<ProxyService>(); _logger = logger.ForContext<ProxyService>();
_httpClient = new HttpClient();
} }
private ProxyMatch GetProxyTagMatch(string message, IEnumerable<ProxyCacheService.ProxyDatabaseResult> potentialMembers) private ProxyMatch GetProxyTagMatch(string message, IEnumerable<ProxyCacheService.ProxyDatabaseResult> potentialMembers)
@ -272,10 +267,5 @@ namespace PluralKit.Bot
_logger.Information("Bulk deleting {Count} messages in channel {Channel}", messages.Count, channel.Id); _logger.Information("Bulk deleting {Count} messages in channel {Channel}", messages.Count, channel.Id);
await _data.DeleteMessagesBulk(messages.Select(m => m.Id).ToList()); await _data.DeleteMessagesBulk(messages.Select(m => m.Id).ToList());
} }
public void Dispose()
{
_httpClient.Dispose();
}
} }
} }

View File

@ -25,12 +25,12 @@ namespace PluralKit.Bot
private IMetrics _metrics; private IMetrics _metrics;
private HttpClient _client; private HttpClient _client;
public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger) public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger, HttpClient client)
{ {
_metrics = metrics; _metrics = metrics;
_webhookCache = webhookCache; _webhookCache = webhookCache;
_client = client;
_logger = logger.ForContext<WebhookExecutorService>(); _logger = logger.ForContext<WebhookExecutorService>();
_client = new HttpClient {Timeout = TimeSpan.FromSeconds(5)};
} }
public async Task<ulong> ExecuteWebhook(ITextChannel channel, string name, string avatarUrl, string content, IReadOnlyCollection<IAttachment> attachments) public async Task<ulong> ExecuteWebhook(ITextChannel channel, string name, string avatarUrl, string content, IReadOnlyCollection<IAttachment> attachments)