C#中通过LRU实现通用高效的超时连接探测

2020-01-05 09:59:13于海丽


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

接口定义了两个属性,一个是最近活越时间,另一个就是LinkedListNode<IDetector>这个属性比交关键,通过LinkedListNode<IDetector>可以让LinkedList在Remove时复杂度为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;
    }
  }