zoukankan      html  css  js  c++  java
  • 通过LRU实现通用高效的超时连接探测

    编写网络通讯都要面对一个问题,就是要把很久不存活的死连接清除,如果不这样做那死连接最终会占用大量内存影响服务运作!在实现过程中一般都会使用ping,pong原理,通过ping,pong来更新连接的时效性,最后通过扫描连接列表来清除掉。虽然这种做法比较简单,但很难抽取出通用性的封装,扫描整个列表复杂度也比较高。以下讲解如何通过LRU算法实现一个通用高效的探测超时连接功能类。

    什么是LRU

    在这里还是要大概介绍一下LRU,LRU算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定的空间已存满数据时,应当把最久没有被访问到的数据淘汰.当然在这里并不需要使用到自动淘汰机制,只需要把未位到达超时的连接清除即可。

    在C#中如何实现LRU

    C#并不存在这样的数据结构,不过有一个结构很适合实现LRU,这个结构就是LinkedList双向链表,通过以下结构图就容易理解通过LinkedList实现LRU

    通过LinkedList的功能我们可以把活越项先移出来,然后再把项移到头部。在这里需要注意LinkedListRemove方法,它有两个重载版本,两个版本的复杂度不一样。一个是O(n)一个是O(1)所以使用上一定要注意,否则在数据多的情况下效率差别巨大(这些细节都可以通过源代码来查看)!

    代码实现

    前面已经大概讲述的原理,接下来要做的就是代码实现了。第一步需要制订一个基础可控测对象规则接口,这样就可以让现有的已经实现的功能实现它并可得到相关功能的支持。

        public interface IDetector
        {
            double ActiveTime
            { get; set; }
            LinkedListNode<IDetector> DetectorNode
            {
                get;
                set;
            }
        }

    接口定义了两个属性,一个是最近活越时间,另一个就是LinkedListNode<IDetector>这个属性比交关键,通过LinkedListNode<IDetector>可以让LinkedListRemove时复杂度为O(1).接下来就要针对基于LRU算法处理超时制定一个应用规则

        public interface ILRUDetector
        {
            void Update(IDetector item);
    
            void Detection(int timeout);
    
            double GetTime();
    
            Action<IList<IDetector>> Timeout { get; set; }
        }

    规则也是比较简单,Update用于更新跟踪对象,一般在处理接受ping或pong包后进行调用;Detection方法是探测超出指定时间的对象,时间当位是毫秒,如果存在有超时的对象则触发Timeout事件;GetTime是获取探测器已经运行的时间单位毫秒!规则定好了那接着要做的事实就是要实现它:

        class LRUDetector : ILRUDetector, IDisposable
        {
            public LRUDetector()
            {
                mTimeWatch = new System.Diagnostics.Stopwatch();
                mTimeWatch.Restart();
            }
            private Buffers.XSpinLock xSpinLock = new Buffers.XSpinLock();
            private System.Diagnostics.Stopwatch mTimeWatch;
            private LinkedList<IDetector> mItems = new LinkedList<IDetector>();
            public Action<IList<IDetector>> Timeout
            {
                get; set;
            }
            public void Detection(int timeout)
            {
                double time = GetTime();
                List<IDetector> result = new List<IDetector>();
                using (xSpinLock.Enter())
                {
                    LinkedListNode<IDetector> last = mItems.Last;
                    while (last != null && (time - last.Value.ActiveTime) > timeout)
                    {
                        mItems.Remove(last);
                        result.Add(last.Value);
                        last.Value.DetectorNode = null;
                        last = mItems.Last;
                    }
                }
                if (Timeout != null && result.Count > 0)
                    Timeout(result);
            }
            public void Update(IDetector item)
            {
                using (xSpinLock.Enter())
                {
                    if (item.DetectorNode == null)
                        item.DetectorNode = new LinkedListNode<IDetector>(item);
                    item.ActiveTime = GetTime();
                    if (item.DetectorNode.List == mItems)
                        mItems.Remove(item.DetectorNode);
                    mItems.AddFirst(item);
                }
            }
            public void Dispose()
            {
                mItems.Clear();
            }
            public double GetTime()
            {
                return mTimeWatch.Elapsed.TotalMilliseconds;
            }
        }

    代码并不复杂,相信不用过多解释也能看懂相关操作原理。

    测试

    既然功能已经实现,接下来就要对代码进行测试看运行效果。测试代码比较简单首先开启一个Timer定时执行Detection,另外开一个线程去调用Update方法

        class Program
        {
            public class TestDetector : IDetector
            {
                public double ActiveTime { get; set; }
                public string Name { get; set; }
                public LinkedListNode<IDetector> DetectorNode { get; set; }
            }
            static void Main(string[] args)
            {
                LRUDetector lRUDetector = new LRUDetector();
                lRUDetector.Timeout = (items) =>
                {
                    foreach (TestDetector item in items)
                        Console.WriteLine($"{(item.Name)} timeout {lRUDetector.GetTime() - item.ActiveTime}ms");
                };
                System.Threading.Timer timer = null;
                timer = new System.Threading.Timer(o =>
                {
                    timer.Change(-1, -1);
                    lRUDetector.Detection(5000);
                    timer.Change(5000, 5000);
                }, null, 5000, 5000);
                System.Threading.ThreadPool.QueueUserWorkItem(o =>
                {
                    int i = 0;
                    while (true)
                    {
                        System.Threading.Thread.Sleep(500);
                        i++;
                        TestDetector testDetector = new TestDetector();
                        testDetector.Name = "my name is " + i;
                        lRUDetector.Update(testDetector);
                    }
                });
                Console.Read();
            }
        }

    运行效果:

  • 相关阅读:
    Java实现 LeetCode 27 移除元素
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 24 两两交换链表中的节点
    Java实现 LeetCode 24 两两交换链表中的节点
    Java实现 LeetCode 24 两两交换链表中的节点
  • 原文地址:https://www.cnblogs.com/smark/p/9950802.html
Copyright © 2011-2022 走看看