From 7d7090b4c947bb14c787fe39e5d1412c89341f89 Mon Sep 17 00:00:00 2001 From: Ogoun Date: Wed, 6 May 2020 19:16:24 +0300 Subject: [PATCH] Test refactoring --- .../Services/Network/Utils/RequestBuffer.cs | 86 ++++++++++++------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/ZeroLevel/Services/Network/Utils/RequestBuffer.cs b/ZeroLevel/Services/Network/Utils/RequestBuffer.cs index 2f5d2e7..7e6de8a 100644 --- a/ZeroLevel/Services/Network/Utils/RequestBuffer.cs +++ b/ZeroLevel/Services/Network/Utils/RequestBuffer.cs @@ -1,60 +1,89 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using ZeroLevel.Services.Pools; namespace ZeroLevel.Network { internal sealed class RequestBuffer { - private ConcurrentDictionary _requests = new ConcurrentDictionary(); - private static ObjectPool _ri_pool = new ObjectPool(() => new RequestInfo()); + private ConcurrentDictionary> _callbacks = new ConcurrentDictionary>(); + private ConcurrentDictionary> _fallbacks = new ConcurrentDictionary>(); + private ConcurrentDictionary _timeouts = new ConcurrentDictionary(); - public void RegisterForFrame(int identity, Action callback, Action fail = null) + public void RegisterForFrame(int identity, Action callback, Action fallback = null) { - var ri = _ri_pool.Allocate(); - ri.Reset(callback, fail); - _requests[identity] = ri; + if (callback != null) + { + _callbacks.TryAdd(identity, callback); + } + if (fallback != null) + { + _fallbacks.TryAdd(identity, fallback); + } } - public void Fail(long frameId, string message) + public void Fail(long identity, string message) { - RequestInfo ri; - if (_requests.TryRemove(frameId, out ri)) + Action rec; + if (_fallbacks.TryRemove(identity, out rec)) { - ri.Fail(message); - _ri_pool.Free(ri); + try + { + rec(message); + } + catch (Exception ex) + { + Log.Error(ex, $"Fail invoke fallback for request '{identity}' with message '{message ?? string.Empty}'"); + } + rec = null; } } - public void Success(long frameId, byte[] data) + public void Success(long identity, byte[] data) { - RequestInfo ri; - if (_requests.TryRemove(frameId, out ri)) + Action rec; + if (_callbacks.TryRemove(identity, out rec)) { - ri.Success(data); - _ri_pool.Free(ri); + try + { + rec(data); + } + catch (Exception ex) + { + Log.Error(ex, $"Fail invoke callback for request '{identity}'. Response size '{data?.Length ?? 0}'"); + } + rec = null; } } - public void StartSend(long frameId) + public void StartSend(long identity) { - RequestInfo ri; - if (_requests.TryGetValue(frameId, out ri)) + if (_callbacks.ContainsKey(identity) + || _fallbacks.ContainsKey(identity)) { - ri.StartSend(); + _timeouts.TryAdd(identity, DateTime.UtcNow.Ticks); } } - public void Timeout(List frameIds) + public void Timeout(List identities) { - RequestInfo ri; - for (int i = 0; i < frameIds.Count; i++) + long t; + Action rec; + foreach (var id in identities) { - if (_requests.TryRemove(frameIds[i], out ri)) + if (_fallbacks.TryRemove(id, out rec)) { - _ri_pool.Free(ri); + try + { + rec("Timeout"); + } + catch (Exception ex) + { + Log.Error(ex, $"Fail invoke fallback for request '{id}' by timeout"); + } + rec = null; } + _timeouts.TryRemove(id, out t); } } @@ -62,10 +91,9 @@ namespace ZeroLevel.Network { var now_ticks = DateTime.UtcNow.Ticks; var to_remove = new List(); - foreach (var pair in _requests) + foreach (var pair in _timeouts) { - if (pair.Value.Sended == false) continue; - var diff = now_ticks - pair.Value.Timestamp; + var diff = now_ticks - pair.Value; if (diff > BaseSocket.MAX_REQUEST_TIME_TICKS) { to_remove.Add(pair.Key);