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)
}))
.AddSingleton<Bot>()
.AddSingleton(_ => new HttpClient { Timeout = TimeSpan.FromSeconds(5) })
.AddTransient<CommandTree>()
.AddTransient<SystemCommands>()

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
@ -21,7 +20,7 @@ namespace PluralKit.Bot
public string InnerText;
}
class ProxyService: IDisposable {
class ProxyService {
private IDiscordClient _client;
private LogChannelService _logChannel;
private IDataStore _data;
@ -29,9 +28,7 @@ namespace PluralKit.Bot
private ILogger _logger;
private WebhookExecutorService _webhookExecutor;
private ProxyCacheService _cache;
private HttpClient _httpClient;
public ProxyService(IDiscordClient client, LogChannelService logChannel, IDataStore data, EmbedService embeds, ILogger logger, ProxyCacheService cache, WebhookExecutorService webhookExecutor)
{
_client = client;
@ -41,8 +38,6 @@ namespace PluralKit.Bot
_cache = cache;
_webhookExecutor = webhookExecutor;
_logger = logger.ForContext<ProxyService>();
_httpClient = new HttpClient();
}
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);
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 HttpClient _client;
public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger)
public WebhookExecutorService(IMetrics metrics, WebhookCacheService webhookCache, ILogger logger, HttpClient client)
{
_metrics = metrics;
_webhookCache = webhookCache;
_client = client;
_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)