RequestServer: Pass the Request object to disk cache entry factories

This object will be needed in a future commit to store requests awaiting
other requests to finish. Doing this in a separate commit just to make
that commit less noisy.
This commit is contained in:
Timothy Flynn 2025-10-25 08:09:11 -04:00 committed by Andreas Kling
parent 5384f84550
commit 95d23d02f1
Notes: github-actions[bot] 2025-10-28 10:53:55 +00:00
4 changed files with 24 additions and 19 deletions

View file

@ -10,6 +10,7 @@
#include <LibURL/URL.h> #include <LibURL/URL.h>
#include <RequestServer/Cache/DiskCache.h> #include <RequestServer/Cache/DiskCache.h>
#include <RequestServer/Cache/Utilities.h> #include <RequestServer/Cache/Utilities.h>
#include <RequestServer/Request.h>
namespace RequestServer { namespace RequestServer {
@ -32,21 +33,21 @@ DiskCache::DiskCache(NonnullRefPtr<Database::Database> database, LexicalPath cac
{ {
} }
Optional<CacheEntryWriter&> DiskCache::create_entry(URL::URL const& url, StringView method, UnixDateTime request_time) Optional<CacheEntryWriter&> DiskCache::create_entry(Request& request)
{ {
if (!is_cacheable(method)) if (!is_cacheable(request.method()))
return {}; return {};
auto serialized_url = serialize_url_for_cache_storage(url); auto serialized_url = serialize_url_for_cache_storage(request.url());
auto cache_key = create_cache_key(serialized_url, method); auto cache_key = create_cache_key(serialized_url, request.method());
auto cache_entry = CacheEntryWriter::create(*this, m_index, cache_key, move(serialized_url), request_time); auto cache_entry = CacheEntryWriter::create(*this, m_index, cache_key, move(serialized_url), request.request_start_time());
if (cache_entry.is_error()) { if (cache_entry.is_error()) {
dbgln("\033[31;1mUnable to create cache entry for\033[0m {}: {}", url, cache_entry.error()); dbgln("\033[31;1mUnable to create cache entry for\033[0m {}: {}", request.url(), cache_entry.error());
return {}; return {};
} }
dbgln("\033[32;1mCreated disk cache entry for\033[0m {}", url); dbgln("\033[32;1mCreated disk cache entry for\033[0m {}", request.url());
auto address = reinterpret_cast<FlatPtr>(cache_entry.value().ptr()); auto address = reinterpret_cast<FlatPtr>(cache_entry.value().ptr());
m_open_cache_entries.set(address, cache_entry.release_value()); m_open_cache_entries.set(address, cache_entry.release_value());
@ -54,23 +55,23 @@ Optional<CacheEntryWriter&> DiskCache::create_entry(URL::URL const& url, StringV
return static_cast<CacheEntryWriter&>(**m_open_cache_entries.get(address)); return static_cast<CacheEntryWriter&>(**m_open_cache_entries.get(address));
} }
Optional<CacheEntryReader&> DiskCache::open_entry(URL::URL const& url, StringView method) Optional<CacheEntryReader&> DiskCache::open_entry(Request& request)
{ {
if (!is_cacheable(method)) if (!is_cacheable(request.method()))
return {}; return {};
auto serialized_url = serialize_url_for_cache_storage(url); auto serialized_url = serialize_url_for_cache_storage(request.url());
auto cache_key = create_cache_key(serialized_url, method); auto cache_key = create_cache_key(serialized_url, request.method());
auto index_entry = m_index.find_entry(cache_key); auto index_entry = m_index.find_entry(cache_key);
if (!index_entry.has_value()) { if (!index_entry.has_value()) {
dbgln("\033[35;1mNo disk cache entry for\033[0m {}", url); dbgln("\033[35;1mNo disk cache entry for\033[0m {}", request.url());
return {}; return {};
} }
auto cache_entry = CacheEntryReader::create(*this, m_index, cache_key, index_entry->data_size); auto cache_entry = CacheEntryReader::create(*this, m_index, cache_key, index_entry->data_size);
if (cache_entry.is_error()) { if (cache_entry.is_error()) {
dbgln("\033[31;1mUnable to open cache entry for\033[0m {}: {}", url, cache_entry.error()); dbgln("\033[31;1mUnable to open cache entry for\033[0m {}: {}", request.url(), cache_entry.error());
m_index.remove_entry(cache_key); m_index.remove_entry(cache_key);
return {}; return {};
} }
@ -79,12 +80,12 @@ Optional<CacheEntryReader&> DiskCache::open_entry(URL::URL const& url, StringVie
auto current_age = calculate_age(cache_entry.value()->headers(), index_entry->request_time, index_entry->response_time); auto current_age = calculate_age(cache_entry.value()->headers(), index_entry->request_time, index_entry->response_time);
if (!is_response_fresh(freshness_lifetime, current_age)) { if (!is_response_fresh(freshness_lifetime, current_age)) {
dbgln("\033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", url, freshness_lifetime.to_seconds(), current_age.to_seconds()); dbgln("\033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", request.url(), freshness_lifetime.to_seconds(), current_age.to_seconds());
cache_entry.value()->remove(); cache_entry.value()->remove();
return {}; return {};
} }
dbgln("\033[32;1mOpened disk cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size); dbgln("\033[32;1mOpened disk cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", request.url(), freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size);
auto address = reinterpret_cast<FlatPtr>(cache_entry.value().ptr()); auto address = reinterpret_cast<FlatPtr>(cache_entry.value().ptr());
m_open_cache_entries.set(address, cache_entry.release_value()); m_open_cache_entries.set(address, cache_entry.release_value());

View file

@ -23,8 +23,8 @@ class DiskCache {
public: public:
static ErrorOr<DiskCache> create(); static ErrorOr<DiskCache> create();
Optional<CacheEntryWriter&> create_entry(URL::URL const&, StringView method, UnixDateTime request_time); Optional<CacheEntryWriter&> create_entry(Request&);
Optional<CacheEntryReader&> open_entry(URL::URL const&, StringView method); Optional<CacheEntryReader&> open_entry(Request&);
void clear_cache(); void clear_cache();
LexicalPath const& cache_directory() { return m_cache_directory; } LexicalPath const& cache_directory() { return m_cache_directory; }

View file

@ -167,13 +167,13 @@ void Request::process()
void Request::handle_initial_state() void Request::handle_initial_state()
{ {
if (m_disk_cache.has_value()) { if (m_disk_cache.has_value()) {
m_cache_entry_reader = m_disk_cache->open_entry(m_url, m_method); m_cache_entry_reader = m_disk_cache->open_entry(*this);
if (m_cache_entry_reader.has_value()) { if (m_cache_entry_reader.has_value()) {
transition_to_state(State::ReadCache); transition_to_state(State::ReadCache);
return; return;
} }
m_cache_entry_writer = m_disk_cache->create_entry(m_url, m_method, m_request_start_time); m_cache_entry_writer = m_disk_cache->create_entry(*this);
} }
transition_to_state(State::DNSLookup); transition_to_state(State::DNSLookup);

View file

@ -50,6 +50,10 @@ public:
~Request(); ~Request();
URL::URL const& url() const { return m_url; }
ByteString const& method() const { return m_method; }
UnixDateTime request_start_time() const { return m_request_start_time; }
void notify_fetch_complete(Badge<ConnectionFromClient>, int result_code); void notify_fetch_complete(Badge<ConnectionFromClient>, int result_code);
private: private: