Commit 74732f50 authored by jan.koester's avatar jan.koester
Browse files

test

parent 7c43b073
Loading
Loading
Loading
Loading
+61 −17
Changes for src/quic.cpp: 61 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -2383,6 +2383,14 @@ void quic::completeHandshake() {
    const auto timeout = std::chrono::milliseconds(1500);

    while (!_handshake_complete) {
        // Bail out if the connection was closed while we were waiting
        if (_conn_state.load() == ConnectionState::Closed ||
            _conn_state.load() == ConnectionState::Draining) {
            NetException e;
            e[NetException::Error] << "quic::completeHandshake: connection closed";
            throw e;
        }

        try {
            buffer recv_buf_wrapper(65535);
            size_t recv_len = udp::recvData(recv_buf_wrapper, MSG_DONTWAIT);
@@ -3457,7 +3465,9 @@ void quic::checkLossAndRetransmit() {

        ssize_t sent = sendPacket(packet.data(), packet.size());
        if (sent < 0) {
            std::this_thread::sleep_for(std::chrono::microseconds(500));
            // Single non-blocking retry — do not sleep with quic_mtx held.
            // If the socket is still busy the packet will be retransmitted
            // on the next loss-detection cycle.
            sent = sendPacket(packet.data(), packet.size());
        }

@@ -3479,7 +3489,7 @@ void quic::checkLossAndRetransmit() {
// ============================================================================

void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data, bool fin) {
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    std::unique_lock<std::recursive_mutex> lock(quic_mtx());
    QUIC_DBG("sendStreamData: sid=%lu data_len=%zu fin=%d", (unsigned long)stream_id, data.size(), (int)fin);

    if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) {
@@ -3506,8 +3516,10 @@ void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data,

    size_t buf_offset = 0;        // how much of 'data' we've sent so far
    int fc_stalls = 0;
    // 60s max stall — large transfers need time for client FC updates
    static constexpr int MAX_FC_STALLS = 60000;
    // 5s max stall — avoids blocking the mutex for too long while
    // waiting for client FC updates.  Previously 60s which could
    // starve all other QUIC connections sharing the same listener.
    static constexpr int MAX_FC_STALLS = 5000;
    bool blocked_sent = false;
    size_t pkt_counter = 0;       // packets sent since last pump/pace

@@ -3518,6 +3530,12 @@ void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data,
    static constexpr size_t PACE_INTERVAL = 10;

    while (buf_offset < data.size()) {
        // Bail out early if the connection was closed/draining while we waited
        if (_conn_state.load() == ConnectionState::Closed ||
            _conn_state.load() == ConnectionState::Draining) {
            break;
        }

        size_t remaining = data.size() - buf_offset;

        // Connection-level flow control
@@ -3553,7 +3571,18 @@ void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data,

            // Process incoming datagrams to receive MAX_DATA / MAX_STREAM_DATA
            pumpIncoming();

            // Release the mutex during sleep so other connections
            // sharing this listener are not starved.
            lock.unlock();
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
            lock.lock();

            // Re-check connection state after re-acquiring lock
            if (_conn_state.load() == ConnectionState::Closed ||
                _conn_state.load() == ConnectionState::Draining) {
                break;
            }
            continue;
        }

@@ -3567,7 +3596,18 @@ void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data,
            // yield briefly so the client can process data and update FC.
            if (++pkt_counter % PACE_INTERVAL == 0) {
                pumpIncoming();

                // Release the mutex during sleep so other connections
                // sharing this listener are not starved.
                lock.unlock();
                std::this_thread::sleep_for(std::chrono::microseconds(200));
                lock.lock();

                // Re-check connection state after re-acquiring lock
                if (_conn_state.load() == ConnectionState::Closed ||
                    _conn_state.load() == ConnectionState::Draining) {
                    goto done;
                }

                // Re-check FC limits — pumpIncoming may have raised them,
                // allowing us to send more than the original 'remaining'.
@@ -3613,9 +3653,17 @@ void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data,

            ssize_t sent = sendPacket(packet.data(), packet.size());
            if (sent < 0) {
                for (int retry = 0; retry < 10 && sent < 0; ++retry) {
                // Retry with brief pauses.  Release the mutex during
                // sleep to avoid blocking other QUIC connections.
                for (int retry = 0; retry < 5 && sent < 0; ++retry) {
                    lock.unlock();
                    std::this_thread::sleep_for(
                        std::chrono::microseconds(100 * (1 << retry)));
                    lock.lock();
                    if (_conn_state.load() == ConnectionState::Closed ||
                        _conn_state.load() == ConnectionState::Draining) {
                        goto done;
                    }
                    sent = sendPacket(packet.data(), packet.size());
                }
                if (sent < 0) goto done;
@@ -3657,8 +3705,8 @@ void quic::sendMaxStreamData(uint64_t stream_id, uint64_t max_data) {
    applyHeaderProtection(packet, hp_key);
    ssize_t sent = sendPacket(packet.data(), packet.size());
    if (sent < 0) {
        // Flow control frames are critical — retry once
        std::this_thread::sleep_for(std::chrono::microseconds(100));
        // Flow control frames are critical — immediate non-blocking retry.
        // Do not sleep here: caller typically holds quic_mtx().
        sendPacket(packet.data(), packet.size());
    }
}
@@ -3679,8 +3727,8 @@ void quic::sendMaxData(uint64_t max_data) {
    applyHeaderProtection(packet, hp_key);
    ssize_t sent = sendPacket(packet.data(), packet.size());
    if (sent < 0) {
        // Flow control frames are critical — retry once
        std::this_thread::sleep_for(std::chrono::microseconds(100));
        // Flow control frames are critical — immediate non-blocking retry.
        // Do not sleep here: caller typically holds quic_mtx().
        sendPacket(packet.data(), packet.size());
    }
}
@@ -4442,11 +4490,9 @@ size_t quic::sendData(buffer& data, int flags) {
        // Send packet
        ssize_t ret = sendPacket(packet.data(), packet.size());
        if (ret < 0) {
            for (int retry = 0; retry < 10 && ret < 0; ++retry) {
                std::this_thread::sleep_for(
                    std::chrono::microseconds(100 * (1 << retry)));
            // Non-blocking retry — do not sleep with quic_mtx held.
            // Lost packets will be retransmitted by the loss detection.
            ret = sendPacket(packet.data(), packet.size());
            }
            if (ret < 0) break;
        }

@@ -4520,11 +4566,9 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,

        ssize_t ret = sendPacket(packet.data(), packet.size());
        if (ret < 0) {
            for (int retry = 0; retry < 10 && ret < 0; ++retry) {
                std::this_thread::sleep_for(
                    std::chrono::microseconds(100 * (1 << retry)));
            // Non-blocking retry — do not sleep with quic_mtx held.
            // Lost packets will be retransmitted by the loss detection.
            ret = sendPacket(packet.data(), packet.size());
            }
            if (ret < 0) break;
        }