Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions include/bitcoin/network/net/proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ class BCT_API proxy
queue deferred_{};
size_t backlog_{};
bool writing_{};
bool looping_{};
bool again_{};
bool batched_{};
bool parted_{};
bool opened_{};
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/network/net/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class BCT_API socket
/// A held write completes when held, before it is written.
void hold(bool enable) NOEXCEPT;

/// Complete a write held since hold, if any (strand).
void settle() NOEXCEPT;

/// Writes are held (strand).
bool holding() const NOEXCEPT;

Expand Down Expand Up @@ -733,7 +736,9 @@ class BCT_API socket
// readable bytes of which are the held writes.
http::flat_buffer response_;
std::function<void()> deferred_{};
std::function<void()> settled_{};
bool hold_{};
bool settling_{};
bool sending_{};

// Websocket framing (once upgraded).
Expand Down
26 changes: 21 additions & 5 deletions src/net/proxy_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,33 @@ void proxy::do_write(pending& write_, bool notification) NOEXCEPT
write();
}

// A write invoked by a completion within the loop is invoked by the loop.
void proxy::write() NOEXCEPT
{
BC_ASSERT(stranded());
if (queue_.empty())
if (looping_)
{
again_ = true;
return;
}

// Invokes oldest writer on the queue, completion invokes handle_write.
// Its writes are held for the next writer while another is queued.
writing_ = true;
socket_->hold(queue_.size() > one);
queue_.front().call();
// Its writes are held for the next writer while another is queued, and a
// held write completes on settle.
looping_ = true;
do
{
again_ = false;
if (queue_.empty())
break;

writing_ = true;
socket_->hold(queue_.size() > one);
queue_.front().call();
socket_->settle();
}
while (again_);
looping_ = false;
}

void proxy::handle_write(const code& ec, size_t bytes) NOEXCEPT
Expand Down
17 changes: 17 additions & 0 deletions src/net/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,15 @@ void socket::hold(bool enable) NOEXCEPT
{
BC_ASSERT(stranded());
hold_ = enable;
settling_ = enable;
}

void socket::settle() NOEXCEPT
{
BC_ASSERT(stranded());
settling_ = false;
if (settled_)
std::exchange(settled_, {})();
}

bool socket::holding() const NOEXCEPT
Expand All @@ -448,6 +457,7 @@ void socket::release(count_handler&& handler) NOEXCEPT
{
BC_ASSERT(stranded());
hold_ = false;
settling_ = false;
transmit({}, false, handler);
}

Expand All @@ -471,6 +481,13 @@ void socket::transmit(const std::span<const io::const_buffer>& buffers,
if (hold && hold_ && size <= floored_subtract(bound, held))
{
response_.append(buffers);
if (settling_)
{
settling_ = false;
settled_ = std::bind(handler, error::success, size);
return;
}

io::post(strand_, std::bind(handler, error::success, size));
return;
}
Expand Down
32 changes: 32 additions & 0 deletions test/net/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ BC_FIXTURE_TEST_CASE(socket__hold__fits_minimum__completes_held_and_written_with
written.set(!ec && size == second.size() && !instance->holding());
});
});
instance->settle();
});

BC_REQUIRE(held.get());
Expand All @@ -1110,6 +1111,34 @@ BC_FIXTURE_TEST_CASE(socket__hold__fits_minimum__completes_held_and_written_with
instance->stop();
}

BC_FIXTURE_TEST_CASE(socket__settle__held__completes_on_settle, acceptor_fixture)
{
static const system::data_array<2> data{ 'a', 'b' };
auto params = clear;
params.minimum_buffer = 64;
const awaiter<code> accepted{};
const auto instance = accept(params, accepted);
BC_REQUIRE_EQUAL(accepted.get(), error::success);

const awaiter<bool> settled{};
io::post(instance->strand(), [=]() NOEXCEPT
{
const auto completed = std::make_shared<bool>(false);
instance->hold(true);
instance->tcp_write({ data.data(), data.size() }, [=](const code& ec, size_t size) NOEXCEPT
{
*completed = !ec && size == data.size();
});

const auto before = *completed;
instance->settle();
settled.set(!before && *completed && instance->holding());
});

BC_REQUIRE(settled.get());
instance->stop();
}

BC_FIXTURE_TEST_CASE(socket__hold__exceeds_minimum__written, acceptor_fixture)
{
static const system::data_array<2> data{ 'a', 'b' };
Expand All @@ -1127,6 +1156,7 @@ BC_FIXTURE_TEST_CASE(socket__hold__exceeds_minimum__written, acceptor_fixture)
{
written.set(!ec && size == data.size() && !instance->holding());
});
instance->settle();
});

BC_REQUIRE(written.get());
Expand Down Expand Up @@ -1161,6 +1191,7 @@ BC_FIXTURE_TEST_CASE(socket__release__held__written, acceptor_fixture)
released.set(!ec && is_zero(size) && !instance->holding());
});
});
instance->settle();
});

BC_REQUIRE(held.get());
Expand Down Expand Up @@ -1193,6 +1224,7 @@ BC_FIXTURE_TEST_CASE(socket__lazy_stop__held__written, acceptor_fixture)
held.set(!ec && instance->holding());
instance->lazy_stop();
});
instance->settle();
});

BC_REQUIRE(held.get());
Expand Down
Loading