aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/api/dynamic_dst/tvm_client.h
blob: 67eeb2618a3bc5e936937e9897f57ada619b12df (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <library/cpp/tvmauth/client/misc/api/threaded_updater.h>

#include <library/cpp/threading/future/future.h>

#include <util/generic/map.h>
#include <util/thread/lfqueue.h>

#include <optional>

namespace NTvmAuth::NDynamicClient {
    enum class EDstStatus {
        Success,
        Fail,
    };

    struct TDstResponse {
        EDstStatus Status = EDstStatus::Fail;
        TString Error;

        bool operator==(const TDstResponse& o) const {
            return Status == o.Status && Error == o.Error;
        }
    };

    using TDsts = NTvmApi::TDstSet;
    using TAddResponse = TMap<NTvmApi::TClientSettings::TDst, TDstResponse>;

    class TTvmClient: public NTvmApi::TThreadedUpdater {
    public:
        static TIntrusivePtr<TTvmClient> Create(const NTvmApi::TClientSettings& settings, TLoggerPtr logger);
        virtual ~TTvmClient();

        NThreading::TFuture<TAddResponse> Add(TDsts&& dsts);
        std::optional<TString> GetOptionalServiceTicketFor(const TTvmId dst);

    protected: // for tests
        struct TTask {
            ui64 Id = 0;
            NThreading::TPromise<TAddResponse> Promise;
            TDsts Dsts;
        };

        using TBase = NTvmApi::TThreadedUpdater;

    protected: // for tests
        TTvmClient(const NTvmApi::TClientSettings& settings, TLoggerPtr logger);

        void Worker() override;
        void ProcessTasks();

        void SetResponseForTask(TTask& task, const TServiceTickets& cache);

    private:
        std::atomic<ui64> TaskIds_ = {0};
        TLockFreeQueue<TTask> TaskQueue_;
        TVector<TTask> Tasks_;
    };
}