Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::quic::flow_controller Class Reference

Connection-level flow control for QUIC (RFC 9000 Section 4) More...

#include <flow_control.h>

Collaboration diagram for kcenon::network::protocols::quic::flow_controller:
Collaboration graph

Public Member Functions

 flow_controller (uint64_t initial_window=1048576)
 Construct a flow controller.
 
 ~flow_controller ()=default
 
 flow_controller (const flow_controller &)=default
 
auto operator= (const flow_controller &) -> flow_controller &=default
 
 flow_controller (flow_controller &&) noexcept=default
 
auto operator= (flow_controller &&) noexcept -> flow_controller &=default
 
auto available_send_window () const noexcept -> uint64_t
 Get available send window.
 
auto consume_send_window (uint64_t bytes) -> VoidResult
 Try to consume send window for outgoing data.
 
void update_send_limit (uint64_t max_data)
 Update the send limit (from peer's MAX_DATA)
 
auto is_send_blocked () const noexcept -> bool
 Check if send is blocked.
 
auto send_limit () const noexcept -> uint64_t
 Get the current send limit (peer's MAX_DATA)
 
auto bytes_sent () const noexcept -> uint64_t
 Get total bytes sent.
 
auto record_received (uint64_t bytes) -> VoidResult
 Record received data.
 
void record_consumed (uint64_t bytes)
 Record data consumed by application.
 
auto receive_limit () const noexcept -> uint64_t
 Get our receive limit (advertised as MAX_DATA)
 
auto bytes_received () const noexcept -> uint64_t
 Get total bytes received.
 
auto bytes_consumed () const noexcept -> uint64_t
 Get bytes consumed by application.
 
auto should_send_max_data () const noexcept -> bool
 Check if MAX_DATA frame should be sent.
 
auto generate_max_data () -> std::optional< uint64_t >
 Generate MAX_DATA value if update needed.
 
auto should_send_data_blocked () const noexcept -> bool
 Check if DATA_BLOCKED frame should be sent.
 
void mark_data_blocked_sent ()
 Mark DATA_BLOCKED as sent (to avoid repeated sending)
 
void set_window_size (uint64_t window)
 Set the window size for auto-tuning.
 
auto window_size () const noexcept -> uint64_t
 Get the current window size.
 
void set_update_threshold (double threshold)
 Set the threshold for sending MAX_DATA updates (0.0 - 1.0)
 
void reset (uint64_t initial_window)
 Reset flow controller state.
 

Private Attributes

uint64_t send_limit_ {0}
 
uint64_t bytes_sent_ {0}
 
bool data_blocked_sent_ {false}
 
uint64_t receive_limit_ {0}
 
uint64_t bytes_received_ {0}
 
uint64_t bytes_consumed_ {0}
 
uint64_t window_size_
 
double update_threshold_ {0.5}
 
uint64_t last_sent_max_data_ {0}
 

Detailed Description

Connection-level flow control for QUIC (RFC 9000 Section 4)

QUIC provides connection-level and stream-level flow control. This class handles connection-level flow control, tracking the total amount of data that can be sent/received across all streams.

Definition at line 33 of file flow_control.h.

Constructor & Destructor Documentation

◆ flow_controller() [1/3]

kcenon::network::protocols::quic::flow_controller::flow_controller ( uint64_t initial_window = 1048576)
explicit

Construct a flow controller.

Parameters
initial_windowInitial window size (from transport parameters)

Definition at line 12 of file flow_control.cpp.

◆ ~flow_controller()

kcenon::network::protocols::quic::flow_controller::~flow_controller ( )
default

◆ flow_controller() [2/3]

kcenon::network::protocols::quic::flow_controller::flow_controller ( const flow_controller & )
default

◆ flow_controller() [3/3]

kcenon::network::protocols::quic::flow_controller::flow_controller ( flow_controller && )
defaultnoexcept

Member Function Documentation

◆ available_send_window()

auto kcenon::network::protocols::quic::flow_controller::available_send_window ( ) const -> uint64_t
nodiscardnoexcept

Get available send window.

Returns
Number of bytes we can send

Definition at line 24 of file flow_control.cpp.

25{
26 if (bytes_sent_ >= send_limit_) {
27 return 0;
28 }
29 return send_limit_ - bytes_sent_;
30}

References bytes_sent_, and send_limit_.

◆ bytes_consumed()

auto kcenon::network::protocols::quic::flow_controller::bytes_consumed ( ) const -> uint64_t
inlinenodiscardnoexcept

Get bytes consumed by application.

Definition at line 120 of file flow_control.h.

References bytes_consumed_.

◆ bytes_received()

auto kcenon::network::protocols::quic::flow_controller::bytes_received ( ) const -> uint64_t
inlinenodiscardnoexcept

Get total bytes received.

Definition at line 115 of file flow_control.h.

References bytes_received_.

◆ bytes_sent()

auto kcenon::network::protocols::quic::flow_controller::bytes_sent ( ) const -> uint64_t
inlinenodiscardnoexcept

Get total bytes sent.

Definition at line 88 of file flow_control.h.

88{ return bytes_sent_; }

References bytes_sent_.

◆ consume_send_window()

auto kcenon::network::protocols::quic::flow_controller::consume_send_window ( uint64_t bytes) -> VoidResult
nodiscard

Try to consume send window for outgoing data.

Parameters
bytesNumber of bytes to consume
Returns
Success or error if blocked

Definition at line 32 of file flow_control.cpp.

33{
34 if (bytes == 0) {
35 return ok();
36 }
37
38 const uint64_t available = available_send_window();
39 if (bytes > available) {
41 "Send window exhausted",
42 "quic::flow_controller");
43 }
44
45 bytes_sent_ += bytes;
46 data_blocked_sent_ = false; // Reset blocked flag on successful send
47 return ok();
48}
auto available_send_window() const noexcept -> uint64_t
Get available send window.
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()

References kcenon::network::error_void(), kcenon::network::ok(), and kcenon::network::protocols::quic::flow_control_error::send_blocked.

Here is the call graph for this function:

◆ generate_max_data()

auto kcenon::network::protocols::quic::flow_controller::generate_max_data ( ) -> std::optional<uint64_t>
nodiscard

Generate MAX_DATA value if update needed.

Returns
New MAX_DATA value or nullopt

Definition at line 109 of file flow_control.cpp.

110{
111 if (!should_send_max_data()) {
112 return std::nullopt;
113 }
114
115 // New limit is current consumption point plus window size
116 const uint64_t new_limit = bytes_consumed_ + window_size_;
117
118 // Only send if it's an increase
119 if (new_limit <= last_sent_max_data_) {
120 return std::nullopt;
121 }
122
123 receive_limit_ = new_limit;
124 last_sent_max_data_ = new_limit;
125 return new_limit;
126}
auto should_send_max_data() const noexcept -> bool
Check if MAX_DATA frame should be sent.

◆ is_send_blocked()

auto kcenon::network::protocols::quic::flow_controller::is_send_blocked ( ) const -> bool
nodiscardnoexcept

Check if send is blocked.

Definition at line 59 of file flow_control.cpp.

60{
61 return bytes_sent_ >= send_limit_;
62}

References bytes_sent_, and send_limit_.

Referenced by should_send_data_blocked().

Here is the caller graph for this function:

◆ mark_data_blocked_sent()

void kcenon::network::protocols::quic::flow_controller::mark_data_blocked_sent ( )

Mark DATA_BLOCKED as sent (to avoid repeated sending)

Definition at line 133 of file flow_control.cpp.

134{
135 data_blocked_sent_ = true;
136}

References data_blocked_sent_.

◆ operator=() [1/2]

auto kcenon::network::protocols::quic::flow_controller::operator= ( const flow_controller & ) -> flow_controller &=default
default

◆ operator=() [2/2]

auto kcenon::network::protocols::quic::flow_controller::operator= ( flow_controller && ) -> flow_controller &=default
defaultnoexcept

◆ receive_limit()

auto kcenon::network::protocols::quic::flow_controller::receive_limit ( ) const -> uint64_t
inlinenodiscardnoexcept

Get our receive limit (advertised as MAX_DATA)

Definition at line 110 of file flow_control.h.

110{ return receive_limit_; }

References receive_limit_.

◆ record_consumed()

void kcenon::network::protocols::quic::flow_controller::record_consumed ( uint64_t bytes)

Record data consumed by application.

Parameters
bytesNumber of bytes consumed

Definition at line 85 of file flow_control.cpp.

86{
87 bytes_consumed_ += bytes;
88 // Consumed cannot exceed received
91 }
92}

