Skip to content
API Documentation
BastionGuard™
NetQuality Struct Reference

#include <network_quality.hpp>

Public Member Functions

void update_sample (double rtt_ms, bool success)
std::string classify_network () const
int recommended_dns_timeout () const
int recommended_https_timeout () const
bool is_mobile_like () const
double rtt_ms () const
double loss () const

Public Attributes

std::atomic< double > ewma_rtt_ms {50.0}
std::atomic< double > ewma_loss {0.0}
std::atomic< int > samples {3}

Detailed Description

Definition at line 28 of file network_quality.hpp.

Member Function Documentation

◆ classify_network()

std::string NetQuality::classify_network ( ) const
inline

Definition at line 52 of file network_quality.hpp.

52 {
53 double rtt = ewma_rtt_ms.load(std::memory_order_relaxed);
54 double loss = ewma_loss.load(std::memory_order_relaxed);
55
56 if (samples.load(std::memory_order_relaxed) < 3)
57 return "bootstrap";
58
59 if (rtt < 25.0 && loss < 0.005)
60 return "fiber-ftth";
61 if (rtt < 50.0 && loss < 0.008)
62 return "ethernet";
63 if (rtt < 120.0 && loss < 0.015)
64 return "wifi";
65 if (rtt < 200.0 && loss < 0.03)
66 return "dsl/adsl";
67 if (rtt < 400.0 && loss < 0.05)
68 return "fwa-4g";
69 if (rtt < 700.0 && loss < 0.08)
70 return "5g";
71 return "unstable";
72 }
std::atomic< int > samples
std::atomic< double > ewma_rtt_ms
double loss() const
std::atomic< double > ewma_loss

References ewma_loss, ewma_rtt_ms, loss(), and samples.

Referenced by recommended_dns_timeout(), and recommended_https_timeout().

◆ is_mobile_like()

bool NetQuality::is_mobile_like ( ) const
inline

Definition at line 101 of file network_quality.hpp.

101 {
102 double rtt = ewma_rtt_ms.load(std::memory_order_relaxed);
103 double loss = ewma_loss.load(std::memory_order_relaxed);
104 return (rtt > 250.0) || (loss > 0.05);
105 }

References ewma_loss, ewma_rtt_ms, and loss().

◆ loss()

double NetQuality::loss ( ) const
inline

Definition at line 109 of file network_quality.hpp.

109{ return ewma_loss.load(std::memory_order_relaxed); }

References ewma_loss.

Referenced by classify_network(), and is_mobile_like().

◆ recommended_dns_timeout()

int NetQuality::recommended_dns_timeout ( ) const
inline

Definition at line 75 of file network_quality.hpp.

75 {
76 std::string net = classify_network();
77 if (net == "fiber-ftth") return 60;
78 if (net == "ethernet") return 80;
79 if (net == "wifi") return 120;
80 if (net == "dsl/adsl") return 160;
81 if (net == "fwa-4g") return 200;
82 if (net == "5g") return 150;
83 if (net == "bootstrap") return 100;
84 return 250;
85 }
std::string classify_network() const

References classify_network().

◆ recommended_https_timeout()

int NetQuality::recommended_https_timeout ( ) const
inline

Definition at line 88 of file network_quality.hpp.

88 {
89 std::string net = classify_network();
90 if (net == "fiber-ftth") return 400;
91 if (net == "ethernet") return 600;
92 if (net == "wifi") return 900;
93 if (net == "dsl/adsl") return 1200;
94 if (net == "fwa-4g") return 1500;
95 if (net == "5g") return 1100;
96 if (net == "bootstrap") return 800;
97 return 1800;
98 }

References classify_network().

◆ rtt_ms()

double NetQuality::rtt_ms ( ) const
inline

Definition at line 108 of file network_quality.hpp.

108{ return ewma_rtt_ms.load(std::memory_order_relaxed); }

References ewma_rtt_ms.

Referenced by update_sample().

◆ update_sample()

void NetQuality::update_sample ( double rtt_ms,
bool success )
inline

Definition at line 34 of file network_quality.hpp.

34 {
35 const double alpha = 0.55;
36 int s = samples.fetch_add(1, std::memory_order_relaxed) + 1;
37
38 double prev_rtt = ewma_rtt_ms.load(std::memory_order_relaxed);
39 double prev_loss = ewma_loss.load(std::memory_order_relaxed);
40
41
42 rtt_ms = std::clamp(rtt_ms, 1.0, 2000.0);
43
44 double new_rtt = alpha * rtt_ms + (1.0 - alpha) * prev_rtt;
45 double new_loss = alpha * (success ? 0.0 : 1.0) + (1.0 - alpha) * prev_loss;
46
47 ewma_rtt_ms.store(new_rtt, std::memory_order_relaxed);
48 ewma_loss.store(new_loss, std::memory_order_relaxed);
49 }
double rtt_ms() const

References ewma_loss, ewma_rtt_ms, rtt_ms(), and samples.

Referenced by NetQualityWarmup::NetQualityWarmup().

Member Data Documentation

◆ ewma_loss

std::atomic<double> NetQuality::ewma_loss {0.0}

Definition at line 30 of file network_quality.hpp.

Referenced by classify_network(), is_mobile_like(), loss(), and update_sample().

◆ ewma_rtt_ms

std::atomic<double> NetQuality::ewma_rtt_ms {50.0}

Definition at line 29 of file network_quality.hpp.

Referenced by classify_network(), is_mobile_like(), rtt_ms(), and update_sample().

◆ samples

std::atomic<int> NetQuality::samples {3}

Definition at line 31 of file network_quality.hpp.

Referenced by classify_network(), and update_sample().


The documentation for this struct was generated from the following file: