aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/dns/thread.cpp
blob: a8441d7c90dfc990c136afb56c58fb5f97a17953 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "thread.h"

#include "magic.h"

#include <util/network/socket.h>
#include <util/thread/factory.h>
#include <util/thread/lfqueue.h>
#include <util/system/event.h>
#include <util/generic/vector.h>
#include <util/generic/singleton.h>

using namespace NDns;

namespace {
    class TThreadedResolver: public IThreadFactory::IThreadAble, public TNonCopyable {
        struct TResolveRequest {
            inline TResolveRequest(const TString& host, ui16 port) 
                : Host(host)
                , Port(port)
            {
            }

            inline TNetworkAddressPtr Wait() {
                E.Wait();

                if (!Error) {
                    if (!Result) {
                        ythrow TNetworkResolutionError(EAI_AGAIN) << TStringBuf(": resolver down");
                    }

                    return Result;
                }

                Error->Raise();

                ythrow TNetworkResolutionError(EAI_FAIL) << TStringBuf(": shit happen");
            }

            inline void Resolve() noexcept { 
                try {
                    Result = new TNetworkAddress(Host, Port);
                } catch (...) {
                    Error = SaveError();
                }

                Wake();
            }

            inline void Wake() noexcept { 
                E.Signal();
            }

            TString Host; 
            ui16 Port;
            TManualEvent E;
            TNetworkAddressPtr Result;
            IErrorRef Error;
        };

    public:
        inline TThreadedResolver()
            : E_(TSystemEvent::rAuto)
        {
            T_.push_back(SystemThreadFactory()->Run(this));
        }

        inline ~TThreadedResolver() override { 
            Schedule(nullptr);

            for (size_t i = 0; i < T_.size(); ++i) {
                T_[i]->Join();
            }

            {
                TResolveRequest* rr = nullptr;

                while (Q_.Dequeue(&rr)) {
                    if (rr) {
                        rr->Wake();
                    }
                }
            }
        }

        static inline TThreadedResolver* Instance() {
            return Singleton<TThreadedResolver>();
        }

        inline TNetworkAddressPtr Resolve(const TString& host, ui16 port) { 
            TResolveRequest rr(host, port);

            Schedule(&rr);

            return rr.Wait();
        }

    private:
        inline void Schedule(TResolveRequest* rr) {
            Q_.Enqueue(rr);
            E_.Signal();
        }

        void DoExecute() override {
            while (true) {
                TResolveRequest* rr = nullptr;

                while (!Q_.Dequeue(&rr)) {
                    E_.Wait();
                }

                if (rr) {
                    rr->Resolve();
                } else {
                    break;
                }
            }

            Schedule(nullptr);
        }

    private:
        TLockFreeQueue<TResolveRequest*> Q_;
        TSystemEvent E_;
        typedef TAutoPtr<IThreadFactory::IThread> IThreadRef;
        TVector<IThreadRef> T_; 
    };
}

namespace NDns {
    TNetworkAddressPtr ThreadedResolve(const TString& host, ui16 port) { 
        return TThreadedResolver::Instance()->Resolve(host, port);
    }
}