use own random number generator for tunnel selection

This commit is contained in:
orignal
2026-03-04 13:43:47 -05:00
parent cf802974c8
commit 8beb6772a6
2 changed files with 7 additions and 4 deletions
+5 -4
View File
@@ -45,7 +45,8 @@ namespace tunnel
m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels),
m_InboundVariance (inboundVariance), m_OutboundVariance (outboundVariance),
m_IsActive (true), m_IsHighBandwidth (isHighBandwidth), m_CustomPeerSelector(nullptr)
m_IsActive (true), m_IsHighBandwidth (isHighBandwidth), m_CustomPeerSelector(nullptr),
m_Rng (i2p::util::GetMonotonicMicroseconds ()%1000000LL)
{
if (m_NumInboundTunnels > TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY)
m_NumInboundTunnels = TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY;
@@ -59,7 +60,7 @@ namespace tunnel
m_InboundVariance = (m_NumInboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumInboundHops : 0;
if (m_OutboundVariance > 0 && m_NumOutboundHops + m_OutboundVariance > STANDARD_NUM_RECORDS)
m_OutboundVariance = (m_NumOutboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumOutboundHops : 0;
m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + rand () % TUNNEL_POOL_MANAGE_INTERVAL;
m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + m_Rng () % TUNNEL_POOL_MANAGE_INTERVAL;
}
TunnelPool::~TunnelPool ()
@@ -233,7 +234,7 @@ namespace tunnel
typename TTunnels::value_type excluded, i2p::data::RouterInfo::CompatibleTransports compatible)
{
if (tunnels.empty ()) return nullptr;
uint32_t ind = (m_LocalDestination ? m_LocalDestination->GetRng ()() : rand ()) % (tunnels.size ()/2 + 1), i = 0;
uint32_t ind = m_Rng () % (tunnels.size ()/2 + 1), i = 0;
bool skipped = false;
typename TTunnels::value_type tunnel = nullptr;
for (const auto& it: tunnels)
@@ -253,7 +254,7 @@ namespace tunnel
}
if (!tunnel && skipped)
{
ind = (m_LocalDestination ? m_LocalDestination->GetRng ()() : rand ()) % (tunnels.size ()/2 + 1), i = 0;
ind = m_Rng () % (tunnels.size ()/2 + 1), i = 0;
for (const auto& it: tunnels)
{
if (it->IsEstablished () && it != excluded)
+2
View File
@@ -15,6 +15,7 @@
#include <utility>
#include <mutex>
#include <memory>
#include <random>
#include "Identity.h"
#include "LeaseSet.h"
#include "RouterInfo.h"
@@ -151,6 +152,7 @@ namespace tunnel
uint64_t m_NextManageTime; // in seconds
std::mutex m_CustomPeerSelectorMutex;
ITunnelPeerSelector * m_CustomPeerSelector;
std::mt19937 m_Rng; // for tunnel selection
int m_MinLatency = 0; // if > 0 this tunnel pool will try building tunnels with minimum latency by ms
int m_MaxLatency = 0; // if > 0 this tunnel pool will try building tunnels with maximum latency by ms