1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:27:35 +00:00

RequestServer+LibHTTP+LibGemini: Cache connections to the same host

This makes connections (particularly TLS-based ones) do the handshaking
stuff only once.
Currently the cache is configured to keep at most two connections evenly
balanced in queue size, and with a grace period of 10s after the last
queued job has finished (after which the connection will be dropped).
This commit is contained in:
Ali Mohammad Pur 2021-09-18 03:48:22 +04:30 committed by Ali Mohammad Pur
parent c5d7eb8618
commit 65f7e45a75
22 changed files with 295 additions and 51 deletions

View file

@ -15,6 +15,7 @@
#include <AK/Types.h>
#include <LibHTTP/HttpRequest.h>
#include <RequestServer/ClientConnection.h>
#include <RequestServer/ConnectionCache.h>
#include <RequestServer/Request.h>
namespace RequestServer::Detail {
@ -29,6 +30,9 @@ void init(TSelf* self, TJob job)
};
job->on_finish = [self](bool success) {
Core::deferred_invoke([url = self->job().url(), socket = self->job().socket()] {
ConnectionCache::request_did_finish(url, socket);
});
if (auto* response = self->job().response()) {
self->set_status_code(response->code());
self->set_response_headers(response->headers());
@ -80,7 +84,12 @@ OwnPtr<Request> start_request(TBadgedProtocol&& protocol, ClientConnection& clie
auto job = TJob::construct(request, *output_stream);
auto protocol_request = TRequest::create_with_job(forward<TBadgedProtocol>(protocol), client, (TJob&)*job, move(output_stream));
protocol_request->set_request_fd(pipe_result.value().read_fd);
job->start();
if constexpr (IsSame<typename TBadgedProtocol::Type, HttpsProtocol>)
ConnectionCache::get_or_create_connection(ConnectionCache::g_tls_connection_cache, url, *job);
else
ConnectionCache::get_or_create_connection(ConnectionCache::g_tcp_connection_cache, url, *job);
return protocol_request;
}