1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 09:44:58 +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

@ -82,6 +82,7 @@ public:
bool can_read_line() const;
bool can_read() const;
bool can_read_only_from_buffer() const { return !m_buffered_data.is_empty() && !can_read_from_fd(); }
bool seek(i64, SeekMode = SeekMode::SetPosition, off_t* = nullptr);

View file

@ -19,7 +19,7 @@ NetworkJob::~NetworkJob()
{
}
void NetworkJob::start()
void NetworkJob::start(NonnullRefPtr<Core::Socket>)
{
}

View file

@ -35,7 +35,7 @@ public:
NetworkResponse* response() { return m_response.ptr(); }
const NetworkResponse* response() const { return m_response.ptr(); }
virtual void start() = 0;
virtual void start(NonnullRefPtr<Core::Socket>) = 0;
virtual void shutdown() = 0;
void cancel()

View file

@ -14,15 +14,11 @@
namespace Gemini {
void GeminiJob::start()
void GeminiJob::start(NonnullRefPtr<Core::Socket> socket)
{
VERIFY(!m_socket);
m_socket = TLS::TLSv12::construct(this);
m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
m_socket->on_tls_connected = [this] {
dbgln_if(GEMINIJOB_DEBUG, "GeminiJob: on_connected callback");
on_socket_connected();
};
VERIFY(is<TLS::TLSv12>(*socket));
m_socket = static_ptr_cast<TLS::TLSv12>(socket);
m_socket->on_tls_error = [this](TLS::AlertDescription error) {
if (error == TLS::AlertDescription::HandshakeFailure) {
deferred_invoke([this] {
@ -45,12 +41,21 @@ void GeminiJob::start()
if (on_certificate_requested)
on_certificate_requested(*this);
};
if (m_socket->is_established()) {
deferred_invoke([this] { on_socket_connected(); });
} else {
m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
m_socket->on_tls_connected = [this] {
on_socket_connected();
};
bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port_or_default());
if (!success) {
deferred_invoke([this] {
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
});
}
}
}
void GeminiJob::shutdown()
@ -59,7 +64,6 @@ void GeminiJob::shutdown()
return;
m_socket->on_tls_ready_to_read = nullptr;
m_socket->on_tls_connected = nullptr;
remove_child(*m_socket);
m_socket = nullptr;
}

View file

@ -27,10 +27,13 @@ public:
{
}
virtual void start() override;
virtual void start(NonnullRefPtr<Core::Socket>) override;
virtual void shutdown() override;
void set_certificate(String certificate, String key);
Core::Socket const* socket() const { return m_socket; }
URL url() const { return m_request.url(); }
Function<void(GeminiJob&)> on_certificate_requested;
protected:

View file

@ -19,7 +19,7 @@ public:
explicit Job(const GeminiRequest&, OutputStream&);
virtual ~Job() override;
virtual void start() override = 0;
virtual void start(NonnullRefPtr<Core::Socket>) override = 0;
virtual void shutdown() override = 0;
GeminiResponse* response() { return static_cast<GeminiResponse*>(Core::NetworkJob::response()); }

View file

@ -12,26 +12,35 @@
#include <unistd.h>
namespace HTTP {
void HttpJob::start()
void HttpJob::start(NonnullRefPtr<Core::Socket> socket)
{
VERIFY(!m_socket);
m_socket = Core::TCPSocket::construct(this);
m_socket->on_connected = [this] {
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
on_socket_connected();
};
m_socket = move(socket);
m_socket->on_error = [this] {
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_error callback");
deferred_invoke([this] {
did_fail(Core::NetworkJob::Error::ConnectionFailed);
});
};
if (m_socket->is_connected()) {
dbgln("Reusing previous connection for {}", url());
deferred_invoke([this] {
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
on_socket_connected();
});
} else {
dbgln("Creating new connection for {}", url());
m_socket->on_connected = [this] {
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
on_socket_connected();
};
bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default());
if (!success) {
deferred_invoke([this] {
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
});
}
};
}
void HttpJob::shutdown()
@ -40,13 +49,24 @@ void HttpJob::shutdown()
return;
m_socket->on_ready_to_read = nullptr;
m_socket->on_connected = nullptr;
remove_child(*m_socket);
m_socket = nullptr;
}
void HttpJob::register_on_ready_to_read(Function<void()> callback)
{
m_socket->on_ready_to_read = move(callback);
m_socket->on_ready_to_read = [callback = move(callback), this] {
callback();
// As IODevice so graciously buffers everything, there's a possible
// scenario where it buffers the entire response, and we get stuck waiting
// for select() in the notifier (which will never return).
// So handle this case by exhausting the buffer here.
if (m_socket->can_read_only_from_buffer() && m_state != State::Finished && !has_error()) {
deferred_invoke([this] {
if (m_socket && m_socket->on_ready_to_read)
m_socket->on_ready_to_read();
});
}
};
}
void HttpJob::register_on_ready_to_write(Function<void()> callback)

View file

@ -27,9 +27,12 @@ public:
{
}
virtual void start() override;
virtual void start(NonnullRefPtr<Core::Socket>) override;
virtual void shutdown() override;
Core::Socket const* socket() const { return m_socket; }
URL url() const { return m_request.url(); }
protected:
virtual bool should_fail_on_empty_payload() const override { return false; }
virtual void register_on_ready_to_read(Function<void()>) override;

View file

@ -55,7 +55,6 @@ ByteBuffer HttpRequest::to_raw_request() const
builder.append(header.value);
builder.append("\r\n");
}
builder.append("Connection: close\r\n");
if (!m_body.is_empty()) {
builder.appendff("Content-Length: {}\r\n\r\n", m_body.size());
builder.append((char const*)m_body.data(), m_body.size());

View file

@ -14,15 +14,12 @@
namespace HTTP {
void HttpsJob::start()
void HttpsJob::start(NonnullRefPtr<Core::Socket> socket)
{
VERIFY(!m_socket);
m_socket = TLS::TLSv12::construct(this);
m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
m_socket->on_tls_connected = [this] {
dbgln_if(HTTPSJOB_DEBUG, "HttpsJob: on_connected callback");
on_socket_connected();
};
VERIFY(is<TLS::TLSv12>(*socket));
m_socket = static_ptr_cast<TLS::TLSv12>(socket);
m_socket->on_tls_error = [&](TLS::AlertDescription error) {
if (error == TLS::AlertDescription::HandshakeFailure) {
deferred_invoke([this] {
@ -46,12 +43,23 @@ void HttpsJob::start()
if (on_certificate_requested)
on_certificate_requested(*this);
};
if (m_socket->is_established()) {
dbgln("Reusing previous connection for {}", url());
deferred_invoke([this] { on_socket_connected(); });
} else {
dbgln("Creating a new connection for {}", url());
m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
m_socket->on_tls_connected = [this] {
dbgln_if(HTTPSJOB_DEBUG, "HttpsJob: on_connected callback");
on_socket_connected();
};
bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port_or_default());
if (!success) {
deferred_invoke([this] {
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
});
}
}
}
void HttpsJob::shutdown()
@ -60,7 +68,6 @@ void HttpsJob::shutdown()
return;
m_socket->on_tls_ready_to_read = nullptr;
m_socket->on_tls_connected = nullptr;
remove_child(*m_socket);
m_socket = nullptr;
}

View file

@ -28,10 +28,13 @@ public:
{
}
virtual void start() override;
virtual void start(NonnullRefPtr<Core::Socket>) override;
virtual void shutdown() override;
void set_certificate(String certificate, String key);
Core::Socket const* socket() const { return m_socket; }
URL url() const { return m_request.url(); }
Function<void(HttpsJob&)> on_certificate_requested;
protected:

View file

@ -85,7 +85,7 @@ void Job::flush_received_buffers()
{
if (!m_can_stream_response || m_buffered_size == 0)
return;
dbgln_if(JOB_DEBUG, "Job: Flushing received buffers: have {} bytes in {} buffers", m_buffered_size, m_received_buffers.size());
dbgln_if(JOB_DEBUG, "Job: Flushing received buffers: have {} bytes in {} buffers for {}", m_buffered_size, m_received_buffers.size(), m_request.url());
for (size_t i = 0; i < m_received_buffers.size(); ++i) {
auto& payload = m_received_buffers[i];
auto written = do_write(payload);
@ -100,7 +100,7 @@ void Job::flush_received_buffers()
payload = payload.slice(written, payload.size() - written);
break;
}
dbgln_if(JOB_DEBUG, "Job: Flushing received buffers done: have {} bytes in {} buffers", m_buffered_size, m_received_buffers.size());
dbgln_if(JOB_DEBUG, "Job: Flushing received buffers done: have {} bytes in {} buffers for {}", m_buffered_size, m_received_buffers.size(), m_request.url());
}
void Job::on_socket_connected()
@ -121,6 +121,7 @@ void Job::on_socket_connected()
deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
});
register_on_ready_to_read([&] {
dbgln_if(JOB_DEBUG, "Ready to read for {}, state = {}, cancelled = {}", m_request.url(), to_underlying(m_state), is_cancelled());
if (is_cancelled())
return;
@ -132,9 +133,12 @@ void Job::on_socket_connected()
}
if (m_state == State::InStatus) {
if (!can_read_line())
if (!can_read_line()) {
dbgln_if(JOB_DEBUG, "Job {} cannot read line", m_request.url());
return;
}
auto line = read_line(PAGE_SIZE);
dbgln_if(JOB_DEBUG, "Job {} read line of length {}", m_request.url(), line.length());
if (line.is_null()) {
dbgln("Job: Expected HTTP status");
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
@ -287,7 +291,9 @@ void Job::on_socket_connected()
}
}
dbgln_if(JOB_DEBUG, "Waiting for payload for {}", m_request.url());
auto payload = receive(read_size);
dbgln_if(JOB_DEBUG, "Received {} bytes of payload from {}", payload.size(), m_request.url());
if (payload.is_empty()) {
if (eof()) {
finish_up();

View file

@ -21,7 +21,7 @@ public:
explicit Job(const HttpRequest&, OutputStream&);
virtual ~Job() override;
virtual void start() override = 0;
virtual void start(NonnullRefPtr<Core::Socket>) override = 0;
virtual void shutdown() override = 0;
HttpResponse* response() { return static_cast<HttpResponse*>(Core::NetworkJob::response()); }

View file

@ -8,6 +8,7 @@ compile_ipc(RequestClient.ipc RequestClientEndpoint.h)
set(SOURCES
ClientConnection.cpp
ConnectionCache.cpp
Request.cpp
RequestClientEndpoint.h
RequestServerEndpoint.h

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ConnectionCache.h"
#include <LibCore/EventLoop.h>
namespace RequestServer::ConnectionCache {
HashMap<ConnectionKey, Vector<Connection<Core::TCPSocket>>> g_tcp_connection_cache {};
HashMap<ConnectionKey, Vector<Connection<TLS::TLSv12>>> g_tls_connection_cache {};
void request_did_finish(URL const& url, Core::Socket const* socket)
{
if (!socket) {
dbgln("Request with a null socket finished for URL {}", url);
return;
}
dbgln("Request for {} finished", url);
ConnectionKey key { url.host(), url.port_or_default() };
auto fire_off_next_job = [&](auto& cache) {
auto it = cache.find(key);
if (it == cache.end()) {
dbgln("Request for URL {} finished, but we don't own that!", url);
return;
}
auto connection_it = it->value.find_if([&](auto& connection) { return connection.socket == socket; });
if (connection_it.is_end()) {
dbgln("Request for URL {} finished, but we don't have a socket for that!", url);
return;
}
auto& connection = *connection_it;
if (connection.request_queue.is_empty()) {
connection.has_started = false;
connection.removal_timer->on_timeout = [&connection, &cache_entry = it->value] {
Core::deferred_invoke([&] {
dbgln("Removing no-longer-used connection {}", &connection);
cache_entry.remove_first_matching([&](auto& entry) { return &entry == &connection; });
});
};
connection.removal_timer->start();
} else {
using SocketType = RemoveCVReference<decltype(*connection.socket)>;
bool is_connected;
if constexpr (IsSame<SocketType, TLS::TLSv12>)
is_connected = connection.socket->is_established();
else
is_connected = connection.socket->is_connected();
if (!is_connected) {
// Create another socket for the connection.
dbgln("Creating a new socket for {}", url);
connection.socket = SocketType::construct(nullptr);
}
dbgln("Running next job in queue for connection {}", &connection);
auto request = connection.request_queue.take_first();
request(connection.socket);
}
};
if (is<TLS::TLSv12>(socket))
fire_off_next_job(g_tls_connection_cache);
else if (is<Core::TCPSocket>(socket))
fire_off_next_job(g_tcp_connection_cache);
else
dbgln("Unknown socket {} finished for URL {}", *socket, url);
}
}

View file

@ -0,0 +1,103 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/URL.h>
#include <AK/Vector.h>
#include <LibCore/TCPSocket.h>
#include <LibCore/Timer.h>
#include <LibTLS/TLSv12.h>
namespace RequestServer::ConnectionCache {
template<typename Socket>
struct Connection {
using QueueType = Vector<Function<void(Core::Socket&)>>;
using SocketType = Socket;
NonnullRefPtr<Socket> socket;
QueueType request_queue;
NonnullRefPtr<Core::Timer> removal_timer;
bool has_started { false };
};
struct ConnectionKey {
String hostname;
u16 port { 0 };
bool operator==(ConnectionKey const&) const = default;
};
};
template<>
struct AK::Traits<RequestServer::ConnectionCache::ConnectionKey> : public AK::GenericTraits<RequestServer::ConnectionCache::ConnectionKey> {
static u32 hash(RequestServer::ConnectionCache::ConnectionKey const& key)
{
return pair_int_hash(key.hostname.hash(), key.port);
}
};
namespace RequestServer::ConnectionCache {
extern HashMap<ConnectionKey, Vector<Connection<Core::TCPSocket>>> g_tcp_connection_cache;
extern HashMap<ConnectionKey, Vector<Connection<TLS::TLSv12>>> g_tls_connection_cache;
void request_did_finish(URL const&, Core::Socket const*);
constexpr static inline size_t MaxConcurrentConnectionsPerURL = 2;
constexpr static inline size_t ConnectionKeepAliveTimeMilliseconds = 10'000;
decltype(auto) get_or_create_connection(auto& cache, URL const& url, auto& job)
{
auto start_job = [&job](auto& socket) {
job.start(socket);
};
auto& sockets_for_url = cache.ensure({ url.host(), url.port_or_default() });
auto it = sockets_for_url.find_if([](auto& connection) { return connection.request_queue.is_empty(); });
auto did_add_new_connection = false;
if (it.is_end() && sockets_for_url.size() < ConnectionCache::MaxConcurrentConnectionsPerURL) {
sockets_for_url.append({
RemoveCVReference<decltype(cache.begin()->value.at(0))>::SocketType::construct(nullptr),
{},
Core::Timer::create_single_shot(ConnectionKeepAliveTimeMilliseconds, nullptr),
});
did_add_new_connection = true;
}
size_t index;
if (it.is_end()) {
if (did_add_new_connection) {
index = sockets_for_url.size() - 1;
} else {
// Find the least backed-up connection (based on how many entries are in their request queue.
index = 0;
auto min_queue_size = (size_t)-1;
for (auto it = sockets_for_url.begin(); it != sockets_for_url.end(); ++it) {
if (auto queue_size = it->request_queue.size(); min_queue_size > queue_size) {
index = it.index();
min_queue_size = queue_size;
}
}
}
} else {
index = it.index();
}
auto& connection = sockets_for_url[index];
if (!connection.has_started) {
dbgln("Immediately start request for url {} in {}", url, &connection);
connection.has_started = true;
connection.removal_timer->stop();
start_job(*connection.socket);
} else {
dbgln("Enqueue request for URL {} in {}", url, &connection);
connection.request_queue.append(move(start_job));
}
return connection;
}
}

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ConnectionCache.h"
#include <LibGemini/GeminiJob.h>
#include <LibGemini/GeminiRequest.h>
#include <RequestServer/GeminiProtocol.h>
@ -34,7 +35,9 @@ OwnPtr<Request> GeminiProtocol::start_request(ClientConnection& client, const St
auto job = Gemini::GeminiJob::construct(request, *output_stream);
auto protocol_request = GeminiRequest::create_with_job({}, client, (Gemini::GeminiJob&)*job, move(output_stream));
protocol_request->set_request_fd(pipe_result.value().read_fd);
job->start();
ConnectionCache::get_or_create_connection(ConnectionCache::g_tls_connection_cache, url, *job);
return protocol_request;
}

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ConnectionCache.h"
#include <LibCore/EventLoop.h>
#include <LibGemini/GeminiJob.h>
#include <LibGemini/GeminiResponse.h>
#include <RequestServer/GeminiRequest.h>
@ -15,6 +17,9 @@ GeminiRequest::GeminiRequest(ClientConnection& client, NonnullRefPtr<Gemini::Gem
, m_job(job)
{
m_job->on_finish = [this](bool success) {
Core::deferred_invoke([url = m_job->url(), socket = m_job->socket()] {
ConnectionCache::request_did_finish(url, socket);
});
if (auto* response = m_job->response()) {
set_downloaded_size(this->output_stream().size());
if (!response->meta().is_empty()) {

View file

@ -18,6 +18,8 @@ public:
virtual ~GeminiRequest() override;
static NonnullOwnPtr<GeminiRequest> create_with_job(Badge<GeminiProtocol>, ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>, NonnullOwnPtr<OutputFileStream>&&);
Gemini::GeminiJob const& job() const { return *m_job; }
private:
explicit GeminiRequest(ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>, NonnullOwnPtr<OutputFileStream>&&);

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;
}

View file

@ -20,6 +20,7 @@ public:
static NonnullOwnPtr<HttpRequest> create_with_job(Badge<HttpProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
HTTP::HttpJob& job() { return m_job; }
HTTP::HttpJob const& job() const { return m_job; }
private:
explicit HttpRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);

View file

@ -19,6 +19,7 @@ public:
static NonnullOwnPtr<HttpsRequest> create_with_job(Badge<HttpsProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
HTTP::HttpsJob& job() { return m_job; }
HTTP::HttpsJob const& job() const { return m_job; }
private:
explicit HttpsRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);