|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 NUClear Contributors |
| 5 | + * |
| 6 | + * This file is part of the NUClear codebase. |
| 7 | + * See https://github.com/Fastcode/NUClear for further info. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 15 | + * Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 20 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | +#ifndef NUCLEAR_EXTENSION_NETWORK_RTT_ESTIMATOR_HPP |
| 23 | +#define NUCLEAR_EXTENSION_NETWORK_RTT_ESTIMATOR_HPP |
| 24 | + |
| 25 | +#include <chrono> |
| 26 | +#include <cstdint> |
| 27 | +#include <stdexcept> |
| 28 | + |
| 29 | +namespace NUClear { |
| 30 | +namespace extension { |
| 31 | + namespace network { |
| 32 | + |
| 33 | + /** |
| 34 | + * Implements TCP-style Round Trip Time (RTT) estimation using Jacobson/Karels algorithm. |
| 35 | + * |
| 36 | + * This class provides RTT estimation functionality similar to TCP's RTT estimation mechanism. |
| 37 | + * It uses an Exponentially Weighted Moving Average (EWMA) to smooth RTT measurements and |
| 38 | + * calculate a retransmission timeout (RTO) value. The implementation follows the TCP |
| 39 | + * Jacobson/Karels algorithm which provides robust RTT estimation that: |
| 40 | + * - Smoothly tracks the mean RTT |
| 41 | + * - Adapts to RTT variations |
| 42 | + * - Handles network jitter |
| 43 | + * - Provides conservative timeout values |
| 44 | + */ |
| 45 | + class RTTEstimator { |
| 46 | + public: |
| 47 | + /** |
| 48 | + * Construct a new RTT Estimator |
| 49 | + * |
| 50 | + * @param alpha Weight for RTT smoothing (default: 0.125, TCP standard) |
| 51 | + * @param beta Weight for RTT variation (default: 0.25, TCP standard) |
| 52 | + * @param initial_rtt Initial RTT estimate in seconds (default: 1.0) |
| 53 | + * @param initial_rtt_var Initial RTT variation in seconds (default: 0.0) |
| 54 | + * @param min_rto Minimum RTO value in seconds (default: 0.1) |
| 55 | + * @param max_rto Maximum RTO value in seconds (default: 60.0) |
| 56 | + * |
| 57 | + * The alpha and beta parameters control how quickly the estimator adapts to changes: |
| 58 | + * - alpha: Lower values (e.g. 0.125) make the smoothed RTT more stable but slower to adapt |
| 59 | + * - beta: Lower values (e.g. 0.25) make the RTT variation more stable but slower to adapt |
| 60 | + * |
| 61 | + * @throws std::invalid_argument if alpha or beta are not in range [0,1] |
| 62 | + * @throws std::invalid_argument if min_rto >= max_rto |
| 63 | + */ |
| 64 | + RTTEstimator(float alpha = 0.125f, |
| 65 | + float beta = 0.25f, |
| 66 | + float initial_rtt = 1.0f, |
| 67 | + float initial_rtt_var = 0.0f, |
| 68 | + float min_rto = 0.1f, |
| 69 | + float max_rto = 60.0f) |
| 70 | + : alpha(alpha) |
| 71 | + , beta(beta) |
| 72 | + , min_rto(min_rto) |
| 73 | + , max_rto(max_rto) |
| 74 | + , smoothed_rtt(initial_rtt) |
| 75 | + , rtt_var(initial_rtt_var) |
| 76 | + , rto(std::min(std::max(initial_rtt + 4 * initial_rtt_var, min_rto), max_rto)) { |
| 77 | + |
| 78 | + if (alpha < 0.0f || alpha > 1.0f) { |
| 79 | + throw std::invalid_argument("alpha must be in range [0,1]"); |
| 80 | + } |
| 81 | + if (beta < 0.0f || beta > 1.0f) { |
| 82 | + throw std::invalid_argument("beta must be in range [0,1]"); |
| 83 | + } |
| 84 | + if (min_rto >= max_rto) { |
| 85 | + throw std::invalid_argument("min_rto must be less than max_rto"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Update the RTT estimate with a new measurement |
| 91 | + * |
| 92 | + * Updates the smoothed RTT, RTT variation, and RTO using the Jacobson/Karels algorithm: |
| 93 | + * 1. RTT variation = (1 - beta) * old_variation + beta * |smoothed_rtt - new_rtt| |
| 94 | + * 2. Smoothed RTT = (1 - alpha) * old_rtt + alpha * new_rtt |
| 95 | + * 3. RTO = smoothed_rtt + 4 * rtt_var |
| 96 | + * |
| 97 | + * The RTO is bounded between min_rto and max_rto to prevent extreme values. |
| 98 | + * |
| 99 | + * @param time The measured round trip time |
| 100 | + */ |
| 101 | + void measure(std::chrono::steady_clock::duration time); |
| 102 | + |
| 103 | + /** |
| 104 | + * Get the current retransmission timeout |
| 105 | + * |
| 106 | + * @return The RTO as a duration. This value represents the recommended timeout |
| 107 | + * for network operations based on the current RTT estimates. |
| 108 | + */ |
| 109 | + std::chrono::steady_clock::duration timeout() const; |
| 110 | + |
| 111 | + private: |
| 112 | + float alpha; ///< Weight for RTT smoothing (typically 0.125) |
| 113 | + float beta; ///< Weight for RTT variation (typically 0.25) |
| 114 | + float min_rto; ///< Minimum RTO value in seconds |
| 115 | + float max_rto; ///< Maximum RTO value in seconds |
| 116 | + float smoothed_rtt; ///< Smoothed RTT estimate in seconds |
| 117 | + float rtt_var; ///< RTT variation in seconds |
| 118 | + float rto; ///< Retransmission timeout in seconds |
| 119 | + }; |
| 120 | + |
| 121 | + } // namespace network |
| 122 | +} // namespace extension |
| 123 | +} // namespace NUClear |
| 124 | + |
| 125 | +#endif // NUCLEAR_EXTENSION_NETWORK_RTT_ESTIMATOR_HPP |
0 commit comments