implement dynamic timeout

This commit is contained in:
Green Sky 2023-03-12 01:24:11 +01:00
parent dfeb569aee
commit 95bfa2473c
No known key found for this signature in database
4 changed files with 44 additions and 49 deletions

View File

@ -11,15 +11,12 @@
#include <iostream> #include <iostream>
#include <limits> #include <limits>
// https://youtu.be/0HRwNSA-JYM
inline constexpr bool PLOTTING = false;
LEDBAT::LEDBAT(void) { LEDBAT::LEDBAT(void) {
_time_start_offset = clock::now(); _time_start_offset = clock::now();
{ // add some high delay values
// spec want +inf
//_rtt_buffer.push_back(_base_delay);
//_rtt_buffer.push_back(_base_delay);
//_rtt_buffer.push_back(_base_delay);
}
} }
size_t LEDBAT::canSend(void) const { size_t LEDBAT::canSend(void) const {
@ -27,16 +24,6 @@ size_t LEDBAT::canSend(void) const {
return 496u; return 496u;
} }
//const float time_since_last_sent {std::min(
//getTimeNow() - std::get<1>(_in_flight.back()),
//0.01f // 10ms max
//)};
//const float bps {std::min(
//(_cwnd / getCurrentDelay()),
//max_byterate_allowed
//)};
const int64_t cspace = _cwnd - _in_flight_bytes; const int64_t cspace = _cwnd - _in_flight_bytes;
if (cspace < 496) { if (cspace < 496) {
return 0u; return 0u;
@ -49,24 +36,27 @@ size_t LEDBAT::canSend(void) const {
size_t space = std::ceil(std::min(cspace, fspace) / 496.f) * 496.f; size_t space = std::ceil(std::min(cspace, fspace) / 496.f) * 496.f;
// data size, no overhead
//const int64_t can_send_size {std::min<int64_t>(
//bps * time_since_last_sent - segment_overhead,
//maximum_segment_size - segment_overhead
//)};
//const int64_t can_send_size {static_cast<int64_t>(bps * time_since_last_sent - segment_overhead)};
//if (can_send_size < 100) {
//return 0;
//} else {
//return can_send_size;
//}
return space; return space;
} }
std::vector<LEDBAT::SeqIDType> LEDBAT::getTimeouts(void) const {
std::vector<LEDBAT::SeqIDType> list;
// after 2 delays we trigger timeout
const auto now_adjusted = getTimeNow() - getCurrentDelay()*2.f;
for (const auto& [seq, time_stamp, size] : _in_flight) {
if (now_adjusted > time_stamp) {
list.push_back(seq);
}
}
return list;
}
void LEDBAT::onSent(SeqIDType seq, size_t data_size) { void LEDBAT::onSent(SeqIDType seq, size_t data_size) {
if (true) { if (false) {
for (const auto& it : _in_flight) { for (const auto& it : _in_flight) {
assert(std::get<0>(it) != seq); assert(std::get<0>(it) != seq);
} }
@ -107,12 +97,7 @@ void LEDBAT::onAck(std::vector<SeqIDType> seqs) {
return; // not found, ignore return; // not found, ignore
} }
//addRTT(now - most_recent);
updateWindows(); updateWindows();
// update cto - no? we dont handle timeouts
} }
void LEDBAT::onLoss(SeqIDType seq, bool discard) { void LEDBAT::onLoss(SeqIDType seq, bool discard) {
@ -129,7 +114,7 @@ void LEDBAT::onLoss(SeqIDType seq, bool discard) {
// at most once per rtt? // at most once per rtt?
if (false) { if (PLOTTING) {
std::cerr << "CCA: onLoss: TIME: " << getTimeNow() << "\n"; std::cerr << "CCA: onLoss: TIME: " << getTimeNow() << "\n";
} }
@ -243,7 +228,7 @@ void LEDBAT::updateWindows(void) {
} // no else, we on point. very unlikely with float } // no else, we on point. very unlikely with float
} }
if (false) { // plotting if (PLOTTING) { // plotting
std::cerr << std::fixed << "CCA: onAck: TIME: " << now << " cwnd: " << _cwnd << "\n"; std::cerr << std::fixed << "CCA: onAck: TIME: " << now << " cwnd: " << _cwnd << "\n";
std::cerr << std::fixed << "CCA: onAck: TIME: " << now << " fwnd: " << _fwnd << "\n"; std::cerr << std::fixed << "CCA: onAck: TIME: " << now << " fwnd: " << _fwnd << "\n";
std::cerr << std::fixed << "CCA: onAck: TIME: " << now << " current_delay: " << current_delay << "\n"; std::cerr << std::fixed << "CCA: onAck: TIME: " << now << " current_delay: " << current_delay << "\n";

View File

@ -29,17 +29,17 @@ struct LEDBAT {
static_assert(maximum_segment_size == 574); // mesured in wireshark static_assert(maximum_segment_size == 574); // mesured in wireshark
// ledbat++ says 60ms, we might need other values if relayed // ledbat++ says 60ms, we might need other values if relayed
const float target_delay {0.060f}; //const float target_delay {0.060f};
//const float target_delay {0.030f}; const float target_delay {0.030f};
//const float target_delay {0.120f}; // 2x if relayed? //const float target_delay {0.120f}; // 2x if relayed?
// TODO: use a factor for multiple of rtt // TODO: use a factor for multiple of rtt
static constexpr size_t current_delay_filter_window {16*4*2}; static constexpr size_t current_delay_filter_window {16*4};
//static constexpr size_t rtt_buffer_size_max {2000}; //static constexpr size_t rtt_buffer_size_max {2000};
float max_byterate_allowed {10*1024*1024}; // 10MiB/s //float max_byterate_allowed {10*1024*1024}; // 10MiB/s
//float max_byterate_allowed {2*1024*1024}; float max_byterate_allowed {1*1024*1024};
public: public:
LEDBAT(void); LEDBAT(void);
@ -55,6 +55,9 @@ struct LEDBAT {
// respect max_byterate_allowed // respect max_byterate_allowed
size_t canSend(void) const; size_t canSend(void) const;
// get the list of timed out seq_ids
std::vector<SeqIDType> getTimeouts(void) const;
public: // callbacks public: // callbacks
// data size is without overhead // data size is without overhead
void onSent(SeqIDType seq, size_t data_size); void onSent(SeqIDType seq, size_t data_size);

View File

@ -11,6 +11,7 @@
// TODO: should i really use both? // TODO: should i really use both?
#include <unordered_map> #include <unordered_map>
#include <map> #include <map>
#include <set>
#include <optional> #include <optional>
#include <cassert> #include <cassert>
#include <cstdio> #include <cstdio>
@ -216,7 +217,9 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
for (auto& [group_number, group] : ngc_ft1_ctx->groups) { for (auto& [group_number, group] : ngc_ft1_ctx->groups) {
for (auto& [peer_number, peer] : group.peers) { for (auto& [peer_number, peer] : group.peers) {
//for (auto& tf_opt : peer.send_transfers) { auto timeouts = peer.cca.getTimeouts();
std::set<LEDBAT::SeqIDType> timeouts_set{timeouts.cbegin(), timeouts.cend()};
for (size_t idx = 0; idx < peer.send_transfers.size(); idx++) { for (size_t idx = 0; idx < peer.send_transfers.size(); idx++) {
auto& tf_opt = peer.send_transfers[idx]; auto& tf_opt = peer.send_transfers[idx];
if (tf_opt.has_value()) { if (tf_opt.has_value()) {
@ -245,11 +248,13 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
case State::SENDING: { case State::SENDING: {
tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) { tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) {
// no ack after 5 sec -> resend // no ack after 5 sec -> resend
if (time_since_activity >= ngc_ft1_ctx->options.sending_resend_without_ack_after) { //if (time_since_activity >= ngc_ft1_ctx->options.sending_resend_without_ack_after) {
if (timeouts_set.count({idx, id})) {
// TODO: can fail // TODO: can fail
_send_pkg_FT1_DATA(tox, group_number, peer_number, idx, id, data.data(), data.size()); _send_pkg_FT1_DATA(tox, group_number, peer_number, idx, id, data.data(), data.size());
peer.cca.onLoss({idx, id}, false); peer.cca.onLoss({idx, id}, false);
time_since_activity = 0.f; time_since_activity = 0.f;
timeouts_set.erase({idx, id});
} }
}); });
@ -261,6 +266,7 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
// clean up cca // clean up cca
tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) { tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) {
peer.cca.onLoss({idx, id}, true); peer.cca.onLoss({idx, id}, true);
timeouts_set.erase({idx, id});
}); });
tf_opt.reset(); tf_opt.reset();
@ -322,10 +328,12 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
case State::FINISHING: // we still have unacked packets case State::FINISHING: // we still have unacked packets
tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) { tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) {
// no ack after 5 sec -> resend // no ack after 5 sec -> resend
if (time_since_activity >= ngc_ft1_ctx->options.sending_resend_without_ack_after) { //if (time_since_activity >= ngc_ft1_ctx->options.sending_resend_without_ack_after) {
if (timeouts_set.count({idx, id})) {
_send_pkg_FT1_DATA(tox, group_number, peer_number, idx, id, data.data(), data.size()); _send_pkg_FT1_DATA(tox, group_number, peer_number, idx, id, data.data(), data.size());
peer.cca.onLoss({idx, id}, false); peer.cca.onLoss({idx, id}, false);
time_since_activity = 0.f; time_since_activity = 0.f;
timeouts_set.erase({idx, id});
} }
}); });
if (tf.time_since_activity >= ngc_ft1_ctx->options.sending_give_up_after) { if (tf.time_since_activity >= ngc_ft1_ctx->options.sending_give_up_after) {
@ -336,6 +344,7 @@ void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx, float time_delta) {
// clean up cca // clean up cca
tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) { tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector<uint8_t>& data, float& time_since_activity) {
peer.cca.onLoss({idx, id}, true); peer.cca.onLoss({idx, id}, true);
timeouts_set.erase({idx, id});
}); });
tf_opt.reset(); tf_opt.reset();

View File

@ -22,10 +22,8 @@ struct NGC_FT1_options {
float init_retry_timeout_after; // 10sec float init_retry_timeout_after; // 10sec
float sending_resend_without_ack_after; // 5sec //float sending_resend_without_ack_after; // 5sec
float sending_give_up_after; // 30sec float sending_give_up_after; // 30sec
size_t packet_window_size; // 2
}; };
// uint32_t - same as tox friend ft // uint32_t - same as tox friend ft