aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/threaded_updater.h
blob: 783684ba3beb3b378abfb450617e6b525f14f0bf (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once

#include "async_updater.h"
#include "settings.h"

#include <library/cpp/tvmauth/client/logger.h>

#include <library/cpp/http/simple/http_client.h>

#include <util/datetime/base.h>
#include <util/generic/ptr.h>
#include <util/system/event.h>
#include <util/system/thread.h>

class TKeepAliveHttpClient;

namespace NTvmAuth::NInternal {
    class TClientCaningKnife;
}
namespace NTvmAuth {
    class TThreadedUpdaterBase: public TAsyncUpdaterBase {
    public:
        TThreadedUpdaterBase(TDuration workerAwakingPeriod,
                             TLoggerPtr logger,
                             const TString& url,
                             ui16 port,
                             TDuration socketTimeout,
                             TDuration connectTimeout);
        virtual ~TThreadedUpdaterBase();

    protected:
        void StartWorker();
        void StopWorker();

        virtual void Worker() {
        }

        TKeepAliveHttpClient& GetClient() const;

        void LogDebug(const TString& msg) const;
        void LogInfo(const TString& msg) const;
        void LogWarning(const TString& msg) const;
        void LogError(const TString& msg) const;

    protected:
        TDuration WorkerAwakingPeriod_;

        const TLoggerPtr Logger_;

    protected:
        const TString TvmUrl_;

    private:
        static void* WorkerWrap(void* arg);

        void StartTvmClientStopping() const override {
            Event_.Signal();
        }

        bool IsTvmClientStopped() const override {
            return IsStopped_;
        }

    private:
        mutable THolder<TKeepAliveHttpClient> HttpClient_;

        const ui32 TvmPort_;
        const TDuration TvmSocketTimeout_;
        const TDuration TvmConnectTimeout_;

        mutable TAutoEvent Event_;
        mutable TAutoEvent Started_;
        std::atomic_bool IsStopped_;
        THolder<TThread> Thread_;
    };
}