mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:37:46 +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:
parent
c5d7eb8618
commit
65f7e45a75
22 changed files with 295 additions and 51 deletions
|
@ -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);
|
||||
});
|
||||
};
|
||||
bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default());
|
||||
if (!success) {
|
||||
if (m_socket->is_connected()) {
|
||||
dbgln("Reusing previous connection for {}", url());
|
||||
deferred_invoke([this] {
|
||||
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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,11 +43,22 @@ void HttpsJob::start()
|
|||
if (on_certificate_requested)
|
||||
on_certificate_requested(*this);
|
||||
};
|
||||
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);
|
||||
});
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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()); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue