Refactor proxy service

This commit is contained in:
Ske
2019-08-12 05:47:55 +02:00
parent fda98bc25f
commit 145ecb91ad
4 changed files with 153 additions and 142 deletions

View File

@@ -8,18 +8,12 @@ using Serilog;
namespace PluralKit.Bot
{
public class WebhookCacheService: IDisposable
public class WebhookCacheService
{
public class WebhookCacheEntry
{
internal DiscordWebhookClient Client;
internal IWebhook Webhook;
}
public static readonly string WebhookName = "PluralKit Proxy Webhook";
private IDiscordClient _client;
private ConcurrentDictionary<ulong, Lazy<Task<WebhookCacheEntry>>> _webhooks;
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
private ILogger _logger;
@@ -27,45 +21,42 @@ namespace PluralKit.Bot
{
_client = client;
_logger = logger.ForContext<WebhookCacheService>();
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<WebhookCacheEntry>>>();
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
}
public async Task<WebhookCacheEntry> GetWebhook(ulong channelId)
public async Task<IWebhook> GetWebhook(ulong channelId)
{
var channel = await _client.GetChannelAsync(channelId) as ITextChannel;
if (channel == null) return null;
return await GetWebhook(channel);
}
public async Task<WebhookCacheEntry> GetWebhook(ITextChannel channel)
public async Task<IWebhook> GetWebhook(ITextChannel channel)
{
// We cache the webhook through a Lazy<Task<T>>, this way we make sure to only create one webhook per channel
// If the webhook is requested twice before it's actually been found, the Lazy<T> wrapper will stop the
// webhook from being created twice.
var lazyWebhookValue =
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<WebhookCacheEntry>>(() => GetOrCreateWebhook(channel)));
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => GetOrCreateWebhook(channel)));
// It's possible to "move" a webhook to a different channel after creation
// Here, we ensure it's actually still pointing towards the proper channel, and if not, wipe and refetch one.
var webhook = await lazyWebhookValue.Value;
if (webhook.Webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
return webhook;
}
public async Task<WebhookCacheEntry> InvalidateAndRefreshWebhook(WebhookCacheEntry webhook)
public async Task<IWebhook> InvalidateAndRefreshWebhook(IWebhook webhook)
{
_logger.Information("Refreshing webhook for channel {Channel}", webhook.Webhook.ChannelId);
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
_webhooks.TryRemove(webhook.Webhook.ChannelId, out _);
return await GetWebhook(webhook.Webhook.Channel);
_webhooks.TryRemove(webhook.ChannelId, out _);
return await GetWebhook(webhook.Channel);
}
private async Task<WebhookCacheEntry> GetOrCreateWebhook(ITextChannel channel)
{
var webhook = await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
return await DoCreateWebhookClient(webhook);
}
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel) =>
await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel)
{
_logger.Debug("Finding webhook for channel {Channel}", channel.Id);
@@ -78,28 +69,8 @@ namespace PluralKit.Bot
return channel.CreateWebhookAsync(WebhookName);
}
private Task<WebhookCacheEntry> DoCreateWebhookClient(IWebhook webhook)
{
// DiscordWebhookClient's ctor is synchronous despite doing web calls, so we wrap it in a Task
return Task.Run(() =>
{
return new WebhookCacheEntry
{
Client = new DiscordWebhookClient(webhook),
Webhook = webhook
};
});
}
private bool IsWebhookMine(IWebhook arg) => arg.Creator.Id == _client.CurrentUser.Id && arg.Name == WebhookName;
public int CacheSize => _webhooks.Count;
public void Dispose()
{
foreach (var entry in _webhooks.Values)
if (entry.IsValueCreated)
entry.Value.Result.Client.Dispose();
}
}
}