|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Davide Faconti |
| 3 | + * |
| 4 | + * This file is part of pj_bridge. |
| 5 | + * |
| 6 | + * pj_bridge is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * pj_bridge is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with pj_bridge. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <cstddef> |
| 23 | +#include <optional> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "pj_bridge/middleware/middleware_interface.hpp" |
| 27 | + |
| 28 | +namespace pj_bridge { |
| 29 | + |
| 30 | +/// Outcome of run_backpressure() for one outgoing frame. The disposition is a |
| 31 | +/// single discriminant (no contradictory flag combinations are representable); |
| 32 | +/// the counters are companion data. |
| 33 | +struct SendOutcome { |
| 34 | + SendResult result = SendResult::kClientGone; ///< how the current frame was handled |
| 35 | + size_t frames_flushed = 0; ///< backlog frames flushed to the socket this call |
| 36 | + size_t dropped = 0; ///< backlog frames evicted on overflow (only when result == kQueued) |
| 37 | +}; |
| 38 | + |
| 39 | +/// Socket-agnostic backpressure policy shared by the send path. The caller |
| 40 | +/// injects the socket/queue primitives (as callables — templated to avoid |
| 41 | +/// std::function type-erasure on the hot path) so the policy is unit-testable |
| 42 | +/// without a real connection: |
| 43 | +/// - @p buffered_amount : `size_t()` — current socket buffer bytes |
| 44 | +/// - @p pop_pending : `std::optional<vector<uint8_t>>()` — pop oldest queued frame (nullopt if empty) |
| 45 | +/// - @p send : `bool(const vector<uint8_t>&)` — transmit a frame; false = client gone |
| 46 | +/// - @p queue_pending : `std::optional<size_t>(const vector<uint8_t>&)` — enqueue (drop-oldest), |
| 47 | +/// returns #dropped, or nullopt if the client is gone |
| 48 | +/// |
| 49 | +/// Policy: first flush the backlog while the socket has room (re-checking the |
| 50 | +/// watermark each iteration so an already-congested socket is never fed |
| 51 | +/// further, and never flushing more than @p max_flush frames so a concurrent |
| 52 | +/// producer cannot make one call flush forever); then handle the current |
| 53 | +/// frame — send it if there is room (kDelivered), else drop a kHeavy frame |
| 54 | +/// before transmit (kShed) or enqueue a kNormal frame (kQueued). A vanished |
| 55 | +/// client surfaces as kClientGone. |
| 56 | +/// |
| 57 | +/// @param max_flush upper bound on frames flushed this call — pass the backlog |
| 58 | +/// size observed at call start. |
| 59 | +template <class BufferedAmount, class PopPending, class Send, class QueuePending> |
| 60 | +SendOutcome run_backpressure( |
| 61 | + FramePriority priority, const std::vector<uint8_t>& frame, size_t watermark, size_t max_flush, |
| 62 | + const BufferedAmount& buffered_amount, const PopPending& pop_pending, const Send& send, |
| 63 | + const QueuePending& queue_pending) { |
| 64 | + SendOutcome out; |
| 65 | + |
| 66 | + // Flush queued frames to the socket, re-checking the watermark each iteration |
| 67 | + // so a socket that fills up mid-flush is never fed further (the flush-recheck |
| 68 | + // fix: the old loop computed the flush count once and could dump a burst of |
| 69 | + // stale frames onto an already-congested socket). Bounded by max_flush so a |
| 70 | + // producer enqueueing concurrently cannot keep this single call flushing. |
| 71 | + while (out.frames_flushed < max_flush && buffered_amount() < watermark) { |
| 72 | + std::optional<std::vector<uint8_t>> queued = pop_pending(); |
| 73 | + if (!queued) { |
| 74 | + break; |
| 75 | + } |
| 76 | + if (!send(*queued)) { |
| 77 | + out.result = SendResult::kClientGone; |
| 78 | + return out; |
| 79 | + } |
| 80 | + out.frames_flushed++; |
| 81 | + } |
| 82 | + |
| 83 | + // Handle the current frame against the live socket buffer. |
| 84 | + if (buffered_amount() < watermark) { |
| 85 | + out.result = send(frame) ? SendResult::kDelivered : SendResult::kClientGone; |
| 86 | + return out; |
| 87 | + } |
| 88 | + |
| 89 | + // Socket congested: shed heavy frames before transmit; queue normal frames. |
| 90 | + if (priority == FramePriority::kHeavy) { |
| 91 | + out.result = SendResult::kShed; |
| 92 | + return out; |
| 93 | + } |
| 94 | + |
| 95 | + std::optional<size_t> dropped = queue_pending(frame); |
| 96 | + if (!dropped) { |
| 97 | + out.result = SendResult::kClientGone; |
| 98 | + return out; |
| 99 | + } |
| 100 | + out.dropped = *dropped; |
| 101 | + out.result = SendResult::kQueued; |
| 102 | + return out; |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace pj_bridge |
0 commit comments