PluralKit/PluralKit.Bot/Services/ShardInfoService.cs

147 lines
5.6 KiB
C#
Raw Normal View History

2021-01-30 00:07:43 +00:00
using System;
2019-12-22 11:50:47 +00:00
using System.Collections.Generic;
2020-06-14 20:19:12 +00:00
using System.Linq;
2021-01-30 00:07:43 +00:00
using System.Net.WebSockets;
2019-12-22 11:50:47 +00:00
using System.Threading.Tasks;
2020-06-14 20:19:12 +00:00
using App.Metrics;
2021-01-15 10:29:43 +00:00
using Myriad.Gateway;
2019-12-22 11:50:47 +00:00
using NodaTime;
using NodaTime.Extensions;
using Serilog;
2019-12-22 11:50:47 +00:00
namespace PluralKit.Bot
{
2021-01-30 00:07:43 +00:00
// TODO: how much of this do we need now that we have logging in the shard library?
// A lot could probably be cleaned up...
2019-12-22 11:50:47 +00:00
public class ShardInfoService
{
public class ShardInfo
{
public bool HasAttachedListeners;
2019-12-22 11:50:47 +00:00
public Instant LastConnectionTime;
public Instant LastHeartbeatTime;
2019-12-22 11:50:47 +00:00
public int DisconnectionCount;
public Duration ShardLatency;
public bool Connected;
2019-12-22 11:50:47 +00:00
}
2020-08-29 11:46:27 +00:00
private readonly IMetrics _metrics;
private readonly ILogger _logger;
2021-01-30 00:07:43 +00:00
private readonly Cluster _client;
private readonly Dictionary<int, ShardInfo> _shardInfo = new();
2019-12-22 11:50:47 +00:00
2021-01-30 00:07:43 +00:00
public ShardInfoService(ILogger logger, Cluster client, IMetrics metrics)
{
2020-05-09 13:44:56 +00:00
_client = client;
2020-06-14 20:19:12 +00:00
_metrics = metrics;
_logger = logger.ForContext<ShardInfoService>();
}
2020-05-09 13:44:56 +00:00
public void Init()
2019-12-22 11:50:47 +00:00
{
2020-05-09 13:44:56 +00:00
// We initialize this before any shards are actually created and connected
// This means the client won't know the shard count, so we attach a listener every time a shard gets connected
2021-01-30 00:07:43 +00:00
_client.ShardCreated += InitializeShard;
2020-05-09 13:44:56 +00:00
}
2019-12-22 11:50:47 +00:00
2020-06-14 20:19:12 +00:00
private void ReportShardStatus()
{
foreach (var (id, shard) in _shardInfo)
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardLatency, new MetricTags("shard", id.ToString()), shard.ShardLatency.TotalMilliseconds);
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardsConnected, _shardInfo.Count(s => s.Value.Connected));
}
2021-01-30 00:07:43 +00:00
private void InitializeShard(Shard shard)
2020-05-09 13:44:56 +00:00
{
2021-01-30 00:07:43 +00:00
// Get or insert info in the client dict
if (_shardInfo.TryGetValue(shard.ShardId, out var info))
2020-05-09 13:44:56 +00:00
{
2021-01-30 00:07:43 +00:00
// Skip adding listeners if we've seen this shard & already added listeners to it
if (info.HasAttachedListeners)
return;
} else _shardInfo[shard.ShardId] = info = new ShardInfo();
// Call our own SocketOpened listener manually (and then attach the listener properly)
SocketOpened(shard);
shard.SocketOpened += () => SocketOpened(shard);
2020-05-09 13:44:56 +00:00
2021-01-30 00:07:43 +00:00
// Register listeners for new shards
_logger.Information("Attaching listeners to new shard #{Shard}", shard.ShardId);
shard.Resumed += () => Resumed(shard);
shard.Ready += () => Ready(shard);
shard.SocketClosed += (closeStatus, message) => SocketClosed(shard, closeStatus, message);
shard.HeartbeatReceived += latency => Heartbeated(shard, latency);
2021-01-30 00:07:43 +00:00
// Register that we've seen it
info.HasAttachedListeners = true;
}
2019-12-22 11:50:47 +00:00
2021-01-30 00:07:43 +00:00
private void SocketOpened(Shard shard)
2019-12-22 11:50:47 +00:00
{
// We do nothing else here, since this kinda doesn't mean *much*? It's only really started once we get Ready/Resumed
// And it doesn't get fired first time around since we don't have time to add the event listener before it's fired'
2020-11-15 12:53:31 +00:00
_logger.Information("Shard #{Shard} opened socket", shard.ShardId);
2019-12-22 11:50:47 +00:00
}
2021-01-30 00:07:43 +00:00
private ShardInfo TryGetShard(Shard shard)
2019-12-22 11:50:47 +00:00
{
// If we haven't seen this shard before, add it to the dict!
2020-05-09 13:44:56 +00:00
// I don't think this will ever occur since the shard number is constant up-front and we handle those
// in the RefreshShardList handler above but you never know, I guess~
if (!_shardInfo.TryGetValue(shard.ShardId, out var info))
_shardInfo[shard.ShardId] = info = new ShardInfo();
return info;
}
2021-01-30 00:07:43 +00:00
private void Resumed(Shard shard)
{
2020-11-15 12:53:31 +00:00
_logger.Information("Shard #{Shard} resumed connection", shard.ShardId);
2020-11-15 12:53:31 +00:00
var info = TryGetShard(shard);
// info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
2020-06-14 20:19:12 +00:00
ReportShardStatus();
2019-12-22 11:50:47 +00:00
}
2021-01-30 00:07:43 +00:00
private void Ready(Shard shard)
2019-12-22 11:50:47 +00:00
{
2020-11-15 12:53:31 +00:00
_logger.Information("Shard #{Shard} sent Ready event", shard.ShardId);
2020-11-15 12:53:31 +00:00
var info = TryGetShard(shard);
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
2020-06-14 20:19:12 +00:00
ReportShardStatus();
2019-12-22 11:50:47 +00:00
}
2021-01-30 00:07:43 +00:00
private void SocketClosed(Shard shard, WebSocketCloseStatus closeStatus, string message)
2019-12-22 11:50:47 +00:00
{
2021-01-30 00:07:43 +00:00
_logger.Warning("Shard #{Shard} disconnected ({CloseCode}: {CloseMessage})",
shard.ShardId, closeStatus, message);
2020-11-15 12:53:31 +00:00
var info = TryGetShard(shard);
info.DisconnectionCount++;
info.Connected = false;
2020-06-14 20:19:12 +00:00
ReportShardStatus();
}
2021-01-30 00:07:43 +00:00
private void Heartbeated(Shard shard, TimeSpan latency)
{
2021-01-30 00:07:43 +00:00
_logger.Information("Shard #{Shard} received heartbeat (latency: {Latency} ms)",
shard.ShardId, latency.Milliseconds);
2020-11-15 12:53:31 +00:00
var info = TryGetShard(shard);
2021-01-30 00:07:43 +00:00
info.LastHeartbeatTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
2021-01-30 00:07:43 +00:00
info.ShardLatency = latency.ToDuration();
2019-12-22 11:50:47 +00:00
}
2021-01-15 10:29:43 +00:00
public ShardInfo GetShardInfo(Shard shard) => _shardInfo[shard.ShardId];
public ICollection<ShardInfo> Shards => _shardInfo.Values;
2019-12-22 11:50:47 +00:00
}
}