mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LookupServer: Cache successful DNS lookup responses for 1 minute
This reduces DNS traffic spam during web browsing. We can definitely do a lot better here, this is just a very low-hanging fruit.
This commit is contained in:
parent
005b416639
commit
3a4da5aa05
Notes:
sideshowbarker
2024-07-19 11:40:49 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/3a4da5aa057
1 changed files with 17 additions and 0 deletions
|
@ -16,6 +16,7 @@
|
|||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define T_A 1
|
||||
|
@ -27,6 +28,13 @@
|
|||
|
||||
#define C_IN 1
|
||||
|
||||
struct CachedLookup {
|
||||
time_t timestamp { 0 };
|
||||
unsigned short record_type { 0 };
|
||||
Vector<String> responses;
|
||||
};
|
||||
|
||||
static HashMap<String, CachedLookup> lookup_cache;
|
||||
static HashMap<String, String> dns_custom_hostnames;
|
||||
|
||||
static Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type);
|
||||
|
@ -207,6 +215,14 @@ static u16 get_next_id()
|
|||
|
||||
Vector<String> lookup(const String& hostname, bool& did_timeout, const String& DNS_IP, unsigned short record_type)
|
||||
{
|
||||
if (auto it = lookup_cache.find(hostname); it != 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;
|
||||
}
|
||||
lookup_cache.remove(it);
|
||||
}
|
||||
|
||||
DNSPacket request_header;
|
||||
request_header.set_id(get_next_id());
|
||||
request_header.set_is_query();
|
||||
|
@ -335,6 +351,7 @@ Vector<String> lookup(const String& hostname, bool& did_timeout, const String& D
|
|||
// FIXME: Parse some other record types perhaps?
|
||||
}
|
||||
|
||||
lookup_cache.set(hostname, { time(nullptr), record_type, addresses });
|
||||
return addresses;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue