Skip to content

Commit 6f3f180

Browse files
committed
Fix stall issues with p2p
1 parent cca5425 commit 6f3f180

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

contrib/epee/include/net/abstract_tcp_server2.inl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ namespace net_utils
826826
return false;
827827

828828
// Wait for the write queue to fall below the max. If it doesn't after a
829-
// randomized delay, drop the connection.
829+
// randomized delay, drop the connection. P2P senders fail fast instead of
830+
// parking an io_context worker thread here.
830831
auto wait_consume = [this] {
831832
auto random_delay = []{
832833
using engine = std::mt19937;
@@ -850,6 +851,12 @@ namespace net_utils
850851
if (m_state.data.write.queue.size() <= ABSTRACT_SERVER_SEND_QUE_MAX_COUNT &&
851852
m_state.data.write.total_bytes <= static_cast<shared_state&>(connection_basic::get_state()).response_soft_limit)
852853
return true;
854+
855+
if (m_connection_type == e_connection_type_P2P) {
856+
MWARNING("Connection " << m_conn_context.m_connection_id << " tripped write limit, terminating");
857+
terminate_async();
858+
return false;
859+
}
853860
m_state.data.write.wait_consume = true;
854861
bool success = m_state.condition.wait_for(
855862
m_state.lock,
@@ -888,7 +895,11 @@ namespace net_utils
888895
};
889896
if (!wait_sender())
890897
return false;
891-
constexpr size_t CHUNK_SIZE = 32 * 1024;
898+
/* CHUNK_SIZE indirectly caps outgoing to 128 * 1024 * 1000
899+
(ABSTRACT_SERVER_SEND_QUE_MAX_COUNT). The "soft" limit total is currently
900+
100 MiB (ABSTRACT_SERVER_SEND_QUE_MAX_BYTES_DEFAULT). These values will
901+
need to be re-visited alongside block limit increases. */
902+
constexpr size_t CHUNK_SIZE = 128 * 1024;
892903
if (m_connection_type == e_connection_type_RPC ||
893904
message.size() <= 2 * CHUNK_SIZE
894905
) {
@@ -900,13 +911,19 @@ namespace net_utils
900911
start_write();
901912
}
902913
else {
914+
std::size_t soft_limit = 0;
915+
const epee::misc_utils::auto_scope_leave_caller scope_exit_handler =
916+
epee::misc_utils::create_scope_leave_handler([&soft_limit, this] {
917+
m_state.data.write.total_bytes += soft_limit;
918+
});
919+
903920
while (!message.empty()) {
904921
if (!wait_consume())
905922
return false;
906923
m_state.data.write.queue.emplace_front(
907924
message.take_slice(CHUNK_SIZE)
908925
);
909-
m_state.data.write.total_bytes += m_state.data.write.queue.front().size();
926+
soft_limit += m_state.data.write.queue.front().size();
910927
start_write();
911928
}
912929
}

0 commit comments

Comments
 (0)