PluralKit/PluralKit.Bot/Services/ShardInfoService.cs

152 lines
4.9 KiB
C#
Raw Normal View History

2021-01-30 00:07:43 +00:00
using System.Net.WebSockets;
2019-12-22 11:50:47 +00:00
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;
2021-06-10 10:52:47 +00:00
using PluralKit.Core;
using Serilog;
2019-12-22 11:50:47 +00:00
namespace PluralKit.Bot;
// TODO: how much of this do we need now that we have logging in the shard library?
// A lot could probably be cleaned up...
public class ShardInfoService
2019-12-22 11:50:47 +00:00
{
private readonly Cluster _client;
private readonly IDatabase _db;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly ModelRepository _repo;
private readonly Dictionary<int, ShardInfo> _shardInfo = new();
public ShardInfoService(ILogger logger, Cluster client, IMetrics metrics, IDatabase db, ModelRepository repo)
2019-12-22 11:50:47 +00:00
{
_client = client;
_metrics = metrics;
_db = db;
_repo = repo;
_logger = logger.ForContext<ShardInfoService>();
}
2019-12-22 11:50:47 +00:00
public ICollection<ShardInfo> Shards => _shardInfo.Values;
2021-06-10 10:52:47 +00:00
public void Init()
{
// 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
_client.ShardCreated += InitializeShard;
}
2021-08-27 15:03:47 +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));
}
private void InitializeShard(Shard shard)
{
// Get or insert info in the client dict
if (_shardInfo.TryGetValue(shard.ShardId, out var info))
2019-12-22 11:50:47 +00:00
{
// Skip adding listeners if we've seen this shard & already added listeners to it
if (info.HasAttachedListeners)
return;
2020-05-09 13:44:56 +00:00
}
else
2020-06-14 20:19:12 +00:00
{
_shardInfo[shard.ShardId] = info = new ShardInfo();
2020-06-14 20:19:12 +00:00
}
// Call our own SocketOpened listener manually (and then attach the listener properly)
2019-12-22 11:50:47 +00:00
// Register listeners for new shards
shard.Resumed += () => ReadyOrResumed(shard);
shard.Ready += () => ReadyOrResumed(shard);
shard.SocketClosed += (closeStatus, message) => SocketClosed(shard, closeStatus, message);
shard.HeartbeatReceived += latency => Heartbeated(shard, latency);
// Register that we've seen it
info.HasAttachedListeners = true;
}
2019-12-22 11:50:47 +00:00
private ShardInfo TryGetShard(Shard shard)
{
// If we haven't seen this shard before, add it to the dict!
// 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-08-27 15:03:47 +00:00
private void ReadyOrResumed(Shard shard)
{
var info = TryGetShard(shard);
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
ReportShardStatus();
_ = ExecuteWithDatabase(async c =>
{
await _repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Up);
await _repo.RegisterShardConnection(c, shard.ShardId);
});
}
2021-08-27 15:03:47 +00:00
private void SocketClosed(Shard shard, WebSocketCloseStatus? closeStatus, string message)
{
var info = TryGetShard(shard);
info.DisconnectionCount++;
info.Connected = false;
ReportShardStatus();
_ = ExecuteWithDatabase(c =>
_repo.SetShardStatus(c, shard.ShardId, PKShardInfo.ShardStatus.Down));
}
private void Heartbeated(Shard shard, TimeSpan latency)
{
var info = TryGetShard(shard);
info.LastHeartbeatTime = SystemClock.Instance.GetCurrentInstant();
info.Connected = true;
info.ShardLatency = latency.ToDuration();
_ = ExecuteWithDatabase(c =>
_repo.RegisterShardHeartbeat(c, shard.ShardId, latency.ToDuration()));
}
2021-06-10 10:52:47 +00:00
private async Task ExecuteWithDatabase(Func<IPKConnection, Task> fn)
{
// wrapper function to log errors because we "async void" it at call site :(
try
{
await using var conn = await _db.Obtain();
await fn(conn);
}
catch (Exception e)
2021-06-10 10:52:47 +00:00
{
_logger.Error(e, "Error persisting shard status");
2019-12-22 11:50:47 +00:00
}
}
public ShardInfo GetShardInfo(int shardId) => _shardInfo[shardId];
public class ShardInfo
{
public bool Connected;
public int DisconnectionCount;
public bool HasAttachedListeners;
public Instant LastConnectionTime;
public Instant LastHeartbeatTime;
public Duration ShardLatency;
2019-12-22 11:50:47 +00:00
}
}