References bytes_consumed_, and bytes_received_.

◆ record_received()

auto kcenon::network::protocols::quic::flow_controller::record_received ( uint64_t bytes) -> VoidResult
nodiscard

Record received data.

Parameters
bytesNumber of bytes received
Returns
Success or error if limit exceeded

Definition at line 68 of file flow_control.cpp.

69{
70 if (bytes == 0) {
71 return ok();
72 }
73
74 const uint64_t new_total = bytes_received_ + bytes;
75 if (new_total > receive_limit_) {
77 "Received data exceeds flow control limit",
78 "quic::flow_controller");
79 }
80
81 bytes_received_ = new_total;
82 return ok();
83}

References kcenon::network::error_void(), kcenon::network::ok(), and kcenon::network::protocols::quic::flow_control_error::receive_overflow.

Here is the call graph for this function:

◆ reset()

void kcenon::network::protocols::quic::flow_controller::reset ( uint64_t initial_window)

Reset flow controller state.

Parameters
initial_windowNew initial window size

Definition at line 156 of file flow_control.cpp.

157{
158 send_limit_ = initial_window;
159 bytes_sent_ = 0;
160 data_blocked_sent_ = false;
161
162 receive_limit_ = initial_window;
163 bytes_received_ = 0;
164 bytes_consumed_ = 0;
165
166 window_size_ = initial_window;
167 last_sent_max_data_ = initial_window;
168}

