PluralKit/Myriad/Gateway/Cluster.cs

95 lines
3.2 KiB
C#
Raw Normal View History

2020-12-22 12:15:26 +00:00
using System.Collections.Concurrent;
2021-06-09 14:22:10 +00:00
using Myriad.Gateway.Limit;
2020-12-22 12:15:26 +00:00
using Myriad.Types;
using Serilog;
2022-01-13 17:26:25 +00:00
using StackExchange.Redis;
namespace Myriad.Gateway;
public class Cluster
2020-12-22 12:15:26 +00:00
{
private readonly GatewaySettings _gatewaySettings;
private readonly ILogger _logger;
private readonly ConcurrentDictionary<int, Shard> _shards = new();
private IGatewayRatelimiter? _ratelimiter;
public Cluster(GatewaySettings gatewaySettings, ILogger logger)
{
_gatewaySettings = gatewaySettings;
_logger = logger.ForContext<Cluster>();
}
public Func<Shard, IGatewayEvent, Task>? EventReceived { get; set; }
public IReadOnlyDictionary<int, Shard> Shards => _shards;
public event Action<Shard>? ShardCreated;
2022-01-13 17:26:25 +00:00
public async Task Start(GatewayInfo.Bot info, ConnectionMultiplexer? conn = null)
{
2022-01-13 17:26:25 +00:00
await Start(info.Url, 0, info.Shards - 1, info.Shards, info.SessionStartLimit.MaxConcurrency, conn);
}
2022-01-13 17:26:25 +00:00
public async Task Start(string url, int shardMin, int shardMax, int shardTotal, int recommendedConcurrency, ConnectionMultiplexer? conn = null)
{
2022-01-13 17:26:25 +00:00
_ratelimiter = GetRateLimiter(recommendedConcurrency, conn);
var shardCount = shardMax - shardMin + 1;
_logger.Information("Starting {ShardCount} of {ShardTotal} shards (#{ShardMin}-#{ShardMax}) at {Url}",
shardCount, shardTotal, shardMin, shardMax, url);
for (var i = shardMin; i <= shardMax; i++)
CreateAndAddShard(url, new ShardInfo(i, shardTotal));
await StartShards();
}
private async Task StartShards()
2020-12-22 12:15:26 +00:00
{
_logger.Information("Connecting shards...");
foreach (var shard in _shards.Values)
await shard.Start();
}
private void CreateAndAddShard(string url, ShardInfo shardInfo)
{
var shard = new Shard(_gatewaySettings, shardInfo, _ratelimiter!, url, _logger);
shard.OnEventReceived += evt => OnShardEventReceived(shard, evt);
_shards[shardInfo.ShardId] = shard;
ShardCreated?.Invoke(shard);
}
private async Task OnShardEventReceived(Shard shard, IGatewayEvent evt)
{
if (EventReceived != null)
await EventReceived(shard, evt);
}
private int GetActualShardConcurrency(int recommendedConcurrency)
{
if (_gatewaySettings.MaxShardConcurrency == null)
return recommendedConcurrency;
return Math.Min(_gatewaySettings.MaxShardConcurrency.Value, recommendedConcurrency);
}
2022-01-13 17:26:25 +00:00
private IGatewayRatelimiter GetRateLimiter(int recommendedConcurrency, ConnectionMultiplexer? conn = null)
{
2022-01-13 17:26:25 +00:00
var concurrency = GetActualShardConcurrency(recommendedConcurrency);
if (_gatewaySettings.UseRedisRatelimiter)
{
if (conn != null)
return new RedisRatelimiter(_logger, conn, concurrency);
else
_logger.Warning("Tried to get Redis ratelimiter but connection is null! Continuing with local ratelimiter.");
}
if (_gatewaySettings.GatewayQueueUrl != null)
return new TwilightGatewayRatelimiter(_logger, _gatewaySettings.GatewayQueueUrl);
return new LocalGatewayRatelimiter(_logger, concurrency);
2020-12-22 12:15:26 +00:00
}
}