From e6b2615693feb6e00e79f63c0f9eb6288fd2a464 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sun, 20 Feb 2022 17:54:39 +0530 Subject: [PATCH] StatsManager: Updated DoMaintenance() to unload not required minute stats from cached hourly stats data for objects older than 1 hour to free memory. --- DnsServerCore/Dns/StatsManager.cs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/DnsServerCore/Dns/StatsManager.cs b/DnsServerCore/Dns/StatsManager.cs index 716adf47..b3b6ec95 100644 --- a/DnsServerCore/Dns/StatsManager.cs +++ b/DnsServerCore/Dns/StatsManager.cs @@ -1,6 +1,6 @@ /* Technitium DNS Server -Copyright (C) 2021 Shreyas Zare (shreyas@technitium.com) +Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -58,7 +58,7 @@ namespace DnsServerCore.Dns readonly Timer _maintenanceTimer; const int MAINTENANCE_TIMER_INITIAL_INTERVAL = 10000; - const int MAINTENANCE_TIMER_INTERVAL = 10000; + const int MAINTENANCE_TIMER_PERIODIC_INTERVAL = 10000; readonly BlockingCollection _queue = new BlockingCollection(); readonly Thread _consumerThread; @@ -99,7 +99,7 @@ namespace DnsServerCore.Dns if (log != null) log.Write(ex); } - }, null, MAINTENANCE_TIMER_INITIAL_INTERVAL, MAINTENANCE_TIMER_INTERVAL); + }, null, MAINTENANCE_TIMER_INITIAL_INTERVAL, MAINTENANCE_TIMER_PERIODIC_INTERVAL); //stats consumer thread _consumerThread = new Thread(delegate () @@ -350,6 +350,18 @@ namespace DnsServerCore.Dns _hourlyStatsCache.TryRemove(key, out _); } + //unload minute stats data from hourly stats cache for data older than last hour + { + DateTime lastHourThreshold = DateTime.UtcNow.AddHours(-1); + lastHourThreshold = new DateTime(lastHourThreshold.Year, lastHourThreshold.Month, lastHourThreshold.Day, lastHourThreshold.Hour, 0, 0, DateTimeKind.Utc); + + foreach (KeyValuePair item in _hourlyStatsCache) + { + if (item.Key < lastHourThreshold) + item.Value.UnloadMinuteStats(); + } + } + //remove old data from daily stats cache { DateTime threshold = DateTime.UtcNow.AddMonths(-12); @@ -1159,7 +1171,7 @@ namespace DnsServerCore.Dns #region variables readonly StatCounter _hourStat; //calculated value - readonly StatCounter[] _minuteStats = new StatCounter[60]; + StatCounter[] _minuteStats = new StatCounter[60]; #endregion @@ -1209,6 +1221,11 @@ namespace DnsServerCore.Dns _minuteStats[dateTime.Minute] = minuteStat; } + public void UnloadMinuteStats() + { + _minuteStats = null; + } + public void WriteTo(BinaryWriter bW) { bW.Write(Encoding.ASCII.GetBytes("HS")); //format