throttling.h 687 B

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. #include <library/cpp/unified_agent_client/f_maybe.h>
  3. #include <util/datetime/base.h>
  4. namespace NUnifiedAgent {
  5. class TThrottler {
  6. public:
  7. explicit TThrottler(double rate, TDuration updatePeriod = TDuration::MilliSeconds(100));
  8. TThrottler(double rate, double burst);
  9. void Consume(double& tokens, TFMaybe<TDuration>& nextCheckDelay);
  10. bool TryConsume(double tokens);
  11. void ConsumeAndWait(double tokens);
  12. private:
  13. ui64 UpdateTokens();
  14. private:
  15. ui64 CyclesPerMillisecond;
  16. ui64 UpdatePeriod;
  17. double PeriodTokens;
  18. double AvailableTokens;
  19. ui64 ExpirationTime;
  20. };
  21. }