You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Network/DiscoveryClient.cs

211 lines
8.1 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
6 years ago
using System.Threading;
using ZeroLevel.Models;
using ZeroLevel.Services.Collections;
namespace ZeroLevel.Network
6 years ago
{
public class DiscoveryClient
: IDiscoveryClient
6 years ago
{
private sealed class DCRouter:
IDisposable
{
private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private IEnumerable<ServiceEndpointInfo> _empty = Enumerable.Empty<ServiceEndpointInfo>();
private List<ServiceEndpointInfo> _services = new List<ServiceEndpointInfo>();
private Dictionary<string, RoundRobinCollection<ServiceEndpointInfo>> _tableByKey;
private Dictionary<string, RoundRobinCollection<ServiceEndpointInfo>> _tableByGroups;
private Dictionary<string, RoundRobinCollection<ServiceEndpointInfo>> _tableByTypes;
6 years ago
internal void Update(IEnumerable<ServiceEndpointsInfo> records)
6 years ago
{
if (records == null)
{
Log.Warning("[DiscoveryClient] UpdateServiceListInfo. Discrovery response is empty");
return;
}
var services = new List<ServiceEndpointInfo>();
foreach (var service in records)
{
var key = service.ServiceKey.ToUpperInvariant();
var type = service.ServiceType.ToUpperInvariant();
var group = service.ServiceGroup.ToUpperInvariant();
services.AddRange(service.Endpoints.Select(e => new ServiceEndpointInfo { Endpoint = e, Group = group, Key = key, Type = type }));
}
_lock.EnterWriteLock();
try
{
_services = services;
_tableByKey = _services.GroupBy(r => r.Key).ToDictionary(g => g.Key, g => new RoundRobinCollection<ServiceEndpointInfo>(g));
_tableByTypes = _services.GroupBy(r => r.Type).ToDictionary(g => g.Key, g => new RoundRobinCollection<ServiceEndpointInfo>(g));
_tableByGroups = _services.GroupBy(r => r.Group).ToDictionary(g => g.Key, g => new RoundRobinCollection<ServiceEndpointInfo>(g));
}
catch (Exception ex)
{
Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Update local routing table error.");
}
finally
{
_lock.ExitWriteLock();
}
6 years ago
}
6 years ago
public ServiceEndpointInfo GetService(string serviceKey, string endpoint)
6 years ago
{
var key = serviceKey.ToUpperInvariant();
_lock.EnterReadLock();
try
6 years ago
{
if (_tableByKey.ContainsKey(key) && _tableByKey[key].Count > 0)
{
return _tableByKey[key].Find(s => s.Endpoint.Equals(endpoint, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
}
6 years ago
}
finally
{
_lock.ExitReadLock();
}
return null;
6 years ago
}
6 years ago
public IEnumerable<ServiceEndpointInfo> GetServiceEndpoints(string serviceKey)
6 years ago
{
var key = serviceKey.Trim().ToUpperInvariant();
_lock.EnterReadLock();
try
6 years ago
{
if (_tableByKey.ContainsKey(key) && _tableByKey[key].Count > 0)
{
return _tableByKey[key].GetCurrentSeq();
}
6 years ago
}
finally
{
_lock.ExitReadLock();
}
return _empty;
6 years ago
}
6 years ago
public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByGroup(string serviceGroup)
6 years ago
{
var group = serviceGroup.Trim().ToUpperInvariant();
_lock.EnterReadLock();
try
6 years ago
{
if (_tableByGroups.ContainsKey(group) && _tableByGroups[group].Count > 0)
{
return _tableByGroups[group].GetCurrentSeq();
}
6 years ago
}
finally
{
_lock.ExitReadLock();
}
return _empty;
6 years ago
}
6 years ago
public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByType(string serviceType)
6 years ago
{
var type = serviceType.Trim().ToUpperInvariant();
_lock.EnterReadLock();
try
{
if (_tableByTypes.ContainsKey(type) && _tableByTypes[type].Count > 0)
{
return _tableByTypes[type].GetCurrentSeq();
}
}
finally
6 years ago
{
_lock.ExitReadLock();
6 years ago
}
return _empty;
6 years ago
}
public void Dispose()
{
_lock.Dispose();
}
6 years ago
}
private readonly DCRouter _router = new DCRouter();
private readonly ExClient _discoveryServerClient;
6 years ago
public DiscoveryClient(ExClient client)
6 years ago
{
_discoveryServerClient = client;
6 years ago
UpdateServiceListInfo();
Sheduller.RemindEvery(TimeSpan.FromSeconds(30), UpdateServiceListInfo);
6 years ago
}
private void UpdateServiceListInfo()
{
_discoveryServerClient.ForceConnect();
if (_discoveryServerClient.Status == SocketClientStatus.Working)
6 years ago
{
try
6 years ago
{
6 years ago
var ir = _discoveryServerClient.Request<IEnumerable<ServiceEndpointsInfo>>("services", records => _router.Update(records));
if (!ir.Success)
{
Log.Warning($"[DiscoveryClient] UpdateServiceListInfo. Error request to inbox 'services'. {ir.Comment}");
}
6 years ago
}
catch (Exception ex)
6 years ago
{
Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Discrovery service response is absent");
}
6 years ago
}
else
6 years ago
{
Log.Warning("[DiscoveryClient] UpdateServiceListInfo. No connection to discovery server");
6 years ago
}
}
public bool Register(ZeroServiceInfo info)
6 years ago
{
_discoveryServerClient.ForceConnect();
if (_discoveryServerClient.Status == SocketClientStatus.Working)
6 years ago
{
bool result = false;
try
6 years ago
{
_discoveryServerClient.Request<ZeroServiceInfo, InvokeResult>("register", info, r =>
{
result = r.Success;
if (!result)
{
Log.Warning($"[DiscoveryClient] Register canceled. Discovery reason: {r.Comment}. Comment: {r.Comment}");
}
});
6 years ago
}
catch (Exception ex)
{
Log.Error(ex, "[DiscoveryClient] Register fault");
}
return result;
6 years ago
}
else
6 years ago
{
Log.Warning("[DiscoveryClient] Register. No connection to discovery server");
return false;
6 years ago
}
}
6 years ago
public IEnumerable<ServiceEndpointInfo> GetServiceEndpoints(string serviceKey) => _router.GetServiceEndpoints(serviceKey);
public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByGroup(string serviceGroup) => _router.GetServiceEndpointsByGroup(serviceGroup);
public IEnumerable<ServiceEndpointInfo> GetServiceEndpointsByType(string serviceType) => _router.GetServiceEndpointsByType(serviceType);
public ServiceEndpointInfo GetService(string serviceKey, string endpoint) => _router.GetService(serviceKey, endpoint);
public void Dispose()
{
_router.Dispose();
_discoveryServerClient.Dispose();
}
6 years ago
}
}

Powered by TurnKey Linux.