Commit b5c3fb8a authored by Jakob Otto's avatar Jakob Otto

Implement read retries for tcp

parent 6344cacd
......@@ -23,7 +23,6 @@
#include "caf/fwd.hpp"
#include "caf/logger.hpp"
#include "caf/net/defaults.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/fwd.hpp"
#include "caf/net/receive_policy.hpp"
......@@ -74,31 +73,36 @@ public:
// -- member functions -------------------------------------------------------
bool handle_read_event(endpoint_manager&) override {
auto buf = this->read_buf_.data() + this->collected_;
size_t len = this->read_threshold_ - this->collected_;
CAF_LOG_TRACE(CAF_ARG2("handle", this->handle().id)
<< CAF_ARG2("missing", len));
auto ret = read(this->handle_, make_span(buf, len));
// Update state.
if (auto num_bytes = get_if<size_t>(&ret)) {
CAF_LOG_DEBUG(CAF_ARG(len)
<< CAF_ARG(this->handle_.id) << CAF_ARG(*num_bytes));
this->collected_ += *num_bytes;
if (this->collected_ >= this->read_threshold_) {
if (auto err = this->next_layer_.handle_data(*this,
make_span(
this->read_buf_))) {
CAF_LOG_ERROR("handle_data failed: " << CAF_ARG(err));
size_t reads = 0;
while (reads++ < this->max_consecutive_reads_) {
auto buf = this->read_buf_.data() + this->collected_;
size_t len = this->read_threshold_ - this->collected_;
CAF_LOG_TRACE(CAF_ARG2("handle", this->handle().id)
<< CAF_ARG2("missing", len));
auto ret = read(this->handle_, make_span(buf, len));
// Update state.
if (auto num_bytes = get_if<size_t>(&ret)) {
CAF_LOG_DEBUG(CAF_ARG(len)
<< CAF_ARG(this->handle_.id) << CAF_ARG(*num_bytes));
this->collected_ += *num_bytes;
if (this->collected_ >= this->read_threshold_) {
if (auto err = this->next_layer_.handle_data(*this,
make_span(
this->read_buf_))) {
CAF_LOG_ERROR("handle_data failed: " << CAF_ARG(err));
return false;
}
this->prepare_next_read();
}
} else {
auto err = get<sec>(ret);
if (err == sec::unavailable_or_would_block) {
break;
} else {
CAF_LOG_DEBUG("read failed" << CAF_ARG(err));
this->next_layer_.handle_error(err);
return false;
}
this->prepare_next_read();
}
} else {
auto err = get<sec>(ret);
if (err != sec::unavailable_or_would_block) {
CAF_LOG_DEBUG("read failed" << CAF_ARG(err));
this->next_layer_.handle_error(err);
return false;
}
}
return true;
......@@ -214,8 +218,6 @@ private:
size_t collected_;
size_t max_;
receive_policy_flag rd_flag_;
// TODO implement retries using this member!
// size_t max_consecutive_reads_;
};
} // namespace caf::net
......@@ -18,6 +18,7 @@
#pragma once
#include "caf/defaults.hpp"
#include "caf/detail/overload.hpp"
#include "caf/fwd.hpp"
#include "caf/logger.hpp"
......@@ -100,6 +101,9 @@ public:
CAF_LOG_TRACE("");
manager_ = &parent;
auto& cfg = system().config();
max_consecutive_reads_ = get_or(this->system().config(),
"middleman.max-consecutive-reads",
defaults::middleman::max_consecutive_reads);
auto max_header_bufs = get_or(cfg, "middleman.max-header-buffers",
defaults::middleman::max_header_buffers);
header_bufs_.reserve(max_header_bufs);
......@@ -224,6 +228,8 @@ protected:
buffer_type read_buf_;
endpoint_manager* manager_;
size_t max_consecutive_reads_;
};
} // namespace caf::net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment