1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* 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, NonnullOwnPtrVector<Connection<Core::TCPSocket>>> g_tcp_connection_cache {};
HashMap<ConnectionKey, NonnullOwnPtrVector<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;
if constexpr (REQUEST_SERVER_DEBUG)
connection->current_url = {};
connection->removal_timer->on_timeout = [ptr = connection.ptr(), &cache_entry = it->value, &key = it->key, &cache] {
Core::deferred_invoke([&] {
dbgln("Removing no-longer-used connection {}", ptr);
cache_entry.remove_first_matching([&](auto& entry) { return entry.ptr() == ptr; });
if (cache_entry.is_empty())
cache.remove(key);
});
};
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();
if constexpr (REQUEST_SERVER_DEBUG) {
connection->timer.start();
connection->current_url = url;
}
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);
}
#if REQUEST_SERVER_DEBUG
void dump_jobs()
{
dbgln("=========== TLS Connection Cache ==========");
for (auto& connection : g_tls_connection_cache) {
dbgln(" - {}:{}", connection.key.hostname, connection.key.port);
for (auto& entry : connection.value) {
dbgln(" - Connection {} (started={})", &entry, entry.has_started);
dbgln(" Currently loading {} ({} elapsed)", entry.current_url, entry.timer.elapsed());
dbgln(" Request Queue:");
for (auto& job : entry.request_queue)
dbgln(" - {}", &job);
}
}
dbgln("=========== TCP Connection Cache ==========");
for (auto& connection : g_tcp_connection_cache) {
dbgln(" - {}:{}", connection.key.hostname, connection.key.port);
for (auto& entry : connection.value) {
dbgln(" - Connection {} (started={})", &entry, entry.has_started);
dbgln(" Currently loading {} ({} elapsed)", entry.current_url, entry.timer.elapsed());
dbgln(" Request Queue:");
for (auto& job : entry.request_queue)
dbgln(" - {}", &job);
}
}
}
#endif
}
|