From 2a87e1904366d9d4d8d5a7d8fa00bf70a7a64c76 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 16:31:50 -0500 Subject: [PATCH 1/6] Regenerate artifacts. --- builds/cmake/CMakePresets.json | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/builds/cmake/CMakePresets.json b/builds/cmake/CMakePresets.json index 739a1f9d3..b99203db6 100644 --- a/builds/cmake/CMakePresets.json +++ b/builds/cmake/CMakePresets.json @@ -26,6 +26,7 @@ "name": "nix-base", "description": "Factored base settings for non-windows *nix based platforms.", "hidden": true, + "inherits": [ "base-pathing" ], "condition": { "type": "inList", "string": "${hostSystemName}", @@ -39,6 +40,7 @@ "name": "win-base", "description": "Factored base settings for windows platforms (msvc developer environment).", "hidden": true, + "inherits": [ "base-pathing" ], "generator": "Ninja", "condition": { "type": "equals", @@ -66,6 +68,7 @@ "name": "gnu-debug", "description": "Factored gnu specific debug settings.", "hidden": true, + "inherits": [ "config-debug" ], "cacheVariables": { "CMAKE_C_FLAGS": "$env{CMAKE_C_FLAGS} -Og -g1 --coverage -fprofile-update=atomic", "CMAKE_CXX_FLAGS": "$env{CMAKE_CXX_FLAGS} -Og -g1 --coverage -fprofile-update=atomic" @@ -75,6 +78,7 @@ "name": "gnu-release", "description": "Factored gnu specific release settings.", "hidden": true, + "inherits": [ "config-release" ], "cacheVariables": { "CMAKE_C_FLAGS": "$env{CMAKE_C_FLAGS} -O3", "CMAKE_CXX_FLAGS": "$env{CMAKE_CXX_FLAGS} -O3" @@ -124,9 +128,10 @@ "name": "optimization-host-native", "description": "Factored settings for march/mtune using for local execution.", "hidden": true, - "cacheVariables": { - "CMAKE_C_FLAGS": "$env{CMAKE_C_FLAGS} -march=native -mtune=native", - "CMAKE_CXX_FLAGS": "$env{CMAKE_CXX_FLAGS} -march=native -mtune=native" + "inherits": [ "nix-base" ], + "environment": { + "CMAKE_C_FLAGS": "$penv{CMAKE_C_FLAGS} -march=native -mtune=native", + "CMAKE_CXX_FLAGS": "$penv{CMAKE_CXX_FLAGS} -march=native -mtune=native" } }, { @@ -175,7 +180,7 @@ }, { "name": "nix-gnu-debug-static-native", - "displayName": "*nix/GNU/Debug/Static", + "displayName": "*nix/GNU/Debug/Static/Native", "description": "Debug build with static linking for native architecture.", "hidden": false, "inherits": [ @@ -187,7 +192,7 @@ }, { "name": "nix-gnu-debug-shared-native", - "displayName": "*nix/GNU/Debug/Shared", + "displayName": "*nix/GNU/Debug/Shared/Native", "description": "Debug build with shared linking for native architecture.", "hidden": false, "inherits": [ @@ -199,7 +204,7 @@ }, { "name": "nix-gnu-release-static-native", - "displayName": "*nix/GNU/Release/Static", + "displayName": "*nix/GNU/Release/Static/Native", "description": "Release build with static linking and standard optimizations for native architecture.", "hidden": false, "inherits": [ @@ -211,7 +216,7 @@ }, { "name": "nix-gnu-release-shared-native", - "displayName": "*nix/GNU/Release/Shared", + "displayName": "*nix/GNU/Release/Shared/Native", "description": "Release build with shared linking and standard optimizations for native architecture.", "hidden": false, "inherits": [ @@ -353,25 +358,25 @@ }, { "name": "nix-gnu-debug-static-native", - "displayName": "*nix/GNU/Debug/Static", + "displayName": "*nix/GNU/Debug/Static/Native", "inherits": [ "nix-target-list" ], "configurePreset": "nix-gnu-debug-static-native" }, { "name": "nix-gnu-debug-shared-native", - "displayName": "*nix/GNU/Debug/Shared", + "displayName": "*nix/GNU/Debug/Shared/Native", "inherits": [ "nix-target-list" ], "configurePreset": "nix-gnu-debug-shared-native" }, { "name": "nix-gnu-release-static-native", - "displayName": "*nix/GNU/Release/Static", + "displayName": "*nix/GNU/Release/Static/Native", "inherits": [ "nix-target-list" ], "configurePreset": "nix-gnu-release-static-native" }, { "name": "nix-gnu-release-shared-native", - "displayName": "*nix/GNU/Release/Shared", + "displayName": "*nix/GNU/Release/Shared/Native", "inherits": [ "nix-target-list" ], "configurePreset": "nix-gnu-release-shared-native" }, From 67a4080ef77a2c997dd1409c174c332e14ef7548 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 11:39:19 -0500 Subject: [PATCH 2/6] Hold small socket writes while more are queued, written as one. --- .../network/impl/messages/json_body.ipp | 22 ++- .../bitcoin/network/messages/http/bodies.hpp | 4 +- .../network/messages/http/flat_buffer.hpp | 5 + include/bitcoin/network/net/proxy.hpp | 6 +- include/bitcoin/network/net/socket.hpp | 26 ++- src/messages/http/flat_buffer.cpp | 47 ++++++ src/messages/peer/body.cpp | 4 +- src/net/proxy_queue.cpp | 10 ++ src/net/socket.cpp | 100 +++++++++++- src/net/socket_body.cpp | 4 +- src/net/socket_http.cpp | 16 +- src/net/socket_stop.cpp | 8 + src/net/socket_ws.cpp | 15 +- test/messages/http/header.cpp | 58 +++++++ test/messages/json_body_writer.cpp | 32 ++++ test/net/socket.cpp | 149 ++++++++++++++++++ 16 files changed, 471 insertions(+), 35 deletions(-) diff --git a/include/bitcoin/network/impl/messages/json_body.ipp b/include/bitcoin/network/impl/messages/json_body.ipp index 64d07556e..6c0a29c49 100644 --- a/include/bitcoin/network/impl/messages/json_body.ipp +++ b/include/bitcoin/network/impl/messages/json_body.ipp @@ -165,14 +165,8 @@ void CLASS::writer::init(code& ec) NOEXCEPT return; } - // Serialization is chunked by the estimate, within that bound less any - // terminator. An unestimated message is chunked by the bound. - const auto reserve = is_zero(terminator_) ? zero : one; - const auto maximum = floored_subtract(value_.buffer->max_size(), reserve); - chunk_ = is_zero(value_.size_hint) ? maximum : - std::min(value_.size_hint, maximum); - - value_.buffer->consume(value_.buffer->size()); + // Serialization is chunked by the estimate, within that bound. + chunk_ = is_zero(value_.size_hint) ? max_size_t : value_.size_hint; ec.clear(); serializer_.reset(&value_.model); } @@ -194,7 +188,13 @@ CLASS::writer::get(code& ec) NOEXCEPT return {}; } - const auto size = chunk_; + // The chunk and any terminator, which follows the final serialized byte, + // are bound by the room that follows the buffer's readable bytes. + const auto reserve = is_zero(terminator_) ? zero : one; + const auto size = std::min(chunk_, system::floored_subtract( + value_.buffer->max_size(), system::ceilinged_add( + value_.buffer->size(), reserve))); + if (is_zero(size)) { ec = to_http_code(http_error_t::buffer_overflow); @@ -203,9 +203,6 @@ CLASS::writer::get(code& ec) NOEXCEPT try { - // Prepares the chunk, which the estimate sized within the bound, and - // any terminator, which follows the final serialized byte. - const auto reserve = is_zero(terminator_) ? zero : one; const auto scratch = value_.buffer->prepare(size + reserve); const auto data = system::pointer_cast(scratch.data()); const auto view = serializer_.read(data, size); @@ -218,7 +215,6 @@ CLASS::writer::get(code& ec) NOEXCEPT } ec.clear(); - value_.buffer->consume(scratch.size()); const auto more = !serializer_.done(); if (more || is_zero(terminator_)) return out_buffer{ std::make_pair(io::buffer(view), more) }; diff --git a/include/bitcoin/network/messages/http/bodies.hpp b/include/bitcoin/network/messages/http/bodies.hpp index 3af2ab110..9e577b236 100644 --- a/include/bitcoin/network/messages/http/bodies.hpp +++ b/include/bitcoin/network/messages/http/bodies.hpp @@ -421,9 +421,9 @@ struct stream_body uint8_t* data{}; const auto size = value_.size; if (!is_null(value_.buffer) && - size <= value_.buffer->max_size()) + size <= floored_subtract(value_.buffer->max_size(), + value_.buffer->size())) { - value_.buffer->clear(); data = pointer_cast( value_.buffer->prepare(size).data()); } diff --git a/include/bitcoin/network/messages/http/flat_buffer.hpp b/include/bitcoin/network/messages/http/flat_buffer.hpp index 065bc3b22..787f59bd8 100644 --- a/include/bitcoin/network/messages/http/flat_buffer.hpp +++ b/include/bitcoin/network/messages/http/flat_buffer.hpp @@ -64,6 +64,11 @@ class BCT_API flat_buffer /// Move size writable bytes (prepared) to the readable bytes. void commit(size_t size) NOEXCEPT; + /// Append the buffers to the readable bytes, of which at most one may be + /// within the writable bytes, throws std::length_error if the readable + /// size would then exceed the limit. + void append(const std::span& buffers) THROWS; + /// Remove size bytes from the front of the readable bytes. void consume(size_t size) NOEXCEPT; diff --git a/include/bitcoin/network/net/proxy.hpp b/include/bitcoin/network/net/proxy.hpp index be6890ef3..a3bd36a03 100644 --- a/include/bitcoin/network/net/proxy.hpp +++ b/include/bitcoin/network/net/proxy.hpp @@ -37,8 +37,10 @@ namespace network { /// Caller must retain ownership of read/write buffers until handler invoked. /// All writes are queued to prevent interleaving. /// Completion handler is invoked once write is complete, at which point the -/// next queued write is invoked. When a channel stops with pending writes the -/// write queue is purged without invoke of the purged handlers. +/// next queued write is invoked. A write with another queued behind it may be +/// held by the socket, completing before it is written with a later write. +/// When a channel stops with pending writes the write queue is purged without +/// invoke of the purged handlers. /// Each send is allocated (bytes/rate_limit) of time, and its completion is /// deferred by whatever portion of that allocation the write did not consume. /// Since nothing is produced until the completion handler is invoked, this diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index c3faf35b1..1e4ce849c 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -211,6 +211,16 @@ class BCT_API socket /// Reduce the retained json serialization buffer to the minimum. void reclaim_buffer() NOEXCEPT; + /// Hold writes that fit the minimum buffer for the next write (strand). + /// A held write completes when held, before it is written. + void hold(bool enable) NOEXCEPT; + + /// Writes are held (strand). + bool holding() const NOEXCEPT; + + /// End the hold and write the held writes (strand). + void release(count_handler&& handler) NOEXCEPT; + /// HTTP/WS (generic/rpc). /// ----------------------------------------------------------------------- @@ -347,6 +357,10 @@ class BCT_API socket const count_handler& handler) NOEXCEPT; void async_write(const io::const_buffer& buffer, bool binary, bool finish, const count_handler& handler) NOEXCEPT; + void transmit(const std::span& buffers, + bool hold, const count_handler& handler) NOEXCEPT; + void interject(const std::span& buffers, + const count_handler& handler) NOEXCEPT; void async_read_http(http::flat_buffer& buffer, http::request& request, const count_handler& handler) NOEXCEPT; void async_write_http(http::response&& response, @@ -452,6 +466,8 @@ class BCT_API socket io::const_buffer data{}; count_handler handler{}; std::array buffers{}; + bool control{}; + bool finish{}; }; enum class parse_mode @@ -673,6 +689,10 @@ class BCT_API socket void handle_chunk_last(const code& ec, size_t size, size_t total, const count_handler& handler) NOEXCEPT; + // transport + void handle_transmit(const code& ec, size_t size, size_t held, + const count_handler& handler) NOEXCEPT; + // utility // ------------------------------------------------------------------------ @@ -711,8 +731,12 @@ class BCT_API socket // Retains the detection prefix for a v1 peer (see handle_detection). http::flat_buffer detection_{ p2ps::stream::detection_size }; - // Retains the json serialization buffer (allocated on first write). + // Retains the serialization buffer (allocated on first write), the + // readable bytes of which are the held writes. http::flat_buffer response_; + std::function deferred_{}; + bool hold_{}; + bool sending_{}; // Websocket framing (once upgraded). std::optional ws_reader_{}; diff --git a/src/messages/http/flat_buffer.cpp b/src/messages/http/flat_buffer.cpp index 6f3ae68da..b8e5cb610 100644 --- a/src/messages/http/flat_buffer.cpp +++ b/src/messages/http/flat_buffer.cpp @@ -18,6 +18,10 @@ */ #include +#include +#include +#include +#include #include #include @@ -95,6 +99,49 @@ void flat_buffer::commit(size_t size) NOEXCEPT end_ += std::min(size, buffer_.size() - end_); } +void flat_buffer::append( + const std::span& buffers) THROWS +{ + // A buffer within the allocation is located by offset, as prepare may + // reallocate. + const const_buffers_type* inner{}; + size_t from{}; + size_t to{}; + size_t size{}; + const auto begin = buffer_.data(); + const auto end = std::next(begin, buffer_.size()); + for (const auto& buffer: buffers) + { + const auto data = pointer_cast(buffer.data()); + if (!is_zero(buffer.size()) && !std::less{}(data, begin) && + std::less{}(data, end)) + { + inner = &buffer; + from = sign_cast(std::distance(begin, + data)); + to = size; + } + + size += buffer.size(); + } + + auto out = pointer_cast(prepare(size).data()); + if (!is_null(inner)) + std::memmove(std::next(out, to), std::next(buffer_.data(), from), + inner->size()); + + for (const auto& buffer: buffers) + { + if (&buffer != inner) + std::copy_n(pointer_cast(buffer.data()), + buffer.size(), out); + + std::advance(out, buffer.size()); + } + + end_ += size; +} + void flat_buffer::consume(size_t size) NOEXCEPT { if (size >= this->size()) diff --git a/src/messages/peer/body.cpp b/src/messages/peer/body.cpp index dff49996d..98216ebbd 100644 --- a/src/messages/peer/body.cpp +++ b/src/messages/peer/body.cpp @@ -197,11 +197,11 @@ void body::writer::init(code& ec) NOEXCEPT { done_ = false; const auto size = ceilinged_add(heading::size(), value_.size); - if (!is_null(value_.buffer) && size <= value_.buffer->max_size()) + if (!is_null(value_.buffer) && size <= floored_subtract( + value_.buffer->max_size(), value_.buffer->size())) { try { - value_.buffer->clear(); const auto data = pointer_cast( value_.buffer->prepare(size).data()); diff --git a/src/net/proxy_queue.cpp b/src/net/proxy_queue.cpp index 0585ba259..e4a6818bd 100644 --- a/src/net/proxy_queue.cpp +++ b/src/net/proxy_queue.cpp @@ -111,7 +111,9 @@ void proxy::write() NOEXCEPT 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(); } @@ -121,6 +123,14 @@ void proxy::handle_write(const code& ec, size_t bytes) NOEXCEPT if (queue_.empty()) return; + // The last queued writer releases any writes that remain held. + if (!stopped() && is_one(queue_.size()) && socket_->holding()) + { + socket_->release(std::bind(&proxy::handle_write, + shared_from_this(), ec, bytes)); + return; + } + // Handler precedes pop so that a handler send does not start a second // write loop (a non-empty queue defers the start to the pop below). const auto handler = std::move(queue_.front().handler); diff --git a/src/net/socket.cpp b/src/net/socket.cpp index 506894784..db9801373 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -420,12 +420,108 @@ void socket::async_write(const io::const_buffer& buffer, bool binary, } else { - VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), - buffer, std::bind(&socket::handle_async, + transmit({ &buffer, one }, finish, + std::bind(&socket::handle_async, shared_from_this(), _1, _2, handler, "async_write")); } } +// Transport (write). +// ---------------------------------------------------------------------------- +// Writes are issued one at a time, a write issued over another deferred to +// its completion. The retained buffer is not prepared while a write of its +// bytes is in progress. + +void socket::hold(bool enable) NOEXCEPT +{ + BC_ASSERT(stranded()); + hold_ = enable; +} + +bool socket::holding() const NOEXCEPT +{ + BC_ASSERT(stranded()); + return !is_zero(response_.size()); +} + +void socket::release(count_handler&& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + hold_ = false; + transmit({}, false, handler); +} + +// protected +// The write is preceded by the held writes, or is held (if hold) with them. +void socket::transmit(const std::span& buffers, + bool hold, const count_handler& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (sending_) + { + deferred_ = std::bind(&socket::transmit, shared_from_this(), + io::to_buffers(buffers), hold, handler); + return; + } + + const auto held = response_.size(); + const auto size = io::buffer_size(buffers); + const auto bound = std::min(minimum_buffer_, response_.max_size()); + if (hold && hold_ && size <= floored_subtract(bound, held)) + { + response_.append(buffers); + io::post(strand_, std::bind(handler, error::success, size)); + return; + } + + io::const_buffers gather{}; + if (!is_zero(held)) + gather.push_back(response_.cdata()); + + gather.insert(gather.end(), buffers.begin(), buffers.end()); + response_.clear(); + sending_ = true; + VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), + gather, std::bind(&socket::handle_transmit, + shared_from_this(), _1, _2, held, handler)); +} + +// protected +// The write is independent of the held writes. +void socket::interject(const std::span& buffers, + const count_handler& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (sending_) + { + deferred_ = std::bind(&socket::interject, shared_from_this(), + io::to_buffers(buffers), handler); + return; + } + + sending_ = true; + VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), + buffers, std::bind(&socket::handle_transmit, + shared_from_this(), _1, _2, zero, handler)); +} + +// private +// The size reported excludes that of the held writes (reported when held). +void socket::handle_transmit(const code& ec, size_t size, size_t held, + const count_handler& handler) NOEXCEPT +{ + BC_ASSERT(stranded()); + + sending_ = false; + const auto next = std::exchange(deferred_, {}); + handler(ec, floored_subtract(size, held)); + + if (next) + next(); +} + // read arbitrary tcp bytes, iterate (up to buffer capacity). void socket::async_read_some(const io::mutable_buffer& buffer, const count_handler& handler) NOEXCEPT diff --git a/src/net/socket_body.cpp b/src/net/socket_body.cpp index 5bbf1dfb8..133b9d591 100644 --- a/src/net/socket_body.cpp +++ b/src/net/socket_body.cpp @@ -94,10 +94,10 @@ void socket::assign_buffer(http::body::value_type& body) NOEXCEPT body.get().buffer = &response_; } -// The buffer is idle here, so its retention is reduced to the minimum. +// The retention of the buffer is reduced to the minimum (unless in use). void socket::reclaim_buffer() NOEXCEPT { - if (response_.capacity() > minimum_buffer_) + if (!sending_ && response_.capacity() > minimum_buffer_) { response_.shrink_to_fit(); response_.reserve(minimum_buffer_); diff --git a/src/net/socket_http.cpp b/src/net/socket_http.cpp index fa369cbfd..3b390cde8 100644 --- a/src/net/socket_http.cpp +++ b/src/net/socket_http.cpp @@ -348,8 +348,8 @@ void socket::do_serialize( return; } - VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), - out->buffers, std::bind(&socket::handle_serialize, + transmit(out->buffers, out->serializer.is_done(), + std::bind(&socket::handle_serialize, shared_from_this(), _1, _2, out, total, handler)); } @@ -516,8 +516,8 @@ void socket::do_http_write_header(const header_state::ptr& out, const io::const_buffer data{ out->header.data(), out->header.size() }; - VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), - data, std::bind(&socket::handle_http_write_header, + transmit({ &data, one }, true, + std::bind(&socket::handle_http_write_header, shared_from_this(), _1, _2, out, handler)); } @@ -594,8 +594,8 @@ void socket::do_chunk_write(code ec, size_t total, io::const_buffer{ http::chunk_end.data(), http::chunk_end.size() } }; - VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), - out->buffers, std::bind(&socket::handle_chunk_write, + transmit(out->buffers, !out->more && !out->last(), + std::bind(&socket::handle_chunk_write, shared_from_this(), _1, _2, total, out, handler)); } @@ -640,8 +640,8 @@ void socket::handle_chunk_write(const code& ec, size_t size, const io::const_buffer last{ http::last_chunk.data(), http::last_chunk.size() }; - VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), - last, std::bind(&socket::handle_chunk_last, + transmit({ &last, one }, true, + std::bind(&socket::handle_chunk_last, shared_from_this(), _1, _2, total, handler)); } diff --git a/src/net/socket_stop.cpp b/src/net/socket_stop.cpp index b08fa4468..2203e2145 100644 --- a/src/net/socket_stop.cpp +++ b/src/net/socket_stop.cpp @@ -94,6 +94,14 @@ void socket::do_lazy_stop() NOEXCEPT timer_->start(std::bind(&socket::handle_lazy_timer, shared_from_this(), _1)); + // Held writes have completed, so they precede the close. + hold_ = false; + if (holding()) + { + release(std::bind(&socket::do_ws_stop, shared_from_this())); + return; + } + do_ws_stop(); } diff --git a/src/net/socket_ws.cpp b/src/net/socket_ws.cpp index b7cad1807..6bf21a496 100644 --- a/src/net/socket_ws.cpp +++ b/src/net/socket_ws.cpp @@ -248,6 +248,7 @@ void socket::ws_send_data(const io::const_buffer& data, bool binary, possible_wide_cast(data.size())); frame->data = data; frame->handler = handler; + frame->finish = finish; ws_queue_.push_back(frame); ws_write_next(); } @@ -263,6 +264,7 @@ void socket::ws_send(ws::opcode code, const data_chunk& payload, possible_wide_cast(payload.size())); frame->payload = payload; frame->handler = handler; + frame->control = true; ws_queue_.push_back(frame); ws_write_next(); } @@ -275,7 +277,10 @@ void socket::ws_pong(const data_chunk& payload) NOEXCEPT const auto queued = !is_null(ws_pong_); if (!queued) + { ws_pong_ = emplace_shared(); + ws_pong_->control = true; + } ws_pong_->header = ws::serialize_header(ws::opcode::pong, true, possible_wide_cast(payload.size())); @@ -309,9 +314,13 @@ void socket::ws_write_next() NOEXCEPT payload }; - VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(), - frame->buffers, std::bind(&socket::handle_ws_frame, - shared_from_this(), _1, _2, frame)); + const count_handler complete = std::bind(&socket::handle_ws_frame, + shared_from_this(), _1, _2, frame); + + if (frame->control) + interject(frame->buffers, complete); + else + transmit(frame->buffers, frame->finish, complete); } // private diff --git a/test/messages/http/header.cpp b/test/messages/http/header.cpp index 1d69ee7ae..8dde96315 100644 --- a/test/messages/http/header.cpp +++ b/test/messages/http/header.cpp @@ -222,4 +222,62 @@ BC_AUTO_TEST_CASE(header__flat_buffer__prepare_commit_consume__expected) BC_REQUIRE_EQUAL(instance.size(), 0u); } +BC_AUTO_TEST_CASE(header__flat_buffer__append__external_buffers__appended_in_order) +{ + const std::string_view first{ "ab" }; + const std::string_view second{ "cd" }; + const std::array buffers{ io::const_buffer{ first.data(), first.size() }, io::const_buffer{ second.data(), second.size() } }; + flat_buffer instance{ 8 }; + instance.append(buffers); + instance.append(buffers); + BC_REQUIRE_EQUAL(std::string(static_cast(instance.data().data()), instance.size()), "abcdabcd"); +} + +BC_AUTO_TEST_CASE(header__flat_buffer__append__writable_buffer_at_start__committed) +{ + flat_buffer instance{ 8 }; + std::memcpy(instance.prepare(2).data(), "ab", 2); + instance.commit(2); + const auto scratch = instance.prepare(3); + std::memcpy(scratch.data(), "cde", 3); + const std::array buffers{ io::const_buffer{ scratch.data(), scratch.size() } }; + instance.append(buffers); + BC_REQUIRE_EQUAL(std::string(static_cast(instance.data().data()), instance.size()), "abcde"); +} + +BC_AUTO_TEST_CASE(header__flat_buffer__append__writable_buffer_between_external__moved_and_reallocated) +{ + const std::string_view head{ "cd" }; + const std::string_view tail{ "h" }; + flat_buffer instance{ 8 }; + std::memcpy(instance.prepare(2).data(), "ab", 2); + instance.commit(2); + const auto scratch = instance.prepare(3); + std::memcpy(scratch.data(), "efg", 3); + BC_REQUIRE_EQUAL(instance.capacity(), 5u); + + const std::array buffers{ io::const_buffer{ head.data(), head.size() }, io::const_buffer{ scratch.data(), scratch.size() }, io::const_buffer{ tail.data(), tail.size() } }; + instance.append(buffers); + BC_REQUIRE_EQUAL(std::string(static_cast(instance.data().data()), instance.size()), "abcdefgh"); +} + +BC_AUTO_TEST_CASE(header__flat_buffer__append__writable_buffer_beyond_start__moved_back) +{ + flat_buffer instance{ 8 }; + const auto scratch = instance.prepare(5); + std::memcpy(scratch.data(), "..abc", 5); + const std::array buffers{ io::const_buffer{ std::next(static_cast(scratch.data()), 2), 3 } }; + instance.append(buffers); + BC_REQUIRE_EQUAL(std::string(static_cast(instance.data().data()), instance.size()), "abc"); +} + +BC_AUTO_TEST_CASE(header__flat_buffer__append__exceeds_limit__throws_unchanged) +{ + const std::string_view data{ "abcd" }; + const std::array buffers{ io::const_buffer{ data.data(), data.size() } }; + flat_buffer instance{ 3 }; + BC_REQUIRE_THROW(instance.append(buffers), std::length_error); + BC_REQUIRE_EQUAL(instance.size(), 0u); +} + BC_AUTO_TEST_SUITE_END() diff --git a/test/messages/json_body_writer.cpp b/test/messages/json_body_writer.cpp index 482c01f35..7c5cec0e0 100644 --- a/test/messages/json_body_writer.cpp +++ b/test/messages/json_body_writer.cpp @@ -166,6 +166,38 @@ BC_AUTO_TEST_CASE(json_body_writer__get__size_hint_smaller_than_payload__four_ch BC_REQUIRE(writer.done()); } +BC_AUTO_TEST_CASE(json_body_writer__get__readable_bytes__retained_and_chunks_bound_by_room) +{ + const std::string_view expected1{ R"({"ke)" }; + const std::string_view expected2{ R"(y":")" }; + const io::const_buffer out1{ expected1.data(), expected1.size() }; + const io::const_buffer out2{ expected2.data(), expected2.size() }; + + http::flat_buffer scratch{ 6 }; + std::memcpy(scratch.prepare(2).data(), "ab", 2); + scratch.commit(2); + json::body<>::value_type body{}; + body.buffer = &scratch; + body.model = object{ { "key", "value" } }; + json::body<>::writer writer(body); + code ec{}; + writer.init(ec); + BC_REQUIRE(!ec); + + const auto buffer1 = writer.get(ec); + BC_REQUIRE(!ec); + BC_REQUIRE(buffer1.has_value()); + BC_REQUIRE(buffer1.value().first == out1); + BC_REQUIRE(buffer1.value().second); + + const auto buffer2 = writer.get(ec); + BC_REQUIRE(!ec); + BC_REQUIRE(buffer2.has_value()); + BC_REQUIRE(buffer2.value().first == out2); + BC_REQUIRE(buffer2.value().second); + BC_REQUIRE_EQUAL(std::string(static_cast(scratch.data().data()), scratch.size()), "ab"); +} + BC_AUTO_TEST_CASE(json_body_writer__init__null_buffer__bad_alloc) { json::body<>::value_type body{}; diff --git a/test/net/socket.cpp b/test/net/socket.cpp index a86a6623e..7689a9177 100644 --- a/test/net/socket.cpp +++ b/test/net/socket.cpp @@ -1056,4 +1056,153 @@ BC_FIXTURE_TEST_CASE(socket__lazy_stop__clear__stopped_client_end_of_file, accep BC_REQUIRE(ec == error::peer_disconnect); } +// hold +// ---------------------------------------------------------------------------- + +BC_FIXTURE_TEST_CASE(socket__holding__default__false, acceptor_fixture) +{ + const auto instance = std::make_shared(log, pool.service(), clear); + const awaiter holding{}; + io::post(instance->strand(), [=]() NOEXCEPT + { + holding.set(instance->holding()); + }); + + BC_REQUIRE(!holding.get()); + instance->stop(); +} + +BC_FIXTURE_TEST_CASE(socket__hold__fits_minimum__completes_held_and_written_with_next, acceptor_fixture) +{ + static const system::data_array<2> first{ 'a', 'b' }; + static const system::data_array<2> second{ 'c', 'd' }; + auto params = clear; + params.minimum_buffer = 64; + const awaiter accepted{}; + const auto instance = accept(params, accepted); + BC_REQUIRE_EQUAL(accepted.get(), error::success); + + const awaiter held{}; + const awaiter written{}; + io::post(instance->strand(), [=]() NOEXCEPT + { + instance->hold(true); + instance->tcp_write({ first.data(), first.size() }, [=](const code& ec, size_t size) NOEXCEPT + { + held.set(!ec && size == first.size() && instance->holding()); + instance->hold(false); + instance->tcp_write({ second.data(), second.size() }, [=](const code& ec, size_t size) NOEXCEPT + { + written.set(!ec && size == second.size() && !instance->holding()); + }); + }); + }); + + BC_REQUIRE(held.get()); + BC_REQUIRE(written.get()); + + code ec{}; + system::data_array<4> received{}; + const system::data_array<4> expected{ 'a', 'b', 'c', 'd' }; + io::read(client, io::buffer(received), ec); + BC_REQUIRE(!ec); + BC_REQUIRE(received == expected); + instance->stop(); +} + +BC_FIXTURE_TEST_CASE(socket__hold__exceeds_minimum__written, acceptor_fixture) +{ + static const system::data_array<2> data{ 'a', 'b' }; + auto params = clear; + params.minimum_buffer = 1; + const awaiter accepted{}; + const auto instance = accept(params, accepted); + BC_REQUIRE_EQUAL(accepted.get(), error::success); + + const awaiter written{}; + io::post(instance->strand(), [=]() NOEXCEPT + { + instance->hold(true); + instance->tcp_write({ data.data(), data.size() }, [=](const code& ec, size_t size) NOEXCEPT + { + written.set(!ec && size == data.size() && !instance->holding()); + }); + }); + + BC_REQUIRE(written.get()); + + code ec{}; + system::data_array<2> received{}; + io::read(client, io::buffer(received), ec); + BC_REQUIRE(!ec); + BC_REQUIRE(received == data); + instance->stop(); +} + +BC_FIXTURE_TEST_CASE(socket__release__held__written, acceptor_fixture) +{ + static const system::data_array<2> data{ 'a', 'b' }; + auto params = clear; + params.minimum_buffer = 64; + const awaiter accepted{}; + const auto instance = accept(params, accepted); + BC_REQUIRE_EQUAL(accepted.get(), error::success); + + const awaiter held{}; + const awaiter released{}; + io::post(instance->strand(), [=]() NOEXCEPT + { + instance->hold(true); + instance->tcp_write({ data.data(), data.size() }, [=](const code& ec, size_t) NOEXCEPT + { + held.set(!ec && instance->holding()); + instance->release([=](const code& ec, size_t size) NOEXCEPT + { + released.set(!ec && is_zero(size) && !instance->holding()); + }); + }); + }); + + BC_REQUIRE(held.get()); + BC_REQUIRE(released.get()); + + code ec{}; + system::data_array<2> received{}; + io::read(client, io::buffer(received), ec); + BC_REQUIRE(!ec); + BC_REQUIRE(received == data); + instance->stop(); +} + +BC_FIXTURE_TEST_CASE(socket__lazy_stop__held__written, acceptor_fixture) +{ + static const system::data_array<2> data{ 'a', 'b' }; + auto params = clear; + params.minimum_buffer = 64; + params.connect_timeout = std::chrono::seconds(10); + const awaiter accepted{}; + const auto instance = accept(params, accepted); + BC_REQUIRE_EQUAL(accepted.get(), error::success); + + const awaiter held{}; + io::post(instance->strand(), [=]() NOEXCEPT + { + instance->hold(true); + instance->tcp_write({ data.data(), data.size() }, [=](const code& ec, size_t) NOEXCEPT + { + held.set(!ec && instance->holding()); + instance->lazy_stop(); + }); + }); + + BC_REQUIRE(held.get()); + + code ec{}; + system::data_array<2> received{}; + io::read(client, io::buffer(received), ec); + BC_REQUIRE(!ec); + BC_REQUIRE(received == data); + instance->stop(); +} + BC_AUTO_TEST_SUITE_END() From a4e82c53ad83997c5d1e9d1c3224d54458e8fab2 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 18:03:08 -0500 Subject: [PATCH 3/6] Make socket write buffer assignment and reclaim private. --- include/bitcoin/network/net/socket.hpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index 1e4ce849c..c2c30e231 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -205,12 +205,6 @@ class BCT_API socket virtual void body_notify(http::request&& notification, count_handler&& handler) NOEXCEPT; - /// Assign the retained json serialization buffer to the message body. - void assign_buffer(http::body::value_type& body) NOEXCEPT; - - /// Reduce the retained json serialization buffer to the minimum. - void reclaim_buffer() NOEXCEPT; - /// Hold writes that fit the minimum buffer for the next write (strand). /// A held write completes when held, before it is written. void hold(bool enable) NOEXCEPT; @@ -689,6 +683,10 @@ class BCT_API socket void handle_chunk_last(const code& ec, size_t size, size_t total, const count_handler& handler) NOEXCEPT; + // buffer + void assign_buffer(http::body::value_type& body) NOEXCEPT; + void reclaim_buffer() NOEXCEPT; + // transport void handle_transmit(const code& ec, size_t size, size_t held, const count_handler& handler) NOEXCEPT; From ef2a2bcd70bd2b89f6c34c532f57e75f75bf0ffd Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 18:10:28 -0500 Subject: [PATCH 4/6] Add minimum write buffer setting, defaulted to the read buffer minimum. --- include/bitcoin/network/settings.hpp | 4 ++++ src/net.cpp | 6 +++--- src/sessions/session_server.cpp | 2 +- src/settings.cpp | 9 +++++++++ test/settings.cpp | 21 +++++++++++++++++++++ 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/include/bitcoin/network/settings.hpp b/include/bitcoin/network/settings.hpp index 006a1ccf2..eed91586a 100644 --- a/include/bitcoin/network/settings.hpp +++ b/include/bitcoin/network/settings.hpp @@ -117,6 +117,7 @@ struct BCT_API settings uint32_t inactivity_minutes{ 10 }; uint32_t expiration_minutes{ 60 }; uint32_t minimum_buffer{ 4 * kilobyte }; + uint32_t minimum_write_buffer{ 4 * kilobyte }; uint32_t maximum_buffer{ 64 * kilobyte }; uint32_t maximum_request{ max_payload }; uint32_t maximum_backlog{ megabyte }; @@ -133,6 +134,9 @@ struct BCT_API settings /// Set the default port of bindings that do not specify a port. virtual void default_ports() NOEXCEPT; + + /// Set the write buffer minimum to the read buffer minimum. + virtual void default_buffers() NOEXCEPT; }; struct BCT_API secure_server diff --git a/src/net.cpp b/src/net.cpp index c8f2910ac..4e214e3b1 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -92,7 +92,7 @@ acceptor::ptr net::create_acceptor(const socket::context& context) NOEXCEPT { .connect_timeout = settings.connect_timeout(settings.inbound), .maximum_request = settings.inbound.maximum_request, - .minimum_buffer = settings.inbound.minimum_buffer, + .minimum_buffer = settings.inbound.minimum_write_buffer, .maximum_buffer = settings.inbound.maximum_buffer, .context = accept }; @@ -111,7 +111,7 @@ acceptor_sam::ptr net::create_acceptor_sam() NOEXCEPT { .connect_timeout = settings.connect_timeout(settings.inbound), .maximum_request = settings.inbound.maximum_request, - .minimum_buffer = settings.inbound.minimum_buffer, + .minimum_buffer = settings.inbound.minimum_write_buffer, .maximum_buffer = settings.inbound.maximum_buffer, .context = privacy_ ? settings.peer_context() : socket::context{} }; @@ -128,7 +128,7 @@ connector::ptr net::to_connector(const settings::socks5& socks, { .connect_timeout = connect_timeout, .maximum_request = options.maximum_request, - .minimum_buffer = options.minimum_buffer, + .minimum_buffer = options.minimum_write_buffer, .maximum_buffer = options.maximum_buffer, .bind = options.binding(slot), .slot = slot diff --git a/src/sessions/session_server.cpp b/src/sessions/session_server.cpp index 21817fa51..f7a9e4e33 100644 --- a/src/sessions/session_server.cpp +++ b/src/sessions/session_server.cpp @@ -114,7 +114,7 @@ code session_server::do_accept(const config::authorities& binds, { .connect_timeout = network_settings().connect_timeout(options_), .maximum_request = options_.maximum_request, - .minimum_buffer = options_.minimum_buffer, + .minimum_buffer = options_.minimum_write_buffer, .maximum_buffer = options_.maximum_buffer, .context = context, .role = role() diff --git a/src/settings.cpp b/src/settings.cpp index 00601e9ab..1f279cef3 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -215,6 +215,11 @@ void settings::tcp_server::default_ports() NOEXCEPT set_default_port(binds, bind_port); } +void settings::tcp_server::default_buffers() NOEXCEPT +{ + minimum_write_buffer = minimum_buffer; +} + // secure_server // ---------------------------------------------------------------------------- @@ -534,6 +539,10 @@ code settings::initialize() NOEXCEPT // configured without a port matches its address on any port. manual.initialize(); + inbound.default_buffers(); + outbound.default_buffers(); + manual.default_buffers(); + const auto port = inbound.bind_port; inbound.default_ports(); set_default_port(inbound.selfs, port); diff --git a/test/settings.cpp b/test/settings.cpp index 6fab2a311..8a85d3497 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -384,6 +384,7 @@ BC_AUTO_TEST_CASE(settings__tcp_server__defaults__expected) BC_REQUIRE_EQUAL(instance.expiration_minutes, 60u); BC_REQUIRE_EQUAL(instance.maximum_request, maximum_request); BC_REQUIRE_EQUAL(instance.minimum_buffer, minimum_buffer); + BC_REQUIRE_EQUAL(instance.minimum_write_buffer, minimum_buffer); BC_REQUIRE_EQUAL(instance.maximum_buffer, 64 * kilobyte); BC_REQUIRE_EQUAL(instance.maximum_backlog, megabyte); BC_REQUIRE_EQUAL(instance.connect_timeout_seconds, 0u); @@ -438,6 +439,26 @@ BC_AUTO_TEST_CASE(settings__tcp_server_default_ports__nonzero_port__unchanged) BC_REQUIRE_EQUAL(instance.binds.at(0).port(), 24u); } +BC_AUTO_TEST_CASE(settings__tcp_server_default_buffers__minimum_buffer__minimum_write_buffer) +{ + settings::tcp_server instance{ "test" }; + instance.minimum_buffer = 42; + instance.default_buffers(); + BC_REQUIRE_EQUAL(instance.minimum_write_buffer, 42u); +} + +BC_AUTO_TEST_CASE(settings__initialize__minimum_buffers__minimum_write_buffers) +{ + settings instance{ system::chain::selection::mainnet }; + instance.inbound.minimum_buffer = 1; + instance.outbound.minimum_buffer = 2; + instance.manual.minimum_buffer = 3; + BC_REQUIRE_EQUAL(instance.initialize(), error::success); + BC_REQUIRE_EQUAL(instance.inbound.minimum_write_buffer, 1u); + BC_REQUIRE_EQUAL(instance.outbound.minimum_write_buffer, 2u); + BC_REQUIRE_EQUAL(instance.manual.minimum_write_buffer, 3u); +} + BC_AUTO_TEST_CASE(settings__secure_server_default_ports__zero_ports__bind_and_safe_ports) { settings::secure_server instance{ "test" }; From 719461cbefb2ce529882e7a3b5b1c032ac335821 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 18:42:57 -0500 Subject: [PATCH 5/6] Regenerate artifacts. --- .github/workflows/ci-expanded.yml | 30 ++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/.github/workflows/ci-expanded.yml b/.github/workflows/ci-expanded.yml index fa525c232..f10a371d0 100644 --- a/.github/workflows/ci-expanded.yml +++ b/.github/workflows/ci-expanded.yml @@ -508,6 +508,16 @@ jobs: if: ${{ startsWith(matrix.image, 'windows') }} uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0 + - name: Prefer Visual Studio LLVM [windows] + if: ${{ startsWith(matrix.image, 'windows') }} + shell: pwsh + run: | + $arch = if ($env:RUNNER_ARCH -eq 'ARM64') { 'ARM64' } else { 'x64' } + $llvm = Join-Path $env:VCINSTALLDIR Tools Llvm $arch bin + if (-not (Test-Path (Join-Path $llvm 'clang-cl.exe'))) { throw "clang-cl not found in $llvm." } + Add-Content -Path $env:GITHUB_PATH -Value $llvm + & (Join-Path $llvm 'clang-cl.exe') --version + - name: Environmental Computation shell: bash run: | @@ -1157,6 +1167,16 @@ jobs: if: ${{ startsWith(matrix.image, 'windows') }} uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0 + - name: Prefer Visual Studio LLVM [windows] + if: ${{ startsWith(matrix.image, 'windows') }} + shell: pwsh + run: | + $arch = if ($env:RUNNER_ARCH -eq 'ARM64') { 'ARM64' } else { 'x64' } + $llvm = Join-Path $env:VCINSTALLDIR Tools Llvm $arch bin + if (-not (Test-Path (Join-Path $llvm 'clang-cl.exe'))) { throw "clang-cl not found in $llvm." } + Add-Content -Path $env:GITHUB_PATH -Value $llvm + & (Join-Path $llvm 'clang-cl.exe') --version + - name: Environmental Computation shell: bash run: | @@ -1571,6 +1591,16 @@ jobs: if: ${{ startsWith(matrix.image, 'windows') }} uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0 + - name: Prefer Visual Studio LLVM [windows] + if: ${{ startsWith(matrix.image, 'windows') }} + shell: pwsh + run: | + $arch = if ($env:RUNNER_ARCH -eq 'ARM64') { 'ARM64' } else { 'x64' } + $llvm = Join-Path $env:VCINSTALLDIR Tools Llvm $arch bin + if (-not (Test-Path (Join-Path $llvm 'clang-cl.exe'))) { throw "clang-cl not found in $llvm." } + Add-Content -Path $env:GITHUB_PATH -Value $llvm + & (Join-Path $llvm 'clang-cl.exe') --version + - name: Environmental Computation shell: bash run: | diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c6e5cbe60..20a45589c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -256,6 +256,16 @@ jobs: if: ${{ startsWith(matrix.image, 'windows') }} uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0 + - name: Prefer Visual Studio LLVM [windows] + if: ${{ startsWith(matrix.image, 'windows') }} + shell: pwsh + run: | + $arch = if ($env:RUNNER_ARCH -eq 'ARM64') { 'ARM64' } else { 'x64' } + $llvm = Join-Path $env:VCINSTALLDIR Tools Llvm $arch bin + if (-not (Test-Path (Join-Path $llvm 'clang-cl.exe'))) { throw "clang-cl not found in $llvm." } + Add-Content -Path $env:GITHUB_PATH -Value $llvm + & (Join-Path $llvm 'clang-cl.exe') --version + - name: Environmental Computation shell: bash run: | @@ -450,6 +460,16 @@ jobs: if: ${{ startsWith(matrix.image, 'windows') }} uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0 + - name: Prefer Visual Studio LLVM [windows] + if: ${{ startsWith(matrix.image, 'windows') }} + shell: pwsh + run: | + $arch = if ($env:RUNNER_ARCH -eq 'ARM64') { 'ARM64' } else { 'x64' } + $llvm = Join-Path $env:VCINSTALLDIR Tools Llvm $arch bin + if (-not (Test-Path (Join-Path $llvm 'clang-cl.exe'))) { throw "clang-cl not found in $llvm." } + Add-Content -Path $env:GITHUB_PATH -Value $llvm + & (Join-Path $llvm 'clang-cl.exe') --version + - name: Environmental Computation shell: bash run: | @@ -636,6 +656,16 @@ jobs: if: ${{ startsWith(matrix.image, 'windows') }} uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0 + - name: Prefer Visual Studio LLVM [windows] + if: ${{ startsWith(matrix.image, 'windows') }} + shell: pwsh + run: | + $arch = if ($env:RUNNER_ARCH -eq 'ARM64') { 'ARM64' } else { 'x64' } + $llvm = Join-Path $env:VCINSTALLDIR Tools Llvm $arch bin + if (-not (Test-Path (Join-Path $llvm 'clang-cl.exe'))) { throw "clang-cl not found in $llvm." } + Add-Content -Path $env:GITHUB_PATH -Value $llvm + & (Join-Path $llvm 'clang-cl.exe') --version + - name: Environmental Computation shell: bash run: | From f0efde4c80643599edf1e14e5077f4ef184ed06c Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Wed, 18 Nov 2026 18:44:38 -0500 Subject: [PATCH 6/6] Rename minimum_write_buffer to minimum_response. --- include/bitcoin/network/settings.hpp | 4 ++-- src/net.cpp | 6 +++--- src/sessions/session_server.cpp | 2 +- src/settings.cpp | 2 +- test/settings.cpp | 14 +++++++------- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/bitcoin/network/settings.hpp b/include/bitcoin/network/settings.hpp index eed91586a..58aef5ab4 100644 --- a/include/bitcoin/network/settings.hpp +++ b/include/bitcoin/network/settings.hpp @@ -117,7 +117,7 @@ struct BCT_API settings uint32_t inactivity_minutes{ 10 }; uint32_t expiration_minutes{ 60 }; uint32_t minimum_buffer{ 4 * kilobyte }; - uint32_t minimum_write_buffer{ 4 * kilobyte }; + uint32_t minimum_response{ 4 * kilobyte }; uint32_t maximum_buffer{ 64 * kilobyte }; uint32_t maximum_request{ max_payload }; uint32_t maximum_backlog{ megabyte }; @@ -135,7 +135,7 @@ struct BCT_API settings /// Set the default port of bindings that do not specify a port. virtual void default_ports() NOEXCEPT; - /// Set the write buffer minimum to the read buffer minimum. + /// Set the minimum response buffer to the minimum (request) buffer. virtual void default_buffers() NOEXCEPT; }; diff --git a/src/net.cpp b/src/net.cpp index 4e214e3b1..286dcabec 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -92,7 +92,7 @@ acceptor::ptr net::create_acceptor(const socket::context& context) NOEXCEPT { .connect_timeout = settings.connect_timeout(settings.inbound), .maximum_request = settings.inbound.maximum_request, - .minimum_buffer = settings.inbound.minimum_write_buffer, + .minimum_buffer = settings.inbound.minimum_response, .maximum_buffer = settings.inbound.maximum_buffer, .context = accept }; @@ -111,7 +111,7 @@ acceptor_sam::ptr net::create_acceptor_sam() NOEXCEPT { .connect_timeout = settings.connect_timeout(settings.inbound), .maximum_request = settings.inbound.maximum_request, - .minimum_buffer = settings.inbound.minimum_write_buffer, + .minimum_buffer = settings.inbound.minimum_response, .maximum_buffer = settings.inbound.maximum_buffer, .context = privacy_ ? settings.peer_context() : socket::context{} }; @@ -128,7 +128,7 @@ connector::ptr net::to_connector(const settings::socks5& socks, { .connect_timeout = connect_timeout, .maximum_request = options.maximum_request, - .minimum_buffer = options.minimum_write_buffer, + .minimum_buffer = options.minimum_response, .maximum_buffer = options.maximum_buffer, .bind = options.binding(slot), .slot = slot diff --git a/src/sessions/session_server.cpp b/src/sessions/session_server.cpp index f7a9e4e33..04a971982 100644 --- a/src/sessions/session_server.cpp +++ b/src/sessions/session_server.cpp @@ -114,7 +114,7 @@ code session_server::do_accept(const config::authorities& binds, { .connect_timeout = network_settings().connect_timeout(options_), .maximum_request = options_.maximum_request, - .minimum_buffer = options_.minimum_write_buffer, + .minimum_buffer = options_.minimum_response, .maximum_buffer = options_.maximum_buffer, .context = context, .role = role() diff --git a/src/settings.cpp b/src/settings.cpp index 1f279cef3..7e6df3feb 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -217,7 +217,7 @@ void settings::tcp_server::default_ports() NOEXCEPT void settings::tcp_server::default_buffers() NOEXCEPT { - minimum_write_buffer = minimum_buffer; + minimum_response = minimum_buffer; } // secure_server diff --git a/test/settings.cpp b/test/settings.cpp index 8a85d3497..b6a22bbbe 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -384,7 +384,7 @@ BC_AUTO_TEST_CASE(settings__tcp_server__defaults__expected) BC_REQUIRE_EQUAL(instance.expiration_minutes, 60u); BC_REQUIRE_EQUAL(instance.maximum_request, maximum_request); BC_REQUIRE_EQUAL(instance.minimum_buffer, minimum_buffer); - BC_REQUIRE_EQUAL(instance.minimum_write_buffer, minimum_buffer); + BC_REQUIRE_EQUAL(instance.minimum_response, minimum_buffer); BC_REQUIRE_EQUAL(instance.maximum_buffer, 64 * kilobyte); BC_REQUIRE_EQUAL(instance.maximum_backlog, megabyte); BC_REQUIRE_EQUAL(instance.connect_timeout_seconds, 0u); @@ -439,24 +439,24 @@ BC_AUTO_TEST_CASE(settings__tcp_server_default_ports__nonzero_port__unchanged) BC_REQUIRE_EQUAL(instance.binds.at(0).port(), 24u); } -BC_AUTO_TEST_CASE(settings__tcp_server_default_buffers__minimum_buffer__minimum_write_buffer) +BC_AUTO_TEST_CASE(settings__tcp_server_default_buffers__minimum_buffer__minimum_response) { settings::tcp_server instance{ "test" }; instance.minimum_buffer = 42; instance.default_buffers(); - BC_REQUIRE_EQUAL(instance.minimum_write_buffer, 42u); + BC_REQUIRE_EQUAL(instance.minimum_response, 42u); } -BC_AUTO_TEST_CASE(settings__initialize__minimum_buffers__minimum_write_buffers) +BC_AUTO_TEST_CASE(settings__initialize__minimum_buffers__minimum_responses) { settings instance{ system::chain::selection::mainnet }; instance.inbound.minimum_buffer = 1; instance.outbound.minimum_buffer = 2; instance.manual.minimum_buffer = 3; BC_REQUIRE_EQUAL(instance.initialize(), error::success); - BC_REQUIRE_EQUAL(instance.inbound.minimum_write_buffer, 1u); - BC_REQUIRE_EQUAL(instance.outbound.minimum_write_buffer, 2u); - BC_REQUIRE_EQUAL(instance.manual.minimum_write_buffer, 3u); + BC_REQUIRE_EQUAL(instance.inbound.minimum_response, 1u); + BC_REQUIRE_EQUAL(instance.outbound.minimum_response, 2u); + BC_REQUIRE_EQUAL(instance.manual.minimum_response, 3u); } BC_AUTO_TEST_CASE(settings__secure_server_default_ports__zero_ports__bind_and_safe_ports)