References bytes_consumed_, bytes_received_, bytes_sent_, data_blocked_sent_, last_sent_max_data_, receive_limit_, send_limit_, and window_size_.

◆ send_limit()

auto kcenon::network::protocols::quic::flow_controller::send_limit ( ) const -> uint64_t
inlinenodiscardnoexcept

Get the current send limit (peer's MAX_DATA)

Definition at line 83 of file flow_control.h.

83{ return send_limit_; }

References send_limit_.

◆ set_update_threshold()

void kcenon::network::protocols::quic::flow_controller::set_update_threshold ( double threshold)

Set the threshold for sending MAX_DATA updates (0.0 - 1.0)

Parameters
thresholdFraction of window that triggers update

Definition at line 147 of file flow_control.cpp.

148{
149 update_threshold_ = std::clamp(threshold, 0.0, 1.0);
150}

References update_threshold_.

◆ set_window_size()

void kcenon::network::protocols::quic::flow_controller::set_window_size ( uint64_t window)

Set the window size for auto-tuning.

Parameters
windowNew window size

Definition at line 142 of file flow_control.cpp.

143{
144 window_size_ = window;
145}

References window_size_.

◆ should_send_data_blocked()

auto kcenon::network::protocols::quic::flow_controller::should_send_data_blocked ( ) const -> bool
nodiscardnoexcept

Check if DATA_BLOCKED frame should be sent.

Definition at line 128 of file flow_control.cpp.

129{
131}
auto is_send_blocked() const noexcept -> bool
Check if send is blocked.

References data_blocked_sent_, and is_send_blocked().

Here is the call graph for this function:

◆ should_send_max_data()

auto kcenon::network::protocols::quic::flow_controller::should_send_max_data ( ) const -> bool
nodiscardnoexcept

Check if MAX_DATA frame should be sent.

Definition at line 98 of file flow_control.cpp.

99{
100 // Send MAX_DATA when we've consumed enough of the window
101 const uint64_t threshold = static_cast<uint64_t>(window_size_ * update_threshold_);
102
103 // Calculate how much of the advertised window has been consumed
104 const uint64_t consumed_from_last = bytes_consumed_ - (last_sent_max_data_ - window_size_);
105
106 return consumed_from_last >= threshold;
107}

References bytes_consumed_, last_sent_max_data_, update_threshold_, and window_size_.

◆ update_send_limit()

void kcenon::network::protocols::quic::flow_controller::update_send_limit ( uint64_t max_data)

Update the send limit (from peer's MAX_DATA)

Parameters
max_dataNew maximum data limit

Definition at line 50 of file flow_control.cpp.

51{
52 // Only update if new limit is higher (MAX_DATA can only increase)
53 if (max_data > send_limit_) {
55 data_blocked_sent_ = false; // Can send again, reset blocked flag
56 }
57}

References data_blocked_sent_, kcenon::network::protocols::quic::max_data, and send_limit_.

Referenced by kcenon::network::protocols::quic::connection::apply_remote_params().

Here is the caller graph for this function:

◆ window_size()

auto kcenon::network::protocols::quic::flow_controller::window_size ( ) const -> uint64_t
inlinenodiscardnoexcept

Get the current window size.

Definition at line 160 of file flow_control.h.

160{ return window_size_; }

References window_size_.

Member Data Documentation

◆ bytes_consumed_

uint64_t kcenon::network::protocols::quic::flow_controller::bytes_consumed_ {0}
private

Definition at line 187 of file flow_control.h.

187{0}; // Bytes consumed by application

Referenced by bytes_consumed(), record_consumed(), reset(), and should_send_max_data().

◆ bytes_received_

uint64_t kcenon::network::protocols::quic::flow_controller::bytes_received_ {0}
private

Definition at line 186 of file flow_control.h.

186{0}; // Total bytes received

Referenced by bytes_received(), record_consumed(), and reset().

◆ bytes_sent_

uint64_t kcenon::network::protocols::quic::flow_controller::bytes_sent_ {0}
private

Definition at line 181 of file flow_control.h.

181{0}; // Total bytes we've sent

Referenced by available_send_window(), bytes_sent(), is_send_blocked(), and reset().

◆ data_blocked_sent_

bool kcenon::network::protocols::quic::flow_controller::data_blocked_sent_ {false}
private

Definition at line 182 of file flow_control.h.

182{false}; // DATA_BLOCKED already sent

Referenced by mark_data_blocked_sent(), reset(), should_send_data_blocked(), and update_send_limit().

◆ last_sent_max_data_

uint64_t kcenon::network::protocols::quic::flow_controller::last_sent_max_data_ {0}
private

Definition at line 194 of file flow_control.h.

194{0};

Referenced by reset(), and should_send_max_data().

◆ receive_limit_

uint64_t kcenon::network::protocols::quic::flow_controller::receive_limit_ {0}
private

Definition at line 185 of file flow_control.h.

185{0}; // Our MAX_DATA (peer's send limit)

Referenced by receive_limit(), and reset().

◆ send_limit_

uint64_t kcenon::network::protocols::quic::flow_controller::send_limit_ {0}
private

Definition at line 180 of file flow_control.h.

180{0}; // Peer's MAX_DATA (our send limit)

Referenced by available_send_window(), is_send_blocked(), reset(), send_limit(), and update_send_limit().

◆ update_threshold_

double kcenon::network::protocols::quic::flow_controller::update_threshold_ {0.5}
private

Definition at line 191 of file flow_control.h.

191{0.5}; // Send MAX_DATA when 50% consumed

Referenced by set_update_threshold(), and should_send_max_data().

◆ window_size_

uint64_t kcenon::network::protocols::quic::flow_controller::window_size_
private

Definition at line 190 of file flow_control.h.

Referenced by reset(), set_window_size(), should_send_max_data(), and window_size().


The documentation for this class was generated from the following files: