Browse Source

don't build tunnels for all pools at the time

pull/1553/merge
orignal 4 years ago
parent
commit
44ca315c75
  1. 20
      libi2pd/Tunnel.cpp
  2. 2
      libi2pd/Tunnel.h
  3. 15
      libi2pd/TunnelPool.cpp
  4. 4
      libi2pd/TunnelPool.h

20
libi2pd/Tunnel.cpp

@ -474,7 +474,7 @@ namespace tunnel @@ -474,7 +474,7 @@ namespace tunnel
{
std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready
uint64_t lastTs = 0;
uint64_t lastTs = 0, lastPoolsTs = 0;
while (m_IsRunning)
{
try
@ -535,12 +535,20 @@ namespace tunnel @@ -535,12 +535,20 @@ namespace tunnel
while (msg);
}
if (i2p::transport::transports.IsOnline())
{
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
if (ts - lastTs >= 15 && i2p::transport::transports.IsOnline()) // manage tunnels every 15 seconds
if (ts - lastTs >= 15) // manage tunnels every 15 seconds
{
ManageTunnels ();
lastTs = ts;
}
if (ts - lastPoolsTs >= 5) // manage pools every 5 seconds
{
ManageTunnelPools (ts);
lastPoolsTs = ts;
}
}
}
catch (std::exception& ex)
{
@ -582,7 +590,6 @@ namespace tunnel @@ -582,7 +590,6 @@ namespace tunnel
ManageInboundTunnels ();
ManageOutboundTunnels ();
ManageTransitTunnels ();
ManageTunnelPools ();
}
void Tunnels::ManagePendingTunnels ()
@ -794,16 +801,13 @@ namespace tunnel @@ -794,16 +801,13 @@ namespace tunnel
}
}
void Tunnels::ManageTunnelPools ()
void Tunnels::ManageTunnelPools (uint64_t ts)
{
std::unique_lock<std::mutex> l(m_PoolsMutex);
for (auto& pool : m_Pools)
{
if (pool && pool->IsActive ())
{
pool->CreateTunnels ();
pool->TestTunnels ();
}
pool->ManageTunnels (ts);
}
}

2
libi2pd/Tunnel.h

@ -230,7 +230,7 @@ namespace tunnel @@ -230,7 +230,7 @@ namespace tunnel
void ManagePendingTunnels ();
template<class PendingTunnels>
void ManagePendingTunnels (PendingTunnels& pendingTunnels);
void ManageTunnelPools ();
void ManageTunnelPools (uint64_t ts);
std::shared_ptr<ZeroHopsInboundTunnel> CreateZeroHopsInboundTunnel ();
std::shared_ptr<ZeroHopsOutboundTunnel> CreateZeroHopsOutboundTunnel ();

15
libi2pd/TunnelPool.cpp

@ -26,9 +26,10 @@ namespace tunnel @@ -26,9 +26,10 @@ namespace tunnel
{
TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels):
m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_IsActive (true),
m_CustomPeerSelector(nullptr)
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels),
m_IsActive (true), m_CustomPeerSelector(nullptr)
{
m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + rand () % TUNNEL_POOL_MANAGE_INTERVAL;
}
TunnelPool::~TunnelPool ()
@ -321,6 +322,16 @@ namespace tunnel @@ -321,6 +322,16 @@ namespace tunnel
}
}
void TunnelPool::ManageTunnels (uint64_t ts)
{
if (ts > m_NextManageTime)
{
CreateTunnels ();
TestTunnels ();
m_NextManageTime = ts + TUNNEL_POOL_MANAGE_INTERVAL + (rand () % TUNNEL_POOL_MANAGE_INTERVAL)/2;
}
}
void TunnelPool::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
{
if (m_LocalDestination)

4
libi2pd/TunnelPool.h

@ -27,6 +27,8 @@ namespace i2p @@ -27,6 +27,8 @@ namespace i2p
{
namespace tunnel
{
const int TUNNEL_POOL_MANAGE_INTERVAL = 10; // in seconds
class Tunnel;
class InboundTunnel;
class OutboundTunnel;
@ -69,6 +71,7 @@ namespace tunnel @@ -69,6 +71,7 @@ namespace tunnel
std::shared_ptr<InboundTunnel> GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded = nullptr) const;
std::shared_ptr<OutboundTunnel> GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const;
void TestTunnels ();
void ManageTunnels (uint64_t ts);
void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
void ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg);
@ -123,6 +126,7 @@ namespace tunnel @@ -123,6 +126,7 @@ namespace tunnel
mutable std::mutex m_TestsMutex;
std::map<uint32_t, std::pair<std::shared_ptr<OutboundTunnel>, std::shared_ptr<InboundTunnel> > > m_Tests;
bool m_IsActive;
uint64_t m_NextManageTime; // in seconds
std::mutex m_CustomPeerSelectorMutex;
ITunnelPeerSelector * m_CustomPeerSelector;

Loading…
Cancel
Save