feat: store shard status in Redis

This commit is contained in:
spiral
2022-01-22 03:52:52 -05:00
parent 22faa47d00
commit 0419ced0d2
18 changed files with 602 additions and 183 deletions

View File

@@ -1,28 +0,0 @@
using Dapper;
using NodaTime;
namespace PluralKit.Core;
public partial class ModelRepository
{
public Task<IEnumerable<PKShardInfo>> GetShards() =>
_db.Execute(conn => conn.QueryAsync<PKShardInfo>("select * from shards order by id"));
public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) =>
conn.ExecuteAsync(
"insert into shards (id, status) values (@Id, @Status) on conflict (id) do update set status = @Status",
new { Id = shard, Status = status });
public Task ClearShardStatus() => _db.Execute(conn => conn.ExecuteAsync("update shards set status = 0"));
public Task RegisterShardHeartbeat(IPKConnection conn, int shard, Duration ping) =>
conn.ExecuteAsync(
"insert into shards (id, last_heartbeat, ping) values (@Id, now(), @Ping) on conflict (id) do update set last_heartbeat = now(), ping = @Ping",
new { Id = shard, Ping = ping.TotalSeconds });
public Task RegisterShardConnection(IPKConnection conn, int shard) =>
conn.ExecuteAsync(
"insert into shards (id, last_connection) values (@Id, now()) on conflict (id) do update set last_connection = now()",
new { Id = shard });
}

View File

@@ -1,18 +0,0 @@
using NodaTime;
namespace PluralKit.Core;
public class PKShardInfo
{
public enum ShardStatus
{
Down = 0,
Up = 1
}
public int Id { get; }
public ShardStatus Status { get; }
public float? Ping { get; }
public Instant? LastHeartbeat { get; }
public Instant? LastConnection { get; }
}

View File

@@ -22,6 +22,7 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Dapper.Contrib" Version="2.0.35" />
<PackageReference Include="Google.Protobuf" Version="3.13.0" />
<PackageReference Include="ipnetwork2" Version="2.5.381" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.10" />

View File

@@ -0,0 +1,22 @@
using Google.Protobuf;
namespace PluralKit.Core;
public static class Proto
{
private static Dictionary<string, MessageParser> _parser = new();
public static byte[] Marshal(this IMessage message) => message.ToByteArray();
public static T Unmarshal<T>(this byte[] message) where T : IMessage<T>, new()
{
var type = typeof(T).ToString();
if (_parser.ContainsKey(type))
return (T)_parser[type].ParseFrom(message);
else
{
_parser.Add(type, new MessageParser<T>(() => new T()));
return Unmarshal<T>(message);
}
}
}

View File

@@ -61,6 +61,16 @@
"System.Reflection.Emit": "4.7.0"
}
},
"Google.Protobuf": {
"type": "Direct",
"requested": "[3.13.0, )",
"resolved": "3.13.0",
"contentHash": "/6VgKCh0P59x/rYsBkCvkUanF0TeUYzwV9hzLIWgt23QRBaKHoxaaMkidEWhKibLR88c3PVCXyyrx9Xlb+Ne6w==",
"dependencies": {
"System.Memory": "4.5.2",
"System.Runtime.CompilerServices.Unsafe": "4.5.2"
}
},
"IPNetwork2": {
"type": "Direct",
"requested": "[2.5.381, )",
@@ -950,6 +960,11 @@
"System.Threading": "4.3.0"
}
},
"System.Memory": {
"type": "Transitive",
"resolved": "4.5.2",
"contentHash": "fvq1GNmUFwbKv+aLVYYdgu/+gc8Nu9oFujOxIjPrsf+meis9JBzTPDL6aP/eeGOz9yPj6rRLUbOjKMpsMEWpNg=="
},
"System.Net.Http": {
"type": "Transitive",
"resolved": "4.3.0",