Skip to content

Commit b354849

Browse files
author
Valtteri Heikkila
committed
Clarified method names and wrote comments in http_linux.cpp
1 parent b130149 commit b354849

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ namespace web { namespace http
127127
~linux_connection_pool()
128128
{
129129
std::lock_guard<std::mutex> lock(m_connections_mutex);
130+
// Close all connections. This happens when linux_client is destroyed because
131+
// it only has shared_ptr reference to pool. Connections use weak_ptr instead.
130132
for (auto& connection : m_connections)
131133
{
132134
connection->close(true);
@@ -155,15 +157,16 @@ namespace web { namespace http
155157

156158
std::shared_ptr<linux_connection> obtain()
157159
{
158-
if (is_connections_empty())
160+
if (is_pool_empty())
159161
{
160162
// No connections in pool => create new connection instance.
163+
// shared_from_this() is only to create a weak_ptr to pool.
161164
return std::make_shared<linux_connection>(shared_from_this(), m_io_service);
162165
}
163166
else
164167
{
165168
// Reuse connection from pool.
166-
auto connection(obtain_begin());
169+
auto connection(get_head());
167170
connection->start_reuse();
168171
return connection;
169172
}
@@ -176,13 +179,13 @@ namespace web { namespace http
176179
}
177180

178181
private:
179-
bool is_connections_empty()
182+
bool is_pool_empty()
180183
{
181184
std::lock_guard<std::mutex> lock(m_connections_mutex);
182185
return m_connections.empty();
183186
}
184187

185-
std::shared_ptr<linux_connection> obtain_begin()
188+
std::shared_ptr<linux_connection> get_head()
186189
{
187190
std::lock_guard<std::mutex> lock(m_connections_mutex);
188191
auto connection(*m_connections.begin());
@@ -349,10 +352,10 @@ namespace web { namespace http
349352
}
350353
request_stream << ":" << port << CRLF;
351354

352-
// Manipulate headers
355+
// Copy headers to keep the original request state intact.
353356
auto headers(ctx->m_request.headers());
354357

355-
// Check user specified transfer-encoding
358+
// Check user specified transfer-encoding.
356359
std::string transferencoding;
357360
if (headers.match(header_names::transfer_encoding, transferencoding) && transferencoding == "chunked")
358361
{
@@ -363,6 +366,7 @@ namespace web { namespace http
363366

364367
if (headers.match(header_names::content_length, ctx->m_known_size))
365368
{
369+
// Have request body if content length header field is non-zero.
366370
has_body = (0 != ctx->m_known_size);
367371
}
368372
else
@@ -387,17 +391,21 @@ namespace web { namespace http
387391
}
388392

389393
request_stream << flatten_http_headers(headers);
394+
// Enforce HTTP connection keep alive (even for the old HTTP/1.0 protocol).
390395
request_stream << "Connection: Keep-Alive" << CRLF;
391396
request_stream << CRLF;
392397

393398
ctx->set_timer(static_cast<int>(client_config().timeout().count()));
394399

395400
if (ctx->m_connection->socket().is_open())
396401
{
402+
// If socket is already open (connection is reused), try to write the request directly.
397403
write_request(ctx);
398404
}
399405
else
400406
{
407+
// If the connection is new (unresolved and unconnected socket), then start async
408+
// call to resolve first, leading eventually to request write.
401409
tcp::resolver::query query(host, utility::conversions::print_string(port));
402410

403411
m_resolver.async_resolve(query, boost::bind(&linux_client::handle_resolve, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator, ctx));
@@ -408,7 +416,9 @@ namespace web { namespace http
408416
{
409417
ctx->m_cancellationRegistration = request_ctx->m_request._cancellation_token().register_callback([ctx]()
410418
{
419+
// Cancel operations and all async handlers.
411420
ctx->m_connection->cancel();
421+
// Shut down transmissions and close socket. Also prevents connection being pooled.
412422
ctx->m_connection->close(false);
413423
});
414424
}
@@ -742,17 +752,19 @@ namespace web { namespace http
742752
|| (boost::asio::error::connection_aborted == ec));
743753
if (socket_was_closed && ctx->m_connection->is_reused() && ctx->m_connection->socket().is_open())
744754
{
745-
// Connection was closed by the server for some reason during the connection was
746-
// being pooled. We re-send the request to get a new connection.
755+
// Connection was closed while connection was in pool.
756+
// Ensure connection is closed in a robust way.
747757
ctx->m_connection->close(false);
748758

759+
// Replace context and destroy the old one. The request,
760+
// completion event and cancellation registration are copied to
761+
// the new context. This will also obtain a new connection.
749762
auto new_ctx = details::linux_client_request_context::create_request_context(ctx->m_http_client, ctx->m_request);
750763
new_ctx->m_request_completion = ctx->m_request_completion;
751764
new_ctx->m_cancellationRegistration = ctx->m_cancellationRegistration;
752-
753-
// Close and destroy and the old socket.
754765
ctx = std::static_pointer_cast<linux_client_request_context>(new_ctx);
755766

767+
// Resend the request using the new context.
756768
send_request(ctx);
757769
}
758770
else
@@ -1082,6 +1094,7 @@ namespace web { namespace http
10821094
linux_client_request_context::~linux_client_request_context()
10831095
{
10841096
m_timeout_timer.cancel();
1097+
// Give connection back to the pool where it can be reused.
10851098
std::static_pointer_cast<linux_client>(m_http_client)->m_pool->release(m_connection);
10861099
}
10871100

@@ -1091,8 +1104,11 @@ namespace web { namespace http
10911104
{
10921105
if (auto pool_ptr = m_pool_weak.lock())
10931106
{
1107+
// Remove connection from pool only if pool still exists (lock succeeds).
10941108
pool_ptr->remove(shared_from_this());
10951109
}
1110+
// If lock fails, pool has been destroyed and we let connection object
1111+
// to expire via shared_ptr. This should happen when this method returns.
10961112
}
10971113
}
10981114

0 commit comments

Comments
 (0)