diff options
author | hor911 <hor911@ydb.tech> | 2023-02-09 12:40:11 +0300 |
---|---|---|
committer | hor911 <hor911@ydb.tech> | 2023-02-09 12:40:11 +0300 |
commit | 24689527cd888aa8a640ecb5077e656b3520d373 (patch) | |
tree | a613ff4cd9567b7113e8376a17f8b85897a42790 /library/cpp/unified_agent_client/throttling.cpp | |
parent | 8642d3642932f03663ba7d2d9670707c192207fd (diff) | |
download | ydb-24689527cd888aa8a640ecb5077e656b3520d373.tar.gz |
Log backend move
Diffstat (limited to 'library/cpp/unified_agent_client/throttling.cpp')
-rw-r--r-- | library/cpp/unified_agent_client/throttling.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/throttling.cpp b/library/cpp/unified_agent_client/throttling.cpp new file mode 100644 index 0000000000..271f7b0e7e --- /dev/null +++ b/library/cpp/unified_agent_client/throttling.cpp @@ -0,0 +1,67 @@ +#include "throttling.h" + +#include <util/datetime/cputimer.h> + +namespace NUnifiedAgent { + TThrottler::TThrottler(double rate, TDuration updatePeriod) + : CyclesPerMillisecond(GetCyclesPerMillisecond()) + , UpdatePeriod(updatePeriod.MilliSeconds() * CyclesPerMillisecond) + , PeriodTokens(updatePeriod.SecondsFloat() * rate) + , AvailableTokens(0) + , ExpirationTime(0) + { + } + + TThrottler::TThrottler(double rate, double burst) + : TThrottler(rate, TDuration::Seconds(burst / rate)) + { + } + + void TThrottler::Consume(double& tokens, TFMaybe<TDuration>& nextCheckDelay) { + const auto updateTime = UpdateTokens(); + + if (tokens <= AvailableTokens) { + AvailableTokens -= tokens; + tokens = 0.0; + nextCheckDelay = Nothing(); + } else { + tokens -= AvailableTokens; + AvailableTokens = 0.0; + nextCheckDelay = TDuration::MicroSeconds((ExpirationTime - updateTime) * 1000 / CyclesPerMillisecond + 1); + } + } + + bool TThrottler::TryConsume(double tokens) { + UpdateTokens(); + + if (tokens > AvailableTokens) { + return false; + } + AvailableTokens -= tokens; + return true; + } + + void TThrottler::ConsumeAndWait(double tokens) { + TFMaybe<TDuration> nextCheckDelay; + while (true) { + Consume(tokens, nextCheckDelay); + if (!nextCheckDelay.Defined()) { + return; + } + Sleep(*nextCheckDelay); + } + } + + ui64 TThrottler::UpdateTokens() { + const auto updateTime = GetCycleCount(); + if (updateTime >= ExpirationTime) { + if (ExpirationTime == 0) { + ExpirationTime = updateTime + UpdatePeriod; + } else { + ExpirationTime += ((updateTime - ExpirationTime) / UpdatePeriod + 1) * UpdatePeriod; + } + AvailableTokens = PeriodTokens; + } + return updateTime; + } +} |