mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-13 00:07:39 +01:00
last sample should have more relevance for latency
This commit is contained in:
parent
97818c6f32
commit
c2e7bc13a6
2 changed files with 11 additions and 51 deletions
29
Tunnel.cpp
29
Tunnel.cpp
|
@ -20,35 +20,10 @@ namespace i2p
|
||||||
{
|
{
|
||||||
namespace tunnel
|
namespace tunnel
|
||||||
{
|
{
|
||||||
|
|
||||||
void TunnelLatency::AddSample(Sample s)
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> l(m_access);
|
|
||||||
m_samples.push_back(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TunnelLatency::HasSamples() const
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> l(m_access);
|
|
||||||
return m_samples.size() > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
TunnelLatency::Latency TunnelLatency::GetMeanLatency() const
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(m_access);
|
|
||||||
if (m_samples.size() > 0) {
|
|
||||||
Latency l = 0;
|
|
||||||
for(auto s : m_samples)
|
|
||||||
l += s;
|
|
||||||
return l / m_samples.size();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Tunnel::Tunnel (std::shared_ptr<const TunnelConfig> config):
|
Tunnel::Tunnel (std::shared_ptr<const TunnelConfig> config):
|
||||||
TunnelBase (config->GetTunnelID (), config->GetNextTunnelID (), config->GetNextIdentHash ()),
|
TunnelBase (config->GetTunnelID (), config->GetNextTunnelID (), config->GetNextIdentHash ()),
|
||||||
m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending), m_IsRecreated (false)
|
m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending), m_IsRecreated (false),
|
||||||
|
m_Latency (0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
31
Tunnel.h
31
Tunnel.h
|
@ -79,21 +79,6 @@ namespace tunnel
|
||||||
eTunnelStateExpiring
|
eTunnelStateExpiring
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief for storing latency history */
|
|
||||||
struct TunnelLatency
|
|
||||||
{
|
|
||||||
typedef uint64_t Sample;
|
|
||||||
typedef uint64_t Latency;
|
|
||||||
|
|
||||||
|
|
||||||
void AddSample(Sample s);
|
|
||||||
bool HasSamples() const;
|
|
||||||
Latency GetMeanLatency() const;
|
|
||||||
|
|
||||||
std::vector<Sample> m_samples;
|
|
||||||
mutable std::mutex m_access;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OutboundTunnel;
|
class OutboundTunnel;
|
||||||
class InboundTunnel;
|
class InboundTunnel;
|
||||||
class Tunnel: public TunnelBase
|
class Tunnel: public TunnelBase
|
||||||
|
@ -133,14 +118,14 @@ namespace tunnel
|
||||||
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg);
|
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg);
|
||||||
void EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out);
|
void EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out);
|
||||||
|
|
||||||
/** @brief add latency sample */
|
/** @brief add latency sample */
|
||||||
void AddLatencySample(const uint64_t ms) { m_Latency.AddSample(ms); }
|
void AddLatencySample(const uint64_t ms) { m_Latency = (m_Latency + ms) >> 1; }
|
||||||
/** @brief get this tunnel's estimated latency */
|
/** @brief get this tunnel's estimated latency */
|
||||||
uint64_t GetMeanLatency() const { return m_Latency.GetMeanLatency(); }
|
uint64_t GetMeanLatency() const { return m_Latency; }
|
||||||
/** @breif return true if this tunnel's latency fits in range [lowerbound, upperbound] */
|
/** @breif return true if this tunnel's latency fits in range [lowerbound, upperbound] */
|
||||||
bool LatencyFitsRange(uint64_t lowerbound, uint64_t upperbound) const;
|
bool LatencyFitsRange(uint64_t lowerbound, uint64_t upperbound) const;
|
||||||
|
|
||||||
bool LatencyIsKnown() const { return m_Latency.HasSamples(); }
|
bool LatencyIsKnown() const { return m_Latency > 0; }
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void PrintHops (std::stringstream& s) const;
|
void PrintHops (std::stringstream& s) const;
|
||||||
|
@ -152,7 +137,7 @@ namespace tunnel
|
||||||
std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
|
std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
|
||||||
TunnelState m_State;
|
TunnelState m_State;
|
||||||
bool m_IsRecreated;
|
bool m_IsRecreated;
|
||||||
TunnelLatency m_Latency;
|
uint64_t m_Latency; // in milliseconds
|
||||||
};
|
};
|
||||||
|
|
||||||
class OutboundTunnel: public Tunnel
|
class OutboundTunnel: public Tunnel
|
||||||
|
|
Loading…
Add table
Reference in a new issue