mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 22:07:36 +00:00
LookupServer: Cache DNS answers for TTL seconds
We now keep DNS answers around in a cache for TTL seconds after getting them the first time. The cache is capped at 256 responses for now. Suggested by @zecke in #10.
This commit is contained in:
parent
90a5907b44
commit
5e47508672
5 changed files with 71 additions and 17 deletions
|
@ -157,8 +157,16 @@ Vector<String> LookupServer::lookup(const String& hostname, bool& did_timeout, u
|
|||
{
|
||||
if (auto it = m_lookup_cache.find(hostname); it != m_lookup_cache.end()) {
|
||||
auto& cached_lookup = it->value;
|
||||
if (cached_lookup.record_type == record_type && cached_lookup.timestamp < (time(nullptr) + 60)) {
|
||||
return it->value.responses;
|
||||
if (cached_lookup.question.record_type() == record_type) {
|
||||
Vector<String> responses;
|
||||
for (auto& cached_answer : cached_lookup.answers) {
|
||||
dbg() << "Cache hit: " << hostname << " -> " << cached_answer.record_data() << ", expired: " << cached_answer.has_expired();
|
||||
if (!cached_answer.has_expired()) {
|
||||
responses.append(cached_answer.record_data());
|
||||
}
|
||||
}
|
||||
if (!responses.is_empty())
|
||||
return responses;
|
||||
}
|
||||
m_lookup_cache.remove(it);
|
||||
}
|
||||
|
@ -232,11 +240,13 @@ Vector<String> LookupServer::lookup(const String& hostname, bool& did_timeout, u
|
|||
return {};
|
||||
}
|
||||
|
||||
Vector<String> addresses;
|
||||
Vector<String> responses;
|
||||
for (auto& answer : response.answers()) {
|
||||
addresses.append(answer.record_data());
|
||||
responses.append(answer.record_data());
|
||||
}
|
||||
|
||||
m_lookup_cache.set(hostname, { time(nullptr), record_type, addresses });
|
||||
return addresses;
|
||||
if (m_lookup_cache.size() >= 256)
|
||||
m_lookup_cache.remove(m_lookup_cache.begin());
|
||||
m_lookup_cache.set(hostname, { request.questions()[0], response.answers() });
|
||||
return responses;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue