Skip to content

Commit 2274a3d

Browse files
authored
Modify the Resolver class (#48)
1 parent e59ad2c commit 2274a3d

File tree

5 files changed

+75
-30
lines changed

5 files changed

+75
-30
lines changed

trantor/net/inner/AresResolver.cc

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ AresResolver::AresResolver(EventLoop* loop, size_t timeout)
5151
: _loop(loop), _ctx(NULL), _timerActive(false), _timeout(timeout)
5252
{
5353
assert(_loop);
54-
static char lookups[] = "b";
54+
// static char lookups[] = "b";
5555
struct ares_options options;
5656
int optmask = ARES_OPT_FLAGS;
5757
options.flags = ARES_FLAG_NOCHECKRESP;
@@ -62,8 +62,8 @@ AresResolver::AresResolver(EventLoop* loop, size_t timeout)
6262
options.sock_state_cb_data = this;
6363
optmask |= ARES_OPT_TIMEOUT;
6464
options.timeout = 2;
65-
optmask |= ARES_OPT_LOOKUPS;
66-
options.lookups = lookups;
65+
// optmask |= ARES_OPT_LOOKUPS;
66+
// options.lookups = lookups;
6767

6868
int status = ares_init_options(&_ctx, &options, optmask);
6969
if (status != ARES_SUCCESS)
@@ -84,23 +84,33 @@ void AresResolver::resolveInLoop(const std::string& hostname,
8484
const Callback& cb)
8585
{
8686
_loop->assertInLoopThread();
87-
auto iter = _dnsCache.find(hostname);
88-
if (iter != _dnsCache.end())
87+
bool cached = false;
88+
InetAddress inet;
8989
{
90-
auto& cachedAddr = iter->second;
91-
if (_timeout == 0 ||
92-
cachedAddr.second.after(_timeout) > trantor::Date::date())
90+
std::lock_guard<std::mutex> lock(globalMutex());
91+
auto iter = globalCache().find(hostname);
92+
if (iter != globalCache().end())
9393
{
94-
struct sockaddr_in addr;
95-
memset(&addr, 0, sizeof addr);
96-
addr.sin_family = AF_INET;
97-
addr.sin_port = 0;
98-
addr.sin_addr = cachedAddr.first;
99-
InetAddress inet(addr);
100-
cb(inet);
101-
return;
94+
auto& cachedAddr = iter->second;
95+
if (_timeout == 0 ||
96+
cachedAddr.second.after(_timeout) > trantor::Date::date())
97+
{
98+
struct sockaddr_in addr;
99+
memset(&addr, 0, sizeof addr);
100+
addr.sin_family = AF_INET;
101+
addr.sin_port = 0;
102+
addr.sin_addr = cachedAddr.first;
103+
inet = InetAddress(addr);
104+
cached = true;
105+
}
102106
}
103107
}
108+
if (cached)
109+
{
110+
cb(inet);
111+
return;
112+
}
113+
104114
QueryData* queryData = new QueryData(this, cb, hostname);
105115
ares_gethostbyname(_ctx,
106116
hostname.c_str(),
@@ -157,8 +167,11 @@ void AresResolver::onQueryResult(int status,
157167
addr.sin_addr = *reinterpret_cast<in_addr*>(result->h_addr);
158168
}
159169
InetAddress inet(addr);
160-
_dnsCache[hostname].first = addr.sin_addr;
161-
_dnsCache[hostname].second = trantor::Date::date();
170+
{
171+
std::lock_guard<std::mutex> lock(globalMutex());
172+
globalCache()[hostname].first = addr.sin_addr;
173+
globalCache()[hostname].second = trantor::Date::date();
174+
}
162175
callback(inet);
163176
}
164177

trantor/net/inner/AresResolver.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,21 @@ class AresResolver : public Resolver,
6262
bool _timerActive;
6363
typedef std::map<int, std::unique_ptr<trantor::Channel>> ChannelList;
6464
ChannelList _channels;
65-
std::unordered_map<std::string, std::pair<struct in_addr, trantor::Date>>
66-
_dnsCache;
65+
static std::unordered_map<std::string,
66+
std::pair<struct in_addr, trantor::Date>>&
67+
globalCache()
68+
{
69+
static std::unordered_map<std::string,
70+
std::pair<struct in_addr, trantor::Date>>
71+
_dnsCache;
72+
return _dnsCache;
73+
}
74+
static std::mutex& globalMutex()
75+
{
76+
static std::mutex _mutex;
77+
return _mutex;
78+
}
79+
6780
const size_t _timeout = 60;
6881

6982
void onRead(int sockfd);

trantor/net/inner/NormalResolver.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ void NormalResolver::resolve(const std::string &hostname,
1717
const Callback &callback)
1818
{
1919
{
20-
std::lock_guard<std::mutex> guard(_dnsMutex);
21-
auto iter = _dnsCache.find(hostname);
22-
if (iter != _dnsCache.end())
20+
std::lock_guard<std::mutex> guard(globalMutex());
21+
auto iter = globalCache().find(hostname);
22+
if (iter != globalCache().end())
2323
{
2424
auto &cachedAddr = iter->second;
2525
if (_timeout == 0 ||
@@ -92,9 +92,9 @@ void NormalResolver::resolve(const std::string &hostname,
9292
InetAddress inet(addr);
9393
callback(inet);
9494
{
95-
std::lock_guard<std::mutex> guard(thisPtr->_dnsMutex);
96-
thisPtr->_dnsCache[hostname].first = addr.sin_addr;
97-
thisPtr->_dnsCache[hostname].second = trantor::Date::date();
95+
std::lock_guard<std::mutex> guard(thisPtr->globalMutex());
96+
thisPtr->globalCache()[hostname].first = addr.sin_addr;
97+
thisPtr->globalCache()[hostname].second = trantor::Date::date();
9898
}
9999
return;
100100
}

trantor/net/inner/NormalResolver.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,20 @@ class NormalResolver : public Resolver,
3030
}
3131

3232
private:
33-
std::mutex _dnsMutex;
34-
std::unordered_map<std::string, std::pair<struct in_addr, trantor::Date>>
35-
_dnsCache;
33+
static std::unordered_map<std::string,
34+
std::pair<struct in_addr, trantor::Date>>&
35+
globalCache()
36+
{
37+
static std::unordered_map<std::string,
38+
std::pair<struct in_addr, trantor::Date>>
39+
_dnsCache;
40+
return _dnsCache;
41+
}
42+
static std::mutex& globalMutex()
43+
{
44+
static std::mutex _mutex;
45+
return _mutex;
46+
}
3647
trantor::SerialTaskQueue _taskQueue;
3748
const size_t _timeout = 60;
3849
std::vector<char> _resolveBuffer;

trantor/tests/DnsTest.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ void dns(const std::shared_ptr<trantor::Resolver> &resolver)
3131
std::cout << "bad address:" << addr.toIp() << " "
3232
<< interval / 1000 << "ms" << std::endl;
3333
});
34+
resolver->resolve("localhost",
35+
[now](const trantor::InetAddress &addr) {
36+
auto interval =
37+
trantor::Date::now().microSecondsSinceEpoch() -
38+
now.microSecondsSinceEpoch();
39+
std::cout << "localhost:" << addr.toIp() << " "
40+
<< interval / 1000 << "ms" << std::endl;
41+
});
3442
}
3543
int main()
3644
{
@@ -39,4 +47,4 @@ int main()
3947
dns(resolver);
4048
loop.runAfter(1.0, [resolver]() { dns(resolver); });
4149
loop.loop();
42-
}
50+
}

0 commit comments

Comments
 (0)