From 415979c61e6f48c0c4f78f18ac2f0248c5a70b67 Mon Sep 17 00:00:00 2001 From: jetsparrow Date: Sat, 16 Oct 2021 22:09:42 +0300 Subject: [PATCH] LeakyBucket DateTime fixes - Remove use of local DateTime.Now - Always take UtcNow only once at method entry --- JetHerald/LeakyBucket.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/JetHerald/LeakyBucket.cs b/JetHerald/LeakyBucket.cs index f213c66..58cd14d 100644 --- a/JetHerald/LeakyBucket.cs +++ b/JetHerald/LeakyBucket.cs @@ -17,8 +17,9 @@ namespace JetHerald public bool IsTimedOut(uint key) { - var debtLimit = DateTime.Now.AddSeconds(config.DebtLimitSeconds); - var time = expiryDates.GetValueOrDefault(key, DateTime.Now); + var now = DateTime.UtcNow; + var debtLimit = now.AddSeconds(config.DebtLimitSeconds); + var time = expiryDates.GetValueOrDefault(key, now); Console.WriteLine(time); return time > debtLimit; } @@ -26,11 +27,12 @@ namespace JetHerald public void ApplyCost(uint key, int cost) { expiryDates.AddOrUpdate(key, - key => DateTime.Now.AddSeconds(cost), + key => DateTime.UtcNow.AddSeconds(cost), (key, oldDebt) => { - if (oldDebt < DateTime.Now) - return DateTime.Now.AddSeconds(cost); + var now = DateTime.UtcNow; + if (oldDebt < now) + return now.AddSeconds(cost); return oldDebt.AddSeconds(cost); });