aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/dns/cache.cpp
blob: fe2bf56496eb85eb8674de5321f4104ad08fbf69 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "cache.h"

#include "thread.h"

#include <util/system/tls.h>
#include <util/system/info.h>
#include <util/system/rwlock.h>
#include <util/thread/singleton.h>
#include <util/generic/singleton.h>
#include <util/generic/hash.h>

using namespace NDns;

namespace {
    struct TResolveTask {
        enum EMethod {
            Normal,
            Threaded
        };

        inline TResolveTask(const TResolveInfo& info, EMethod method)
            : Info(info)
            , Method(method)
        {
        }

        const TResolveInfo& Info;
        const EMethod Method;
    };

    class IDns {
    public:
        virtual ~IDns() = default;
        virtual const TResolvedHost* Resolve(const TResolveTask&) = 0;
    };

    typedef TAtomicSharedPtr<TResolvedHost> TResolvedHostPtr;

    struct THashResolveInfo {
        inline size_t operator()(const TResolveInfo& ri) const {
            return ComputeHash(ri.Host) ^ ri.Port;
        }
    };

    struct TCompareResolveInfo {
        inline bool operator()(const NDns::TResolveInfo& x, const NDns::TResolveInfo& y) const {
            return x.Host == y.Host && x.Port == y.Port;
        }
    };

    class TGlobalCachedDns: public IDns, public TNonCopyable {
    public:
        const TResolvedHost* Resolve(const TResolveTask& rt) override {
            //2. search host in cache
            {
                TReadGuard guard(L_);

                TCache::const_iterator it = C_.find(rt.Info);

                if (it != C_.end()) {
                    return it->second.Get();
                }
            }

            TResolvedHostPtr res = ResolveA(rt);

            //update cache
            {
                TWriteGuard guard(L_);

                std::pair<TCache::iterator, bool> updateResult = C_.insert(std::make_pair(TResolveInfo(res->Host, rt.Info.Port), res));
                TResolvedHost* rh = updateResult.first->second.Get();

                if (updateResult.second) {
                    //fresh resolved host, set cache record id for it
                    rh->Id = C_.size() - 1;
                }

                return rh;
            }
        }

        void AddAlias(const TString& host, const TString& alias) noexcept {
            TWriteGuard guard(LA_);

            A_[host] = alias;
        }

        static inline TGlobalCachedDns* Instance() {
            return SingletonWithPriority<TGlobalCachedDns, 65530>();
        }

    private:
        inline TResolvedHostPtr ResolveA(const TResolveTask& rt) {
            TString originalHost(rt.Info.Host);
            TString host(originalHost);

            //3. replace host to alias, if exist
            if (A_.size()) {
                TReadGuard guard(LA_);
                TStringBuf names[] = {"*", host};

                for (const auto& name : names) {
                    TAliases::const_iterator it = A_.find(name);

                    if (it != A_.end()) {
                        host = it->second;
                    }
                }
            }

            if (host.length() > 2 && host[0] == '[') {
                TString unbracedIpV6(host.data() + 1, host.size() - 2);
                host.swap(unbracedIpV6);
            }

            TAutoPtr<TNetworkAddress> na;

            //4. getaddrinfo (direct or in separate thread)
            if (rt.Method == TResolveTask::Normal) {
                na.Reset(new TNetworkAddress(host, rt.Info.Port));
            } else if (rt.Method == TResolveTask::Threaded) {
                na = ThreadedResolve(host, rt.Info.Port);
            } else {
                Y_ASSERT(0); 
                throw yexception() << TStringBuf("invalid resolve method");
            }

            return new TResolvedHost(originalHost, *na);
        }

        typedef THashMap<TResolveInfo, TResolvedHostPtr, THashResolveInfo, TCompareResolveInfo> TCache;
        TCache C_;
        TRWMutex L_;
        typedef THashMap<TString, TString> TAliases;
        TAliases A_;
        TRWMutex LA_;
    };

    class TCachedDns: public IDns {
    public:
        inline TCachedDns(IDns* slave)
            : S_(slave)
        {
        }

        const TResolvedHost* Resolve(const TResolveTask& rt) override {
            //1. search in local thread cache
            {
                TCache::const_iterator it = C_.find(rt.Info);

                if (it != C_.end()) {
                    return it->second;
                }
            }

            const TResolvedHost* res = S_->Resolve(rt);

            C_[TResolveInfo(res->Host, rt.Info.Port)] = res;

            return res;
        }

    private:
        typedef THashMap<TResolveInfo, const TResolvedHost*, THashResolveInfo, TCompareResolveInfo> TCache;
        TCache C_;
        IDns* S_;
    };

    struct TThreadedDns: public TCachedDns {
        inline TThreadedDns()
            : TCachedDns(TGlobalCachedDns::Instance())
        {
        }
    };

    inline IDns* ThrDns() {
        return FastTlsSingleton<TThreadedDns>();
    }
}

namespace NDns {
    const TResolvedHost* CachedResolve(const TResolveInfo& ri) {
        TResolveTask rt(ri, TResolveTask::Normal);

        return ThrDns()->Resolve(rt);
    }

    const TResolvedHost* CachedThrResolve(const TResolveInfo& ri) {
        TResolveTask rt(ri, TResolveTask::Threaded);

        return ThrDns()->Resolve(rt);
    }

    void AddHostAlias(const TString& host, const TString& alias) {
        TGlobalCachedDns::Instance()->AddAlias(host, alias);
    }
}