♻️ I have no idea what I have done

This commit is contained in:
2025-07-13 21:51:16 +08:00
parent afdbde951c
commit 03e26ef93c
29 changed files with 5933 additions and 645 deletions

View File

@ -2,12 +2,13 @@ using System.Text;
using dotnet_etcd.interfaces;
using Etcdserverpb;
using Google.Protobuf;
using Microsoft.Extensions.Logging;
namespace DysonNetwork.Shared.Registry;
public class ServiceRegistry(IEtcdClient etcd)
public class ServiceRegistry(IEtcdClient etcd, ILogger<ServiceRegistry> logger)
{
public async Task RegisterService(string serviceName, string serviceUrl, long leaseTtlSeconds = 60)
public async Task RegisterService(string serviceName, string serviceUrl, long leaseTtlSeconds = 60, CancellationToken cancellationToken = default)
{
var key = $"/services/{serviceName}";
var leaseResponse = await etcd.LeaseGrantAsync(new LeaseGrantRequest { TTL = leaseTtlSeconds });
@ -17,7 +18,18 @@ public class ServiceRegistry(IEtcdClient etcd)
Value = ByteString.CopyFrom(serviceUrl, Encoding.UTF8),
Lease = leaseResponse.ID
});
await etcd.LeaseKeepAlive(leaseResponse.ID, CancellationToken.None);
_ = Task.Run(async () =>
{
try
{
await etcd.LeaseKeepAlive(leaseResponse.ID, cancellationToken);
}
catch (Exception ex)
{
logger.LogError($"Lease keep-alive failed: {ex.Message}");
}
}, cancellationToken);
}
public async Task UnregisterService(string serviceName)