2019-12-22 11:50:47 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
using DSharpPlus;
|
2020-05-01 15:30:12 +00:00
|
|
|
using DSharpPlus.EventArgs;
|
2019-12-22 11:50:47 +00:00
|
|
|
|
|
|
|
using NodaTime;
|
2020-05-01 15:30:12 +00:00
|
|
|
using NodaTime.Extensions;
|
|
|
|
|
|
|
|
using Serilog;
|
2019-12-22 11:50:47 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class ShardInfoService
|
|
|
|
{
|
|
|
|
public class ShardInfo
|
|
|
|
{
|
|
|
|
public Instant LastConnectionTime;
|
2020-05-01 15:30:12 +00:00
|
|
|
public Instant LastHeartbeatTime;
|
2019-12-22 11:50:47 +00:00
|
|
|
public int DisconnectionCount;
|
2020-05-01 15:30:12 +00:00
|
|
|
public Duration ShardLatency;
|
|
|
|
public bool Connected;
|
2019-12-22 11:50:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 15:30:12 +00:00
|
|
|
private ILogger _logger;
|
2019-12-22 11:50:47 +00:00
|
|
|
private Dictionary<int, ShardInfo> _shardInfo = new Dictionary<int, ShardInfo>();
|
|
|
|
|
2020-05-01 15:30:12 +00:00
|
|
|
public ShardInfoService(ILogger logger)
|
|
|
|
{
|
|
|
|
_logger = logger.ForContext<ShardInfoService>();
|
|
|
|
}
|
|
|
|
|
2019-12-22 11:50:47 +00:00
|
|
|
public void Init(DiscordShardedClient client)
|
|
|
|
{
|
2020-05-01 18:27:51 +00:00
|
|
|
foreach (var (shardId, shard) in client.ShardClients)
|
2020-05-01 15:30:12 +00:00
|
|
|
{
|
2020-05-01 18:27:51 +00:00
|
|
|
_shardInfo[shardId] = new ShardInfo();
|
2019-12-22 11:50:47 +00:00
|
|
|
|
2020-05-01 15:30:12 +00:00
|
|
|
shard.Heartbeated += Heartbeated;
|
|
|
|
shard.SocketClosed += SocketClosed;
|
|
|
|
shard.Ready += Ready;
|
|
|
|
shard.Resumed += Resumed;
|
2020-05-01 18:27:51 +00:00
|
|
|
shard.SocketOpened += () => SocketOpened(shard);
|
2020-05-01 15:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 11:50:47 +00:00
|
|
|
|
2020-05-01 18:27:51 +00:00
|
|
|
private Task SocketOpened(DiscordClient e)
|
2019-12-22 11:50:47 +00:00
|
|
|
{
|
2020-05-01 18:27:51 +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'
|
|
|
|
_logger.Information("Shard #{Shard} opened socket", e.ShardId);
|
2019-12-22 11:50:47 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:27:51 +00:00
|
|
|
private ShardInfo UpdateShard(DiscordClient shard)
|
2019-12-22 11:50:47 +00:00
|
|
|
{
|
2020-05-01 18:27:51 +00:00
|
|
|
// If we haven't seen this shard before, add it to the dict!
|
|
|
|
if (!_shardInfo.TryGetValue(shard.ShardId, out var info))
|
|
|
|
_shardInfo[shard.ShardId] = info = new ShardInfo();
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task Resumed(ReadyEventArgs e)
|
|
|
|
{
|
|
|
|
_logger.Information("Shard #{Shard} resumed connection", e.Client.ShardId);
|
|
|
|
|
|
|
|
var info = UpdateShard(e.Client);
|
|
|
|
// info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
info.Connected = true;
|
2019-12-22 11:50:47 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:27:51 +00:00
|
|
|
private Task Ready(ReadyEventArgs e)
|
2019-12-22 11:50:47 +00:00
|
|
|
{
|
2020-05-01 18:27:51 +00:00
|
|
|
_logger.Information("Shard #{Shard} sent Ready event", e.Client.ShardId);
|
|
|
|
|
|
|
|
var info = UpdateShard(e.Client);
|
|
|
|
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
info.Connected = true;
|
2019-12-22 11:50:47 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:30:12 +00:00
|
|
|
private Task SocketClosed(SocketCloseEventArgs e)
|
2019-12-22 11:50:47 +00:00
|
|
|
{
|
2020-05-01 15:30:12 +00:00
|
|
|
_logger.Warning("Shard #{Shard} disconnected ({CloseCode}: {CloseMessage})", e.Client.ShardId, e.CloseCode, e.CloseMessage);
|
2020-05-01 18:27:51 +00:00
|
|
|
|
|
|
|
var info = UpdateShard(e.Client);
|
|
|
|
info.DisconnectionCount++;
|
|
|
|
info.Connected = false;
|
2020-05-01 15:30:12 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task Heartbeated(HeartbeatEventArgs e)
|
|
|
|
{
|
|
|
|
var latency = Duration.FromMilliseconds(e.Ping);
|
|
|
|
_logger.Information("Shard #{Shard} received heartbeat (latency: {Latency} ms)", e.Client.ShardId, latency.Milliseconds);
|
2020-05-01 18:27:51 +00:00
|
|
|
|
|
|
|
var info = UpdateShard(e.Client);
|
|
|
|
info.LastHeartbeatTime = e.Timestamp.ToInstant();
|
|
|
|
info.Connected = true;
|
|
|
|
info.ShardLatency = latency;
|
2019-12-22 11:50:47 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2020-05-01 15:30:12 +00:00
|
|
|
|
|
|
|
public ShardInfo GetShardInfo(DiscordClient shard) => _shardInfo[shard.ShardId];
|
|
|
|
|
|
|
|
public ICollection<ShardInfo> Shards => _shardInfo.Values;
|
2019-12-22 11:50:47 +00:00
|
|
|
}
|
|
|
|
}
|