aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/throttling.h
blob: 1e5db1e8fa1a8cdd2047fdf31d7ec63e2720b983 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once

#include <library/cpp/unified_agent_client/f_maybe.h>

#include <util/datetime/base.h>

namespace NUnifiedAgent {
    class TThrottler {
    public:
        explicit TThrottler(double rate, TDuration updatePeriod = TDuration::MilliSeconds(100));

        TThrottler(double rate, double burst);

        void Consume(double& tokens, TFMaybe<TDuration>& nextCheckDelay);

        bool TryConsume(double tokens);

        void ConsumeAndWait(double tokens);

    private:
        ui64 UpdateTokens();

    private:
        ui64 CyclesPerMillisecond;
        ui64 UpdatePeriod;
        double PeriodTokens;
        double AvailableTokens;
        ui64 ExpirationTime;
    };
}