From 3e26a22c68f023a342d1fa2eef0573c468f7469d Mon Sep 17 00:00:00 2001 From: "a.bozhenov" Date: Tue, 9 Jul 2019 18:27:19 +0300 Subject: [PATCH] Cleanup --- ZeroLevel/Services/Network/DiscoveryClient.cs | 211 ------------------ ZeroLevel/ZeroLevel.csproj | 9 +- 2 files changed, 5 insertions(+), 215 deletions(-) delete mode 100644 ZeroLevel/Services/Network/DiscoveryClient.cs diff --git a/ZeroLevel/Services/Network/DiscoveryClient.cs b/ZeroLevel/Services/Network/DiscoveryClient.cs deleted file mode 100644 index 74c4d2a..0000000 --- a/ZeroLevel/Services/Network/DiscoveryClient.cs +++ /dev/null @@ -1,211 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Threading; -using ZeroLevel.Models; -using ZeroLevel.Services.Collections; - -namespace ZeroLevel.Network -{ - public class DiscoveryClient - : IDiscoveryClient - { - private sealed class DCRouter: - IDisposable - { - private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); - private IEnumerable _empty = Enumerable.Empty(); - private List _services = new List(); - - private Dictionary> _tableByKey; - private Dictionary> _tableByGroups; - private Dictionary> _tableByTypes; - - internal void Update(IEnumerable records) - { - if (records == null) - { - Log.Warning("[DiscoveryClient] UpdateServiceListInfo. Discrovery response is empty"); - return; - } - var services = new List(); - 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(g)); - _tableByTypes = _services.GroupBy(r => r.Type).ToDictionary(g => g.Key, g => new RoundRobinCollection(g)); - _tableByGroups = _services.GroupBy(r => r.Group).ToDictionary(g => g.Key, g => new RoundRobinCollection(g)); - } - catch (Exception ex) - { - Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Update local routing table error."); - } - finally - { - _lock.ExitWriteLock(); - } - } - - public ServiceEndpointInfo GetService(string serviceKey, string endpoint) - { - var key = serviceKey.ToUpperInvariant(); - _lock.EnterReadLock(); - try - { - if (_tableByKey.ContainsKey(key) && _tableByKey[key].Count > 0) - { - return _tableByKey[key].Find(s => s.Endpoint.Equals(endpoint, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); - } - } - finally - { - _lock.ExitReadLock(); - } - return null; - } - - public IEnumerable GetServiceEndpoints(string serviceKey) - { - var key = serviceKey.Trim().ToUpperInvariant(); - _lock.EnterReadLock(); - try - { - if (_tableByKey.ContainsKey(key) && _tableByKey[key].Count > 0) - { - return _tableByKey[key].GetCurrentSeq(); - } - } - finally - { - _lock.ExitReadLock(); - } - return _empty; - } - - public IEnumerable GetServiceEndpointsByGroup(string serviceGroup) - { - var group = serviceGroup.Trim().ToUpperInvariant(); - _lock.EnterReadLock(); - try - { - if (_tableByGroups.ContainsKey(group) && _tableByGroups[group].Count > 0) - { - return _tableByGroups[group].GetCurrentSeq(); - } - } - finally - { - _lock.ExitReadLock(); - } - return _empty; - } - - public IEnumerable GetServiceEndpointsByType(string serviceType) - { - var type = serviceType.Trim().ToUpperInvariant(); - _lock.EnterReadLock(); - try - { - if (_tableByTypes.ContainsKey(type) && _tableByTypes[type].Count > 0) - { - return _tableByTypes[type].GetCurrentSeq(); - } - } - finally - { - _lock.ExitReadLock(); - } - return _empty; - } - - public void Dispose() - { - _lock.Dispose(); - } - } - - private readonly DCRouter _router = new DCRouter(); - private readonly ExClient _discoveryServerClient; - - public DiscoveryClient(ExClient client) - { - _discoveryServerClient = client; - UpdateServiceListInfo(); - Sheduller.RemindEvery(TimeSpan.FromSeconds(30), UpdateServiceListInfo); - } - - private void UpdateServiceListInfo() - { - _discoveryServerClient.ForceConnect(); - if (_discoveryServerClient.Status == SocketClientStatus.Working) - { - try - { - var ir = _discoveryServerClient.Request>("services", records => _router.Update(records)); - if (!ir.Success) - { - Log.Warning($"[DiscoveryClient] UpdateServiceListInfo. Error request to inbox 'services'. {ir.Comment}"); - } - } - catch (Exception ex) - { - Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Discrovery service response is absent"); - } - } - else - { - Log.Warning("[DiscoveryClient] UpdateServiceListInfo. No connection to discovery server"); - } - } - - public bool Register(ZeroServiceInfo info) - { - _discoveryServerClient.ForceConnect(); - if (_discoveryServerClient.Status == SocketClientStatus.Working) - { - bool result = false; - try - { - _discoveryServerClient.Request("register", info, r => - { - result = r.Success; - if (!result) - { - Log.Warning($"[DiscoveryClient] Register canceled. Discovery reason: {r.Comment}. Comment: {r.Comment}"); - } - }); - } - catch (Exception ex) - { - Log.Error(ex, "[DiscoveryClient] Register fault"); - } - return result; - } - else - { - Log.Warning("[DiscoveryClient] Register. No connection to discovery server"); - return false; - } - } - - public IEnumerable GetServiceEndpoints(string serviceKey) => _router.GetServiceEndpoints(serviceKey); - public IEnumerable GetServiceEndpointsByGroup(string serviceGroup) => _router.GetServiceEndpointsByGroup(serviceGroup); - public IEnumerable GetServiceEndpointsByType(string serviceType) => _router.GetServiceEndpointsByType(serviceType); - public ServiceEndpointInfo GetService(string serviceKey, string endpoint) => _router.GetService(serviceKey, endpoint); - - public void Dispose() - { - _router.Dispose(); - _discoveryServerClient.Dispose(); - } - } -} \ No newline at end of file diff --git a/ZeroLevel/ZeroLevel.csproj b/ZeroLevel/ZeroLevel.csproj index f588533..1095f6f 100644 --- a/ZeroLevel/ZeroLevel.csproj +++ b/ZeroLevel/ZeroLevel.csproj @@ -5,16 +5,17 @@ Infrastructure layer library ogoun ogoun - 2.0.9.0 - Append transliteration ru-latin -Fix .net core bug https://github.com/dotnet/project-system/issues/589 + 3.0.0.0 + New bootstrap concept +New network layer +No backwards compatibility with previous version https://github.com/ogoun/Zero/wiki Copyright Ogoun 2019 https://opensource.org/licenses/MIT https://raw.githubusercontent.com/ogoun/Zero/master/zero.png https://github.com/ogoun/Zero GitHub - 2.0.9 + 3.0.0