Skip to content

Truly independent web browser

LibWeb+LibHTTP: Clear the HTTP memory cache once it has stored a lot

Andreas Klingb7b5043d1e3b
diff --git a/Libraries/LibHTTP/Cache/MemoryCache.cpp b/Libraries/LibHTTP/Cache/MemoryCache.cpp
index addd5d4b86a..2879992af89 100644
--- a/Libraries/LibHTTP/Cache/MemoryCache.cpp
+++ b/Libraries/LibHTTP/Cache/MemoryCache.cpp
@@ -143,15 +143,15 @@ void MemoryCache::create_entry(URL::URL const& url, StringView method, HeaderLis
// FIXME: It would be nicer if create_entry just returned the cache and vary keys. But the call sites of create_entry and
// finalize_entry are pretty far apart, so passing that information along is rather awkward in Fetch.
-void MemoryCache::finalize_entry(URL::URL const& url, StringView method, HeaderList const& request_headers, u32 status_code, HeaderList const& received_response_headers, Core::ImmutableBytes response_body)
+bool MemoryCache::finalize_entry(URL::URL const& url, StringView method, HeaderList const& request_headers, u32 status_code, HeaderList const& received_response_headers, Core::ImmutableBytes response_body)
{
auto response_headers_without_connection_specific_fields = remove_connection_specific_fields(received_response_headers);
auto const& response_headers = *response_headers_without_connection_specific_fields;
if (!is_cacheable(method, request_headers))
- return;
+ return false;
if (!is_cacheable(status_code, response_headers))
- return;
+ return false;
auto serialized_url = serialize_url_for_cache_storage(url);
auto cache_key = create_cache_key(serialized_url, method);
@@ -159,7 +159,7 @@ void MemoryCache::finalize_entry(URL::URL const& url, StringView method, HeaderL
auto cache_entry = take_pending_entry(cache_key, vary_key);
if (!cache_entry.has_value())
- return;
+ return false;
dbgln_if(HTTP_MEMORY_CACHE_DEBUG, "\033[37m[memory]\033[0m \033[34;1mFinished caching\033[0m {} ({} bytes)", url, response_body.size());
cache_entry->response_body = move(response_body);
@@ -167,6 +167,7 @@ void MemoryCache::finalize_entry(URL::URL const& url, StringView method, HeaderL
auto& complete_entries = m_complete_entries.ensure(cache_key);
complete_entries.remove_all_matching([&](auto const& entry) { return entry.vary_key == cache_entry->vary_key; });
complete_entries.append(cache_entry.release_value());
+ return true;
}
void MemoryCache::abandon_entry(URL::URL const& url, StringView method, HeaderList const& request_headers, HeaderList const& received_response_headers)
diff --git a/Libraries/LibHTTP/Cache/MemoryCache.h b/Libraries/LibHTTP/Cache/MemoryCache.h
index ebc2306b6be..6fe3e7fcd3a 100644
--- a/Libraries/LibHTTP/Cache/MemoryCache.h
+++ b/Libraries/LibHTTP/Cache/MemoryCache.h
@@ -37,13 +37,18 @@ public:
Optional<Entry const&> open_entry(URL::URL const&, StringView method, HeaderList const& request_headers, CacheMode);
void create_entry(URL::URL const&, StringView method, HeaderList const& request_headers, UnixDateTime request_time, u32 status_code, ByteString reason_phrase, HeaderList const& response_headers);
- void finalize_entry(URL::URL const&, StringView method, HeaderList const& request_headers, u32 status_code, HeaderList const& response_headers, Core::ImmutableBytes response_body);
+ // Returns whether the response was stored.
+ bool finalize_entry(URL::URL const&, StringView method, HeaderList const& request_headers, u32 status_code, HeaderList const& response_headers, Core::ImmutableBytes response_body);
// Drops the oldest pending entry with the request's cache and vary keys, for a response whose body will not be
// finalized.
void abandon_entry(URL::URL const&, StringView method, HeaderList const& request_headers, HeaderList const& response_headers);
void invalidate(URL::URL const&);
+ // Removes the complete entries, and keeps the pending ones for the responses that are still arriving.
+ void clear_complete_entries() { m_complete_entries.clear(); }
+ bool has_pending_entries() const { return !m_pending_entries.is_empty(); }
+
private:
Optional<Entry> take_pending_entry(u64 cache_key, Optional<u64> vary_key);
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 0af9249038e..d1297dcdb37 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -209,10 +209,37 @@ public:
void clear_cache()
{
m_cache.clear();
+ m_responses_stored_since_clear = 0;
+ m_bytes_in_memory_stored_since_clear = 0;
+ }
+
+ // Drops the stored responses once there may be more than a limit, as the cache otherwise keeps every response for
+ // a URL that is not fetched again for as long as the agent lives. A response body that is a mapped file costs a
+ // mapping, and others cost their size in memory.
+ void did_store_response(Core::ImmutableBytes const& body)
+ {
+ ++m_responses_stored_since_clear;
+ if (!body.is_file_backed() && !body.is_readonly_mapped())
+ m_bytes_in_memory_stored_since_clear += body.size();
+ if (m_responses_stored_since_clear <= stored_response_limit && m_bytes_in_memory_stored_since_clear <= stored_bytes_in_memory_limit)
+ return;
+
+ // NB: A fetch that is still running holds its partition and has a pending entry in it, which must stay the
+ // partition for its key.
+ for (auto& it : m_cache)
+ it.value->clear_complete_entries();
+ m_cache.remove_all_matching([](auto const&, auto const& cache) { return !cache->has_pending_entries() && cache->ref_count() == 1; });
+ m_responses_stored_since_clear = 0;
+ m_bytes_in_memory_stored_since_clear = 0;
}
private:
+ static constexpr size_t stored_response_limit = 1024;
+ static constexpr size_t stored_bytes_in_memory_limit = 32 * MiB;
+
HashMap<Utf16String, NonnullRefPtr<HTTP::MemoryCache>> m_cache;
+ size_t m_responses_stored_since_clear { 0 };
+ size_t m_bytes_in_memory_stored_since_clear { 0 };
};
Optional<Utf16String> http_cache_partition(Infrastructure::Request const& request)
@@ -3556,6 +3583,11 @@ bool http_memory_cache_enabled()
return g_http_memory_cache_enabled;
}
+void did_store_response_in_http_memory_cache(Core::ImmutableBytes const& body)
+{
+ HTTPCache::the().did_store_response(body);
+}
+
void clear_http_memory_cache()
{
HTTPCache::the().clear_cache();
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.h b/Libraries/LibWeb/Fetch/Fetching/Fetching.h
index e6110f7ae0d..689a1511f83 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.h
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.h
@@ -73,6 +73,7 @@ void append_user_agent_client_hints_for_request(Infrastructure::Request&);
WEB_API void set_http_memory_cache_enabled(bool enabled);
WEB_API bool http_memory_cache_enabled();
WEB_API void clear_http_memory_cache();
+void did_store_response_in_http_memory_cache(Core::ImmutableBytes const&);
WEB_API Optional<Utf16String> http_cache_partition(Infrastructure::Request const&);
WEB_API void set_disk_cache_enabled_for_navigations_for_testing(bool enabled);
WEB_API bool disk_cache_enabled_for_navigations_for_testing();
diff --git a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp
index 4edb3693326..5085dd52345 100644
--- a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp
@@ -7,6 +7,7 @@
*/
#include <LibHTTP/Cache/MemoryCache.h>
+#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Fetching/NetworkBodyPump.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/FetchParams.h>
@@ -151,7 +152,8 @@ void NetworkBodyPump::finalize_cache_entry()
auto response_body = m_cache_body.has_value()
? m_cache_body.release_value()
: Core::ImmutableBytes::adopt(move(m_cache_buffer));
- m_http_cache->finalize_entry(request->current_url(), request->method(), request->header_list(), m_response->status(), m_response->header_list(), move(response_body));
+ if (m_http_cache->finalize_entry(request->current_url(), request->method(), request->header_list(), m_response->status(), m_response->header_list(), response_body))
+ did_store_response_in_http_memory_cache(response_body);
}
m_http_cache.clear();