Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

longevity of connection in lambda and error handling #101

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions CRUD/service/persistent_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ class persistent_connection
/// Perform an asynchronous read operation.
void do_read()
{
auto self{this->shared_from_this()}; //make sure connection is alive while lambda is in progress
socket_.async_read_some(boost::asio::buffer(buffer_),
[this](boost::system::error_code ec, std::size_t bytes_transferred)
[this, self](boost::system::error_code ec, std::size_t bytes_transferred)
{

if (!ec) [[likely]]
Expand Down Expand Up @@ -113,17 +114,21 @@ class persistent_connection
/// Perform an asynchronous write operation.
void do_write()
{
auto self{this->shared_from_this()}; //make sure connection is alive while lambda is in progress
reply_.headers.emplace_back("Connection" , "keep-alive");
boost::asio::async_write(socket_, reply_.to_buffers(),
[this](boost::system::error_code, std::size_t)
[this, self](boost::system::error_code ec, std::size_t)
{
// TODO: VL: why is error_code not checked?
request_parser_.reset();
reply_.headers.resize(0);
reply_.status = reply::status_type::ok;
reply_.content="";
request_ = request();
do_read();
request_ = request();
Copy link
Collaborator

@mrbald mrbald Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not do anything at all if ec is an error, so move all happy path callback code under if(!ec)

if(!ec) [[likely]] {
do_read();
} else if (ec != boost::asio::error::operation_aborted) {
connection_manager_.stop(this->shared_from_this());
}
});
}

Expand All @@ -144,12 +149,15 @@ class persistent_connection
auto left_over_size = content_length_value - received_data_size;
auto end_data_ptr = request_.data.data() + received_data_size;
auto left_over_buffer = boost::asio::buffer(end_data_ptr, left_over_size);
auto self{this->shared_from_this()}; //make sure connection is alive while lambda is in progress
boost::asio::async_read(socket_, left_over_buffer, boost::asio::transfer_exactly(left_over_size),
[this](boost::system::error_code ec, std::size_t) {
[this, self](boost::system::error_code ec, std::size_t) {
if (!ec) [[likely]] {
request_handler_.handle_request(request_, reply_);
do_write();
} else if (ec != boost::asio::error::operation_aborted) {
connection_manager_.stop(this->shared_from_this());
}
do_write();
});
}
}
Expand Down