通過LRU實現通用高效的超時連線探測
編寫網路通訊都要面對一個問題,就是要把很久不存活的死連線清除,如果不這樣做那死連線最終會佔用大量記憶體影響服務運作!在實現過程中一般都會使用ping,pong原理,通過ping,pong來更新連線的時效性,最後通過掃描連線列表來清除掉。雖然這種做法比較簡單,但很難抽取出通用性的封裝,掃描整個列表複雜度也比較高。以下講解如何通過LRU演算法實現一個通用高效的探測超時連線功能類。
什麼是LRU
在這裡還是要大概介紹一下LRU,LRU演算法的設計原則是:如果一個數據在最近一段時間沒有被訪問到,那麼在將來它被訪問的可能性也很小.也就是說,當限定的空間已存滿資料時,應當把最久沒有被訪問到的資料淘汰.當然在這裡並不需要使用到自動淘汰機制,只需要把未位到達超時的連線清除即可。
在C#中如何實現LRU
C#並不存在這樣的資料結構,不過有一個結構很適合實現LRU,這個結構就是LinkedList
雙向連結串列,通過以下結構圖就容易理解通過LinkedList
實現LRU
通過LinkedList
的功能我們可以把活越項先移出來,然後再把項移到頭部。在這裡需要注意LinkedList
的Remove
方法,它有兩個過載版本,兩個版本的複雜度不一樣。一個是O(n)一個是O(1)所以使用上一定要注意,否則在資料多的情況下效率差別巨大(這些細節都可以通過原始碼來檢視)!
程式碼實現
前面已經大概講述的原理,接下來要做的就是程式碼實現了。第一步需要制訂一個基礎可控測物件規則介面,這樣就可以讓現有的已經實現的功能實現它並可得到相關功能的支援。
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; } }
程式碼並不複雜,相信不用過多解釋也能看懂相關操作原理。
測試
既然功能已經實現,接下來就要對程式碼進行測試看執行效果。測試程式碼比較簡單首先開啟一個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(); } }
執行效果: